diff --git a/app/applet/controller/LocationController.php b/app/applet/controller/LocationController.php index ae7d830..ca616d4 100644 --- a/app/applet/controller/LocationController.php +++ b/app/applet/controller/LocationController.php @@ -65,7 +65,9 @@ class LocationController extends BaseController $params = parent::prepareParams($request, $scene); if ($scene === 'default') { - + $params['filters'] = []; + $params['filters'][] = ['field' => 'user_id', 'operator' => '=', 'value' => $request->input('user_id')]; +// $params['filters'][] = ['field' => 'user_id', 'operator' => '=', 'value' => 111]; } return $params; diff --git a/app/console/controller/BrandBannerController.php b/app/console/controller/BrandBannerController.php new file mode 100644 index 0000000..fb7a051 --- /dev/null +++ b/app/console/controller/BrandBannerController.php @@ -0,0 +1,119 @@ + $item['id'], + 'status' => $item['status'], + 'sort' => $item['sort'], + 'brand_id' => $item['brand_id'], + 'title' => isset($item['translation']) ? $item['translation']['title'] : '', + 'content' => isset($item['translation']) ? $item['translation']['content'] : '', + 'created_at' => date('Y-m-d H:i:s', $item['created_at']), + 'updated_at' => date('Y-m-d H:i:s', $item['updated_at']) + ]; + } + + if ($scene === 'preset') { + return [ + 'id' => $item['id'], + 'name' => isset($item['translation']) ? $item['translation']['name'] : '' + ]; + } + + return $item; + } + + /** + * 参数预处理 (根据场景处理) + * @param Request $request + * @param string $scene + * @return array + */ + protected function prepareParams(Request $request, string $scene): array + { + $params = parent::prepareParams($request, $scene); + + if ($scene === 'default') { + + } + + 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().$th->getFile().$th->getLine()); + } + } + + public function add(Request $request): Response + { + try { + $id = $this->getService()->add($request->all()); + return $id ? $this->successResponse(['id' => $id]) + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } + + public function edit(Request $request, int $id): Response + { + try { + return $this->getService()->edit($request->all(), $id) + ? $this->successResponse() + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/console/controller/BrandController.php b/app/console/controller/BrandController.php index c6c5307..3f1b1ca 100644 --- a/app/console/controller/BrandController.php +++ b/app/console/controller/BrandController.php @@ -41,14 +41,28 @@ class BrandController extends BaseController { if ($scene === 'default') { - return [ + $result = [ 'id' => $item['id'], 'status' => $item['status'], 'sort' => $item['sort'], + 'toppic' => $item['toppic'], + 'descpic' => $item['descpic'], + 'logo' => $item['logo'], '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']) + 'updated_at' => date('Y-m-d H:i:s', $item['updated_at']), ]; + foreach ($item['banner'] as $k => $v) { + $result['banner'][$k] = $v; + } + foreach ($item['trait'] as $k => $v) { + $result['trait'][$k] = $v; + } + return $result; } if ($scene === 'preset') { @@ -86,6 +100,9 @@ class BrandController extends BaseController */ public function index(Request $request, string $scene = 'default'): Response { +// $Brand = BrandModel::with('banner')->with('trait')->where('id',314)->first()->toArray(); +// var_dump($Brand); +// return json($Brand); try { return parent::index($request, $scene); } catch (\Throwable $th) { @@ -108,8 +125,8 @@ class BrandController extends BaseController public function add(Request $request): Response { try { - return $this->getService()->add($request->all()) - ? $this->successResponse() + $id = $this->getService()->add($request->all()); + return $id ? $this->successResponse(['id' => $id]) : $this->errorResponse(); } catch (\Throwable $th) { return $this->errorResponse($th->getMessage()); @@ -119,11 +136,12 @@ class BrandController extends BaseController public function edit(Request $request, int $id): Response { try { + $request->setPost('id',$id); return $this->getService()->edit($request->all(), $id) ? $this->successResponse() : $this->errorResponse(); } catch (\Throwable $th) { - return $this->errorResponse($th->getMessage()); + return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine()); } } } \ No newline at end of file diff --git a/app/console/controller/BrandTraitController.php b/app/console/controller/BrandTraitController.php new file mode 100644 index 0000000..8f3cd5a --- /dev/null +++ b/app/console/controller/BrandTraitController.php @@ -0,0 +1,112 @@ + $item['id'], + 'status' => $item['status'], + 'sort' => $item['sort'], + 'brand_id' => $item['brand_id'], + 'title' => isset($item['translation']) ? $item['translation']['title'] : '', + 'content' => isset($item['translation']) ? $item['translation']['content'] : '', + 'created_at' => date('Y-m-d H:i:s', $item['created_at']), + 'updated_at' => date('Y-m-d H:i:s', $item['updated_at']) + ]; + } + + return $item; + } + + /** + * 参数预处理 (根据场景处理) + * @param Request $request + * @param string $scene + * @return array + */ + protected function prepareParams(Request $request, string $scene): array + { + $params = parent::prepareParams($request, $scene); + + if ($scene === 'default') { + + } + + 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().$th->getFile().$th->getLine()); + } + } + + public function add(Request $request): Response + { + try { + $id = $this->getService()->add($request->all()); + return $id ? $this->successResponse(['id' => $id]) + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } + + public function edit(Request $request, int $id): Response + { + try { + return $this->getService()->edit($request->all(), $id) + ? $this->successResponse() + : $this->errorResponse(); + } catch (\Throwable $th) { + return $this->errorResponse($th->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/console/model/BrandBannerModel.php b/app/console/model/BrandBannerModel.php new file mode 100644 index 0000000..ac59ff7 --- /dev/null +++ b/app/console/model/BrandBannerModel.php @@ -0,0 +1,83 @@ +belongsTo(BrandModel::class, 'brand_id'); + } + + /** + * 单条翻译 + */ + public function translation() + { + return $this->hasOne(BrandBannerTranslationModel::class, 'brand_id')->where('locale', locale()); + } + + /** + * 多条翻译 + */ + public function translations() + { + return $this->hasMany(VehicleDataTranslationModel::class, 'brand_id'); + } + + + const TRANS_FIELDS = [ + 'name', 'condition_desc' + ]; +} \ No newline at end of file diff --git a/app/console/model/BrandBannerTranslationModel.php b/app/console/model/BrandBannerTranslationModel.php new file mode 100644 index 0000000..889e01f --- /dev/null +++ b/app/console/model/BrandBannerTranslationModel.php @@ -0,0 +1,68 @@ +belongsTo(BrandModel::class, 'brand_id'); + } + + public function banner() + { + return $this->belongsTo(BrandBannerModel::class, 'banner_id'); + } + +} \ No newline at end of file diff --git a/app/console/model/BrandModel.php b/app/console/model/BrandModel.php index be69cae..a6361b5 100644 --- a/app/console/model/BrandModel.php +++ b/app/console/model/BrandModel.php @@ -47,7 +47,7 @@ class BrandModel extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'status', 'sort', 'created_at', 'updated_at']; + protected $fillable = ['id', 'status', 'sort', 'toppic', 'descpic', 'logo', 'letter', 'created_at', 'updated_at']; /** * 不允许自动填充模型字段 @@ -64,4 +64,13 @@ class BrandModel extends Model { return $this->hasMany(BrandTranslationModel::class, 'brand_id'); } + + public function banner() + { + return $this->hasMany(BrandBannerModel::class, 'brand_id','id'); + } + public function trait() + { + return $this->hasMany(BrandTraitModel::class, 'brand_id','id'); + } } \ No newline at end of file diff --git a/app/console/model/BrandTraitModel.php b/app/console/model/BrandTraitModel.php new file mode 100644 index 0000000..52a505a --- /dev/null +++ b/app/console/model/BrandTraitModel.php @@ -0,0 +1,84 @@ +belongsTo(BrandModel::class, 'brand_id'); + } + + /** + * 单条翻译 + */ + public function translation() + { + return $this->hasOne(BrandTraitTranslationModel::class, 'trait_id')->where('locale', locale()); + } + + /** + * 多条翻译 + */ + public function translations() + { + return $this->hasMany(BrandTraitTranslationModel::class, 'trait_id'); + } + + + const TRANS_FIELDS = [ + 'name', 'condition_desc' + ]; +} \ No newline at end of file diff --git a/app/console/model/BrandTraitTranslationModel.php b/app/console/model/BrandTraitTranslationModel.php new file mode 100644 index 0000000..53b3201 --- /dev/null +++ b/app/console/model/BrandTraitTranslationModel.php @@ -0,0 +1,67 @@ +belongsTo(BrandModel::class, 'brand_id'); + } + + public function brandTrait() + { + return $this->belongsTo(BrandTraitModel::class, 'trait_id'); + } +} \ No newline at end of file diff --git a/app/console/model/BrandTranslationModel.php b/app/console/model/BrandTranslationModel.php index aff14ad..62b0abe 100644 --- a/app/console/model/BrandTranslationModel.php +++ b/app/console/model/BrandTranslationModel.php @@ -47,7 +47,7 @@ class BrandTranslationModel extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'brand_id', 'locale', 'name']; + protected $fillable = ['id', 'brand_id', 'locale', 'name', 'desc','toppic_alt','descpic_alt','logo_alt']; /** * 不允许自动填充模型字段 diff --git a/app/console/service/BrandBannerService.php b/app/console/service/BrandBannerService.php new file mode 100644 index 0000000..128fec0 --- /dev/null +++ b/app/console/service/BrandBannerService.php @@ -0,0 +1,120 @@ + function ($params) { + return [ + 'filters' => [ + ['field' => 'status', 'operator' => '=', 'value' => '1'], + ['field' => 'brand_id', 'operator' => '=', 'value' => $params['brand_id']], + ], + 'fields' => ['id', 'status', 'sort', 'brand_id', 'pic', 'created_at', 'updated_at'], + 'relations' => [ + 'translation' => ['brand_id','banner_id', 'title', 'content'] + ], + 'sorts' => [ + ['field' => 'id', 'direction' => 'desc'] + ], + 'pagination' => [ + 'page' => $params['page'] ?? 1, + 'per_page' => $params['per_page'] ?? 10, + ] + ]; + }, + ]; + } + + + protected array $translatable = [ + 'alt' + ]; + + public function add(array $params) + { + $sourceLocale = locale(); + $sourceFields = $this->extractSourceFields($params); + $sourceFields['brand_id'] = $params['brand_id']; + if (empty($sourceFields)) { + throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); + }; + + Db::beginTransaction(); + try { + $info = $this->create($params); + if (!$info) { + throw new BusinessException('提交失败'); + } + + // 异步翻译并写入 hs_vehicle_data_translation 表 + (new TranslationService())->queueTranslations( + 'brandBanner', + $info->id, + $sourceLocale, + $sourceFields + ); + + Db::commit(); + return $info->id; + } catch (\Throwable $th) { + Db::rollBack(); + throw $th; + } + } + + + public function edit(array $params, int $id) + { + $sourceLocale = locale(); + $sourceFields = $this->extractSourceFields($params); + if (empty($sourceFields)) { + throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); + }; + + Db::beginTransaction(); + try { + $info = $this->update($id, $params); + if (!$info) { + throw new BusinessException('提交失败'); + } + + // 异步翻译并写入 hs_vehicle_data_translation 表 + (new TranslationService())->queueTranslations( + 'brandBanner', + $id, + $sourceLocale, + $sourceFields + ); + + Db::commit(); + return $id; + } catch (\Throwable $th) { + Db::rollBack(); + throw $th; + } + } +} \ No newline at end of file diff --git a/app/console/service/BrandService.php b/app/console/service/BrandService.php index 7b0a4ff..3533edd 100644 --- a/app/console/service/BrandService.php +++ b/app/console/service/BrandService.php @@ -2,7 +2,9 @@ namespace app\console\service; +use app\console\model\BrandBannerModel; use app\console\model\BrandModel; +use app\console\model\BrandTraitModel; use app\mzl\common\services\BaseService; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -33,9 +35,13 @@ class BrandService extends BaseService 'filters' => [ ['field' => 'status', 'operator' => '=', 'value' => '1'] ], - 'fields' => ['id', 'status', 'sort', 'created_at', 'updated_at'], + 'fields' => ['id', 'status', 'sort', 'toppic', 'descpic', 'logo', 'letter', 'created_at', 'updated_at'], 'relations' => [ - 'translation' => ['brand_id', 'name'] + 'translation' => ['brand_id', 'name', 'desc','toppic_alt','descpic_alt','logo_alt'], + 'banner' => ['id', 'pic', 'sort',], + 'banner.translation' => ['id', 'alt',], + 'trait' => ['id', 'sort',], + 'trait.translation' => ['id', 'title', 'content',], ], 'sorts' => [ ['field' => 'id', 'direction' => 'desc'] @@ -65,12 +71,20 @@ class BrandService extends BaseService protected array $translatable = [ - 'name' + 'name', 'desc','toppic_alt','descpic_alt','logo_alt' ]; public function add(array $params) { $sourceLocale = locale(); + + $BrandBannerService = new BrandBannerService(new BrandBannerModel()); + $BrandTraitService = new BrandTraitService(new BrandTraitModel()); + $banners = $params['banners'] ?? null; + unset($params['banners']); + $traits = $params['traits'] ?? null; + unset($params['traits']); + $sourceFields = $this->extractSourceFields($params); if (empty($sourceFields)) { throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); @@ -91,8 +105,44 @@ class BrandService extends BaseService $sourceFields ); + //品牌轮播图 + foreach ($banners as $banner) { + $bannersourceFields = $BrandBannerService->extractSourceFields($banner); + $bannersourceFields['brand_id'] = $info->id; + $banner['brand_id'] = $info->id; + $banner = $BrandBannerService->create($banner); + if (!$banner) { + throw new BusinessException('提交失败'); + } + + (new TranslationService())->queueTranslations( + 'brandBanner', + $banner->id, + $sourceLocale, + $bannersourceFields + ); + } + + //品牌特性 + foreach ($traits as $trait) { + $traitsourceFields = $BrandTraitService->extractSourceFields($trait); + $traitsourceFields['brand_id'] = $info->id; + $trait['brand_id'] = $info->id; + $trait = $BrandTraitService->create($trait); + if (!$trait) { + throw new BusinessException('提交失败'); + } + + (new TranslationService())->queueTranslations( + 'brandTrait', + $trait->id, + $sourceLocale, + $traitsourceFields + ); + } + Db::commit(); - return true; + return $info->id; } catch (\Throwable $th) { Db::rollBack(); throw $th; @@ -102,6 +152,14 @@ class BrandService extends BaseService public function edit(array $params, int $id) { $sourceLocale = locale(); + + $BrandBannerService = new BrandBannerService(new BrandBannerModel()); + $BrandTraitService = new BrandTraitService(new BrandTraitModel()); + $banners = $params['banners'] ?? null; + unset($params['banners']); + $traits = $params['traits'] ?? null; + unset($params['traits']); + $sourceFields = $this->extractSourceFields($params); if (empty($sourceFields)) { throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); @@ -122,6 +180,49 @@ class BrandService extends BaseService $sourceFields ); + //品牌轮播图 + foreach ($banners as $banner) { + $bannersourceFields = $BrandBannerService->extractSourceFields($banner); + $bannersourceFields['brand_id'] = $id; + $bannerId = $banner['id']; + $banner['brand_id'] = $id; + $banner = $BrandBannerService->update($bannerId,$banner); + if (!$banner) { + throw new BusinessException('提交失败'); + } + + (new TranslationService())->queueTranslations( + 'brandBanner', + $bannerId, + $sourceLocale, + $bannersourceFields + ); + } + $ids = array_column($banners,'id'); + BrandBannerModel::whereNotIn('id', $ids)->where('brand_id',$id)->delete(); + + //品牌特性 + foreach ($traits as $trait) { + $traitsourceFields = $BrandTraitService->extractSourceFields($trait); + $traitsourceFields['brand_id'] = $id; + $traitId = $trait['id']; + $trait['brand_id'] = $id; + $trait = $BrandTraitService->update($traitId,$trait); + if (!$trait) { + throw new BusinessException('提交失败'); + } + + (new TranslationService())->queueTranslations( + 'brandTrait', + $traitId, + $sourceLocale, + $traitsourceFields + ); + } + $ids = array_column($traits,'id'); + BrandTraitModel::whereNotIn('id', $ids)->where('brand_id',$id)->delete(); + + Db::commit(); return true; } catch (\Throwable $th) { diff --git a/app/console/service/BrandTraitService.php b/app/console/service/BrandTraitService.php new file mode 100644 index 0000000..920c60e --- /dev/null +++ b/app/console/service/BrandTraitService.php @@ -0,0 +1,120 @@ + function ($params) { + return [ + 'filters' => [ + ['field' => 'status', 'operator' => '=', 'value' => '1'], + ['field' => 'brand_id', 'operator' => '=', 'value' => $params['brand_id']], + ], + 'fields' => ['id', 'status', 'sort', 'brand_id', 'created_at', 'updated_at'], + 'relations' => [ + 'translation' => ['brand_id','trait_id', 'title', 'content'] + ], + 'sorts' => [ + ['field' => 'id', 'direction' => 'desc'] + ], + 'pagination' => [ + 'page' => $params['page'] ?? 1, + 'per_page' => $params['per_page'] ?? 10, + ] + ]; + }, + ]; + } + + + protected array $translatable = [ + 'title', 'content' + ]; + + public function add(array $params) + { + $sourceLocale = locale(); + $sourceFields = $this->extractSourceFields($params); + $sourceFields['brand_id'] = $params['brand_id']; + if (empty($sourceFields)) { + throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); + }; + + Db::beginTransaction(); + try { + $info = $this->create($params); + if (!$info) { + throw new BusinessException('提交失败'); + } + + // 异步翻译并写入 hs_vehicle_data_translation 表 + (new TranslationService())->queueTranslations( + 'brandTrait', + $info->id, + $sourceLocale, + $sourceFields + ); + + Db::commit(); + return $info->id; + } catch (\Throwable $th) { + Db::rollBack(); + throw $th; + } + } + + + public function edit(array $params, int $id) + { + $sourceLocale = locale(); + $sourceFields = $this->extractSourceFields($params); + if (empty($sourceFields)) { + throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段"); + }; + + Db::beginTransaction(); + try { + $info = $this->update($id, $params); + if (!$info) { + throw new BusinessException('提交失败'); + } + + // 异步翻译并写入 hs_vehicle_data_translation 表 + (new TranslationService())->queueTranslations( + 'brandTrait', + $id, + $sourceLocale, + $sourceFields + ); + + Db::commit(); + return $id; + } catch (\Throwable $th) { + Db::rollBack(); + throw $th; + } + } +} \ No newline at end of file diff --git a/app/console/validate/BrandBannerValidator.php b/app/console/validate/BrandBannerValidator.php new file mode 100644 index 0000000..c2022f2 --- /dev/null +++ b/app/console/validate/BrandBannerValidator.php @@ -0,0 +1,30 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/console/validate/BrandTraitValidator.php b/app/console/validate/BrandTraitValidator.php new file mode 100644 index 0000000..bb92b1d --- /dev/null +++ b/app/console/validate/BrandTraitValidator.php @@ -0,0 +1,30 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/mzl/modules/translation/config/trans.php b/app/mzl/modules/translation/config/trans.php index 01b353a..7c55644 100644 --- a/app/mzl/modules/translation/config/trans.php +++ b/app/mzl/modules/translation/config/trans.php @@ -23,6 +23,14 @@ return [ 'translation_model' => \app\console\model\BrandTranslationModel::class, 'foreign_key' => 'brand_id', ], + 'brandBanner' => [ + 'translation_model' => \app\console\model\BrandBannerTranslationModel::class, + 'foreign_key' => 'banner_id', + ], + 'brandTrait' => [ + 'translation_model' => \app\console\model\BrandTraitTranslationModel::class, + 'foreign_key' => 'trait_id', + ], 'agreement' => [ 'translation_model' => \app\console\model\AgreementTranslationModel::class, 'foreign_key' => 'agreement_id', diff --git a/config/route.php b/config/route.php index c88c564..df425a2 100644 --- a/config/route.php +++ b/config/route.php @@ -40,6 +40,8 @@ Route::group('/apis', function() { Route::any('/vehicle/random', [\app\apis\controller\VehicleDataController::class, 'random']); // 隐私协议 Route::any('/agt/privacy', [\app\apis\controller\AgreementController::class, 'one']); + // 品牌按字母查询 + Route::any('/brand/letterAll', [\app\apis\controller\BrandController::class, 'letterAll']); }); // ====== ====== ====== 网站 ====== ====== ====== @@ -124,6 +126,18 @@ Route::group('/console', function() { Route::any('/brand/{id}/detail', [\app\console\controller\BrandController::class, 'show']); Route::any('/brand/preset', [\app\console\controller\BrandController::class, 'presets']); + // 品牌轮播 + Route::any('/brandbanner/add', [\app\console\controller\BrandBannerController::class, 'add']); + Route::any('/brandbanner/update/{id}', [\app\console\controller\BrandBannerController::class, 'edit']); + Route::any('/brandbanner/list', [\app\console\controller\BrandBannerController::class, 'index']); + Route::any('/brandbanner/delete/{id}', [\app\console\controller\BrandBannerController::class, 'destroy']); + + // 品牌特性 + Route::any('/brandtrait/add', [\app\console\controller\BrandTraitController::class, 'add']); + Route::any('/brandtrait/update/{id}', [\app\console\controller\BrandTraitController::class, 'edit']); + Route::any('/brandtrait/list', [\app\console\controller\BrandTraitController::class, 'index']); + Route::any('/brandtrait/delete/{id}', [\app\console\controller\BrandTraitController::class, 'destroy']); + // 能源管理 Route::any('/energy/list', [\app\console\controller\EnergyController::class, 'index']); Route::any('/energy/add', [\app\console\controller\EnergyController::class, 'add']);