数据多语言

This commit is contained in:
2025-05-07 14:19:47 +08:00
parent 4c66404a6c
commit 3eea6db29d
19 changed files with 1989 additions and 825 deletions
+105 -16
View File
@@ -3,10 +3,20 @@
namespace app\controller;
use app\model\Vehicle;
use app\model\VehicleEn;
use app\model\VehicleRu;
use app\model\VehicleAr;
use app\model\Location;
use app\model\LocationEn;
use app\model\LocationRu;
use app\model\LocationAr;
use app\service\OssService;
use plugin\admin\app\model\Location;
// use plugin\admin\app\model\Location;
use util\Util;
use support\Request;
use support\Cache;
class VehicleController
{
@@ -28,26 +38,85 @@ class VehicleController
*/
public function index(Request $request)
{
$vehicles = Vehicle::with('location')->with('finalLocation')->get();
$vehicles->each(function ($vehicle) {
$lang = $request->post('lang', 'zh');
switch ($lang) {
case 'zh':
$vehicles = Vehicle::with('location')->with('finalLocation')->get();
$hz = new Location();
break;
case 'en':
$vehicles = VehicleEn::with('location')->with('finalLocation')->get();
$hz = new LocationEn();
break;
case 'ru':
$vehicles = VehicleRu::with('location')->with('finalLocation')->get();
$hz = new LocationRu();
break;
case 'ar':
$vehicles = VehicleAr::with('location')->with('finalLocation')->get();
$hz = new LocationAr();
break;
}
// // $vehicles = Vehicle::with('location')->with('finalLocation')->get();
// $vehicles->each(function ($vehicle) use ($hz) {
// if (isset($vehicle->vin)) {
// $vehicle->vin = $this->maskVin($vehicle->vin);
// }
// // $vehicle->status = $this->status[$vehicle->status]??$this->status[0];
// // $location = Location::find($vehicle->locations_id);
// // $vehicle->location_name = $location ? $location->name : '';
// // $final_location = Location::find($vehicle->final_locations_id);
// // $vehicle->final_location_name = $final_location ? $final_location->name : '';
// $fbo_location = $hz->find($vehicle->fbo_locations_id);
// $vehicle->fbo_destination = $fbo_location ? $fbo_location->name : '';
// if(!str_starts_with($vehicle['img_url'], 'http')){
// $ossservice = new OssService();
// $img_url = $ossservice->generatePrivateUrl($vehicle['img_url'],86400);
// $vehicle['img_url'] = $img_url;
// }
// });
// =================================== new ===================================== start
// 单独处理 locations
$fboIds = $vehicles->pluck('fbo_locations_id')->toArray();
$fbo = $hz->whereIn('id', array_unique($fboIds))->get()->toArray();
$arr = [];
foreach ($fbo as $value) {
$arr[$value['id']] = $value;
}
// 组装数据
foreach ($vehicles as &$vehicle) {
if (isset($vehicle->vin)) {
$vehicle->vin = $this->maskVin($vehicle->vin);
}
// $vehicle->status = $this->status[$vehicle->status]??$this->status[0];
// $location = Location::find($vehicle->locations_id);
// $vehicle->location_name = $location ? $location->name : '';
// $final_location = Location::find($vehicle->final_locations_id);
// $vehicle->final_location_name = $final_location ? $final_location->name : '';
$fbo_location = Location::find($vehicle->fbo_locations_id);
$vehicle->fbo_destination = $fbo_location ? $fbo_location->name : '';
$vehicle->fbo_destination = isset($arr[$vehicle->fbo_locations_id]) ? $arr[$vehicle->fbo_locations_id]['name'] : '';
if(!str_starts_with($vehicle['img_url'], 'http')){
$ossservice = new OssService();
$img_url = $ossservice->generatePrivateUrl($vehicle['img_url'],86400);
$vehicle['img_url'] = $img_url;
$img_url = Cache::get('img_'.$vehicle->id);
if ($img_url) {
$vehicle['img_url'] = $img_url;
} else {
$ossservice = new OssService();
$img_url = $ossservice->generatePrivateUrl($vehicle['img_url'],86400);
$vehicle['img_url'] = $img_url;
Cache::set('img_'.$vehicle->id, $img_url, 86390);
}
}
});
}
unset($vehicle);
// =================================== new ===================================== end
$result = ['list' => $vehicles,'count'=>count($vehicles)];
return Util::success($result);
}
@@ -57,7 +126,27 @@ class VehicleController
*/
public function show(Request $request, $id)
{
$vehicle = Vehicle::with('location')->find($id);
$lang = $request->post('lang', 'zh');
switch ($lang) {
case 'zh':
$vehicle = Vehicle::with('location')->find($id);
break;
case 'en':
$vehicle = VehicleEn::with('location')->find($id);
break;
case 'ru':
$vehicle = VehicleRu::with('location')->find($id);
break;
case 'ar':
$vehicle = VehicleAr::with('location')->find($id);
break;
}
// $vehicle = Vehicle::with('location')->find($id);
if (!$vehicle) {
return Util::fail($vehicle,'Vehicle not found');
}