Files
car_info/app/controller/VehicleController.php
T

162 lines
5.7 KiB
PHP
Raw Normal View History

2025-04-25 11:30:44 +08:00
<?php
namespace app\controller;
use app\model\Vehicle;
2025-05-07 14:19:47 +08:00
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;
2025-04-30 14:00:54 +08:00
use app\service\OssService;
2025-05-07 14:19:47 +08:00
// use plugin\admin\app\model\Location;
2025-05-08 15:53:42 +08:00
use app\service\TranslatorService;
2025-04-30 11:30:08 +08:00
use util\Util;
2025-04-25 11:30:44 +08:00
use support\Request;
2025-05-07 14:19:47 +08:00
use support\Cache;
2025-04-25 11:30:44 +08:00
class VehicleController
{
2025-04-30 11:30:08 +08:00
2025-05-13 15:29:30 +08:00
protected $status = ['未知','待发车','在途(发往fbo港口)','抵达发货港口','在途(发往目的地仓库)','抵达目的地','售出'];
2025-04-25 11:30:44 +08:00
/**
* Mask VIN number by showing only first 2 and last 6 characters
*/
private function maskVin($vin)
{
if (strlen($vin) <= 8) {
return $vin;
}
return substr($vin, 0, 2) . str_repeat('*', strlen($vin) - 8) . substr($vin, -6);
}
/**
* Get all vehicles with their locations
*/
public function index(Request $request)
{
2025-05-07 14:19:47 +08:00
$lang = $request->post('lang', 'zh');
2025-05-08 15:53:42 +08:00
// 接收请求参数(Webman自动注入)
$page = $request->post('page', 1); // 默认第1页
$pageSize = $request->post('pageSize', 30); // 默认每页30条
2025-05-07 14:19:47 +08:00
switch ($lang) {
case 'zh':
2025-05-08 15:53:42 +08:00
$vehicles = Vehicle::with('location')->with('finalLocation') ->offset(($page-1)*$pageSize) ->limit($pageSize)->get();
2025-05-07 14:19:47 +08:00
$hz = new Location();
break;
case 'en':
2025-05-08 15:53:42 +08:00
$vehicles = VehicleEn::with('location')->with('finalLocation')->offset(($page-1)*$pageSize) ->limit($pageSize)->get();
2025-05-07 14:19:47 +08:00
$hz = new LocationEn();
break;
case 'ru':
2025-05-08 15:53:42 +08:00
$vehicles = VehicleRu::with('location')->with('finalLocation')->offset(($page-1)*$pageSize) ->limit($pageSize)->get();
2025-05-07 14:19:47 +08:00
$hz = new LocationRu();
break;
case 'ar':
2025-05-08 15:53:42 +08:00
$vehicles = VehicleAr::with('location')->with('finalLocation')->offset(($page-1)*$pageSize) ->limit($pageSize)->get();
2025-05-07 14:19:47 +08:00
$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) {
2025-04-25 11:30:44 +08:00
if (isset($vehicle->vin)) {
$vehicle->vin = $this->maskVin($vehicle->vin);
}
2025-05-07 14:19:47 +08:00
$vehicle->fbo_destination = isset($arr[$vehicle->fbo_locations_id]) ? $arr[$vehicle->fbo_locations_id]['name'] : '';
2025-05-13 15:29:30 +08:00
$vehicle->status = $this->status[$vehicle->status]??$this->status[0];
2025-04-30 14:05:55 +08:00
if(!str_starts_with($vehicle['img_url'], 'http')){
2025-05-19 13:30:06 +08:00
$img_url = Cache::get('img_dibai_'.$vehicle->id);
2025-05-07 14:19:47 +08:00
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;
2025-05-19 13:30:06 +08:00
Cache::set('img_dibai_'.$vehicle->id, $img_url, 86390);
2025-05-07 14:19:47 +08:00
}
2025-04-30 14:00:54 +08:00
}
2025-05-07 14:19:47 +08:00
}
unset($vehicle);
// =================================== new ===================================== end
2025-05-08 15:53:42 +08:00
$count = Vehicle::count();
$result = ['list' => $vehicles,'count'=>$count];
2025-04-25 11:30:44 +08:00
return Util::success($result);
}
/**
* Get a specific vehicle with its location
*/
public function show(Request $request, $id)
{
2025-05-07 14:19:47 +08:00
$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);
2025-04-25 11:30:44 +08:00
if (!$vehicle) {
return Util::fail($vehicle,'Vehicle not found');
}
if (isset($vehicle->vin)) {
$vehicle->vin = $this->maskVin($vehicle->vin);
}
return Util::success($vehicle);
}
}