2025-05-14 13:19:23 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\apis\controller;
|
|
|
|
|
|
2025-05-23 16:02:35 +08:00
|
|
|
use app\apis\model\BrandBannerModel;
|
|
|
|
|
use app\apis\model\BrandTraitModel;
|
2025-06-03 13:24:31 +08:00
|
|
|
use app\apis\model\BrandTranslationModel;
|
2025-07-28 14:27:22 +08:00
|
|
|
use app\apis\model\VehicleNewModel;
|
|
|
|
|
use app\apis\service\VehicleNewService;
|
2025-05-14 13:19:23 +08:00
|
|
|
use support\Request;
|
|
|
|
|
use support\Response;
|
|
|
|
|
use app\apis\model\BrandModel;
|
|
|
|
|
use app\apis\service\BrandService;
|
|
|
|
|
use app\apis\validate\BrandValidator;
|
|
|
|
|
use app\mzl\common\controller\BaseController;
|
|
|
|
|
|
|
|
|
|
class BrandController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 不需要验证token的接口
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $noNeedJwt = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证器
|
|
|
|
|
*/
|
|
|
|
|
protected mixed $validator = BrandValidator::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回业务 Service 实例
|
|
|
|
|
*/
|
|
|
|
|
protected function getService()
|
|
|
|
|
{
|
|
|
|
|
return new BrandService(new BrandModel());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 额外字段处理 (根据场景处理)
|
|
|
|
|
* @param array $item
|
|
|
|
|
* @param string $scene
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function postProcessItem(array $item, string $scene): array
|
|
|
|
|
{
|
2025-05-23 16:49:21 +08:00
|
|
|
if ($scene === 'default' || $scene === 'letter' || $scene === 'hot' || $scene === 'show') {
|
2025-05-23 16:02:35 +08:00
|
|
|
$result = [
|
|
|
|
|
'id' => $item['id'],
|
|
|
|
|
'status' => $item['status'],
|
|
|
|
|
'sort' => $item['sort'],
|
|
|
|
|
'toppic' => $item['toppic'],
|
|
|
|
|
'descpic' => $item['descpic'],
|
|
|
|
|
'logo' => $item['logo'],
|
|
|
|
|
'is_hot' => $item['is_hot'],
|
|
|
|
|
'name' => isset($item['translation']) ? $item['translation']['name'] : '',
|
|
|
|
|
'desc' => isset($item['translation']) ? $item['translation']['desc'] : '',
|
|
|
|
|
'toppic_alt' => isset($item['translation']) ? $item['translation']['toppic_alt'] : '',
|
|
|
|
|
'descpic_alt' => isset($item['translation']) ? $item['translation']['descpic_alt'] : '',
|
|
|
|
|
'logo_alt' => isset($item['translation']) ? $item['translation']['logo_alt'] : '',
|
|
|
|
|
'created_at' => date('Y-m-d H:i:s', $item['created_at']),
|
|
|
|
|
'updated_at' => date('Y-m-d H:i:s', $item['updated_at']),
|
|
|
|
|
];
|
2025-06-03 13:24:31 +08:00
|
|
|
if($scene === 'letter'){
|
|
|
|
|
$brand = BrandTranslationModel::where(['brand_id' => $item['id'],'locale' => 'en'])->first();
|
|
|
|
|
$result['name'] = isset($brand['name'])?$brand['name']:$result['name'];
|
|
|
|
|
}
|
2025-05-23 16:02:35 +08:00
|
|
|
if ($item['toppic']) {
|
|
|
|
|
$result['toppic_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['toppic']);
|
|
|
|
|
}
|
|
|
|
|
if ($item['descpic']) {
|
|
|
|
|
$result['descpic_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['descpic']);
|
|
|
|
|
}
|
|
|
|
|
if ($item['logo']) {
|
|
|
|
|
$result['logo_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['logo']);
|
|
|
|
|
}
|
|
|
|
|
$banners = BrandBannerModel::with('translation')->where(['brand_id' => $item['id']])->get();
|
|
|
|
|
foreach ($banners as $k => $v) {
|
|
|
|
|
$v['alt'] = isset($v['translation']) ? $v['translation']['alt'] : '';
|
|
|
|
|
unset($v['translation']);
|
|
|
|
|
if ($v['pic']) {
|
|
|
|
|
$v['pic_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($v['pic']);
|
|
|
|
|
}
|
|
|
|
|
$result['banner'][$k] = $v;
|
|
|
|
|
}
|
|
|
|
|
$traits = BrandTraitModel::with('translation')->where(['brand_id' => $item['id']])->get();
|
|
|
|
|
foreach ($traits as $k => $v) {
|
|
|
|
|
$v['title'] = isset($v['translation']) ? $v['translation']['title'] : '';
|
|
|
|
|
$v['content'] = isset($v['translation']) ? $v['translation']['content'] : '';
|
|
|
|
|
unset($v['translation']);
|
|
|
|
|
$result['trait'][$k] = $v;
|
|
|
|
|
}
|
2025-07-28 14:27:22 +08:00
|
|
|
if($scene === 'hot'){
|
|
|
|
|
$vehicles = VehicleNewModel::where(['brand'=>$item['id']])->limit(8)->get();
|
|
|
|
|
foreach ($vehicles as &$v) {
|
|
|
|
|
$v['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($v['cover']);
|
|
|
|
|
}
|
|
|
|
|
$result['vehicle'] = $vehicles;
|
|
|
|
|
}
|
2025-05-23 16:02:35 +08:00
|
|
|
return $result;
|
|
|
|
|
}
|
2025-05-14 13:19:23 +08:00
|
|
|
return $item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 参数预处理 (根据场景处理)
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param string $scene
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareParams(Request $request, string $scene): array
|
|
|
|
|
{
|
|
|
|
|
$params = parent::prepareParams($request, $scene);
|
|
|
|
|
|
2025-07-28 14:27:22 +08:00
|
|
|
if ($scene === 'default' || $scene === 'letter' || $scene === 'hot') {
|
2025-05-23 16:02:35 +08:00
|
|
|
$params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => '1'];
|
|
|
|
|
if(!empty($params['letter'])){
|
|
|
|
|
$params['filters'][] = ['field' => 'letter', 'operator' => '=', 'value' => $params['letter']];
|
|
|
|
|
}
|
2025-07-28 14:27:22 +08:00
|
|
|
if(isset($params['is_hot']) && $params['is_hot'] == 1 || $scene === 'hot'){
|
2025-05-23 16:02:35 +08:00
|
|
|
$params['filters'][] = ['field' => 'is_hot', 'operator' => '=', 'value' => 1];
|
|
|
|
|
}
|
2025-05-14 13:19:23 +08:00
|
|
|
}
|
2025-05-23 16:49:21 +08:00
|
|
|
|
2025-05-14 13:19:23 +08:00
|
|
|
return $params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分页
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
* @return Response 响应对象
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request, string $scene = 'default'): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return parent::index($request, $scene);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-23 16:02:35 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分页
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
* @return Response 响应对象
|
|
|
|
|
*/
|
|
|
|
|
public function letterAll(Request $request, string $scene = 'letter'): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-06-03 13:06:13 +08:00
|
|
|
return parent::all($request, $scene);
|
2025-05-23 16:02:35 +08:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 14:27:22 +08:00
|
|
|
/**
|
|
|
|
|
* 分页
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
* @return Response 响应对象
|
|
|
|
|
*/
|
|
|
|
|
public function hot(Request $request, string $scene = 'hot'): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return parent::all($request, $scene);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-23 16:49:21 +08:00
|
|
|
/**
|
|
|
|
|
* 详情
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
* @return Response 响应对象
|
|
|
|
|
*/
|
|
|
|
|
public function detail(Request $request, int $id, string $scene = 'show'): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$request->setGet(['id'=>$id]);
|
|
|
|
|
return parent::one($request, $scene);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-14 13:19:23 +08:00
|
|
|
}
|