Files
car_info/plugin/admin/app/controller/VehicleController.php
T

177 lines
5.3 KiB
PHP
Raw Normal View History

2025-04-30 11:30:08 +08:00
<?php
2025-05-06 17:16:37 +08:00
declare (strict_types = 1);
2025-04-30 11:30:08 +08:00
namespace plugin\admin\app\controller;
2025-04-30 14:00:54 +08:00
use app\service\OssService;
2025-04-30 11:30:08 +08:00
use plugin\admin\app\model\Location;
2025-04-30 14:51:12 +08:00
use support\Log;
2025-04-30 11:30:08 +08:00
use support\Request;
use support\Response;
use plugin\admin\app\model\Vehicle;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
/**
* 车辆信息
*/
class VehicleController extends Crud
{
/**
* 不需要登录的方法
* @var string[]
*/
protected $noNeedLogin = ['detail'];
/**
* 不需要鉴权的方法
* @var string[]
*/
protected $noNeedAuth = ['detail'];
protected $status = ['未知','待发车','在途(发往fbo港口)','抵达发货港口','在途(发往目的地仓库)','抵达仓库','售出'];
/**
* @var Vehicle
*/
protected $model = null;
/**
* 构造函数
* @return void
*/
public function __construct()
{
$this->model = new Vehicle;
}
/**
* 浏览
* @return Response
*/
public function index(): Response
{
2025-05-06 17:16:37 +08:00
$lang = session('lang', 'zh-cn');
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
$translations = array_merge($translations, $common);
return view('vehicle/index', [
'lang' => $lang,
'translations' => $translations
]);
2025-04-30 11:30:08 +08:00
}
/**
* 插入
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function insert(Request $request): Response
{
2025-05-06 17:16:37 +08:00
$lang = session('lang', 'zh-cn');
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
$translations = array_merge($translations, $common);
return view('vehicle/insert', [
'lang' => $lang,
'translations' => $translations
]);
2025-04-30 11:30:08 +08:00
}
/**
* 更新
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function update(Request $request): Response
{
2025-05-06 17:16:37 +08:00
$id = $request->input('id');
$lang = session('lang', 'zh-cn');
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
$translations = array_merge($translations, $common);
$data = Vehicle::where('id', $id)->first();
if (!$data) {
return json(['code' => 1, 'msg' => $translations['data_not_found'] ?? '数据不存在']);
2025-04-30 11:30:08 +08:00
}
2025-05-06 17:16:37 +08:00
return view('vehicle/update', [
'id' => $id,
'data' => $data,
'lang' => $lang,
'translations' => $translations
]);
2025-04-30 11:30:08 +08:00
}
/**
* 详情
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function detail(Request $request): Response
{
2025-05-06 17:16:37 +08:00
$id = $request->input('id');
$lang = session('lang', 'zh-cn');
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
$translations = array_merge($translations, $common);
$data = Vehicle::where('id', $id)->first();
if (!$data) {
return json(['code' => 1, 'msg' => $translations['data_not_found'] ?? '数据不存在']);
}
return view('vehicle/detail', [
'data' => $data,
'lang' => $lang,
'translations' => $translations
]);
2025-04-30 11:30:08 +08:00
}
/**
* 查询后置处理,添加位置信息
* @param mixed $items
* @return mixed
*/
protected function afterQuery($items)
{
foreach ($items as &$item) {
$location = Location::find($item->locations_id);
$item->status = $this->status[$item->status]??$this->status[0];
$item->location_name = $location ? $location->name : '';
$final_location = Location::find($item->final_locations_id);
$item->final_location_name = $final_location ? $final_location->name : '';
$fbo_location = Location::find($item->fbo_locations_id);
$item->fbo_location_name = $fbo_location ? $fbo_location->name : '';
2025-04-30 14:00:54 +08:00
2025-04-30 14:58:01 +08:00
if(!str_starts_with($item['img_url'], 'http')){
try {
$ossservice = new OssService();
2025-04-30 15:32:58 +08:00
$img_url = $ossservice->generatePrivateUrl($item['img_url']);
2025-04-30 14:58:01 +08:00
Log::info("Vehicle image URL generation:", [
'original_url' => $item['img_url'],
2025-04-30 15:06:32 +08:00
'generated_url' => $img_url,
'current_time' => time()
2025-04-30 14:58:01 +08:00
]);
$item['img_url'] = $img_url;
} catch (\Exception $e) {
Log::error("Vehicle image URL generation failed:", [
'error' => $e->getMessage(),
2025-04-30 15:06:32 +08:00
'original_url' => $item['img_url'],
'error_code' => $e->getCode(),
'trace' => $e->getTraceAsString()
2025-04-30 14:58:01 +08:00
]);
}
2025-04-30 14:09:13 +08:00
}
2025-04-30 14:00:54 +08:00
}
2025-04-30 14:09:13 +08:00
2025-04-30 11:30:08 +08:00
return $items;
}
}