This commit is contained in:
2025-05-14 17:30:37 +08:00
parent fd534aad85
commit 7dc90492b4
8 changed files with 509 additions and 183 deletions
@@ -4,12 +4,12 @@ namespace app\applet\controller;
use support\Request;
use support\Response;
use app\applet\model\DraftsModel;
use app\applet\service\DraftsService;
use app\applet\model\DraftsDataModel;
use app\applet\service\DraftsDataService;
use app\applet\validate\DraftsValidator;
use app\mzl\common\controller\BaseController;
class DraftsController extends BaseController
class DraftsDataController extends BaseController
{
/**
@@ -28,7 +28,7 @@ class DraftsController extends BaseController
*/
protected function getService()
{
return new DraftsService(new DraftsModel());
return new DraftsDataService(new DraftsDataModel());
}
/**
@@ -78,4 +78,42 @@ class DraftsController extends BaseController
return $this->errorResponse($th->getMessage());
}
}
/**
* 新增草稿
* @param Request $request
* @return Response
*/
public function add(Request $request): Response
{
try {
$data = $this->injectAdd($request->all());
return $this->getService()->add($data)
? $this->successResponse()
: $this->errorResponse();
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage());
}
}
public function show(Request $request, int $id, string $scene = 'show'): Response
{
try {
return parent::show($request, $id, $scene);
} 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());
}
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace app\applet\model;
use support\Model;
/**
* VehicleAlbumModel
*/
class DraftsAlbumModel extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'hs_drafts_album';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
/**
* 模型的数据字段的保存格式。
*
* @var string
*/
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'drafts_data_id', 'front_45_degree', 'front_left_45_degree', 'front_left_headlight', 'front_view', 'lower_front', 'wheel_hub', 'front_right_seat', 'right_lower_side', 'rear_right_taillight', 'rear_right_45_degree', 'lower_rear', 'trunk', 'left_lower_side', 'rear_seats', 'center_console', 'control_panel', 'gear_shift', 'car_key', 'steering_wheel', 'dashboard', 'odometer_reading', 'front_left_door_panel', 'front_left_door_function_area', 'front_left_seat', 'interior_ceiling', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
const TRANS_FIELDS = [
'name', 'condition_desc'
];
/**
* 相册字段
*/
const VEHICLE_ALBUM_FIELDS = [
'front_45_degree',
'front_left_45_degree',
'front_left_headlight',
'front_view',
'lower_front',
'wheel_hub',
'front_right_seat',
'right_lower_side',
'rear_right_taillight',
'rear_right_45_degree',
'lower_rear',
'trunk',
'left_lower_side',
'rear_seats',
'center_console',
'control_panel',
'gear_shift',
'car_key',
'steering_wheel',
'dashboard',
'odometer_reading',
'front_left_door_panel',
'front_left_door_function_area',
'front_left_seat',
'interior_ceiling'
];
}
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace app\applet\model;
use support\Model;
/**
* DraftsModel
*/
class DraftsDataModel extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'hs_drafts_data';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
/**
* 模型的数据字段的保存格式。
*
* @var string
*/
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'vin', 'user_id', 'cover', 'price', 'brand', 'type', 'energy', 'model', 'mileage', 'vehicle_age', 'vehicle_age', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
const TRANS_FIELDS = [
'name', 'condition_desc'
];
public function user()
{
return $this->belongsTo(UsersModel::class, 'user_id', 'id');
}
public function types()
{
return $this->belongsTo(TypesModel::class, 'type', 'id');
}
public function energy()
{
return $this->belongsTo(EnergyModel::class, 'energy', 'id');
}
public function album()
{
return $this->hasOne(VehicleAlbumModel::class, 'vehicle_data_id', 'id');
}
public function apply()
{
return $this->hasOne(VehicleApplyModel::class, 'vehicle_data_id', 'id');
}
public function brand()
{
return $this->belongsTo(BrandModel::class, 'brand');
}
public function models()
{
return $this->belongsTo(ModelModel::class, 'model');
}
public function location()
{
return $this->belongsTo(LocationModel::class, 'location');
}
/**
* 单条翻译
*/
public function translation()
{
return $this->hasOne(VehicleDataTranslationModel::class, 'vehicle_id')->where('locale', locale());
}
/**
* 多条翻译
*/
public function translations()
{
return $this->hasMany(VehicleDataTranslationModel::class, 'vehicle_id');
}
}
-56
View File
@@ -1,56 +0,0 @@
<?php
namespace app\applet\model;
use support\Model;
/**
* DraftsModel
*/
class DraftsModel extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'hs_drafts';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
/**
* 模型的数据字段的保存格式。
*
* @var string
*/
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'user_id', 'title', 'body', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+193
View File
@@ -0,0 +1,193 @@
<?php
namespace app\applet\service;
use app\applet\model\DraftsAlbumModel;
use app\applet\model\DraftsDataModel;
use app\mzl\common\services\BaseService;
use app\mzl\modules\translation\service\TranslationService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use support\Db;
use support\exception\BusinessException;
class DraftsDataService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return DraftsDataModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// 'default' => function ($params) {
// $keyword = $params['keyword'] ?? '';
// return [
// 'filters' => [
// ['field' => '***', 'operator' => '=', 'value' => '***']
// ],
// 'fields' => ['id'],
// 'relations' => [],
// 'custom' => function(Builder $q) use ($keyword) {
// // 示例
// if ($keyword) {
// $q->where(function($q2) use($keyword) {
// $q2->whereHas('***', function($q3) use($keyword) {
// $q3->where('***', 'like', "%{$keyword}%");
// })->orWhereHas('***', function($q4) use($keyword) {
// $q4->where('***', 'like', "%{$keyword}%");
// });
// });
// }
// },
// 'sorts' => [
// ['field' => 'created_at', 'direction' => 'desc']
// ],
// 'pagination' => [
// 'page' => $params['page'] ?? 1,
// 'per_page' => $params['per_page'] ?? 10,
// ]
// ];
// }
];
}
// 声明需要多语言翻译
protected array $translatable = [
'name',
'condition_desc',
// 'location'
];
/**
* 从$data中提取出源文本字段,并从$data里删除它们
*/
protected function extractSourceFields(array &$data): array
{
$source = [];
foreach ($this->translatable as $field) {
if (array_key_exists($field, $data)) {
$source[$field] = $data[$field];
unset($data[$field]);
}
}
return $source;
}
public function add(array $params)
{
$sourceLocale = locale();
$sourceFields = $this->extractSourceFields($params);
if (empty($sourceFields)) {
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
};
$albums = DraftsAlbumModel::VEHICLE_ALBUM_FIELDS;
foreach ($params as $ko => $vo) {
if (in_array($ko, $albums)) {
$album[$ko] = $vo ?? '';
} else {
if (in_array($ko, DraftsDataModel::TRANS_FIELDS)) {
$trans[$ko] = $vo ?? '';
} else {
$data[$ko] = $vo ?? '';
}
}
}
Db::beginTransaction();
try {
$info = $this->create($data);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_drafts_translation 表
(new TranslationService())->queueTranslations(
'drafts',
$info->id,
$sourceLocale,
$sourceFields
);
$album['drafts_data_id'] = $info->id;
$albumModel = new DraftsAlbumModel();
if (!$albumModel->create($album)) {
throw new BusinessException('保存失败');
}
Db::commit();
return true;
} 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} 语言的翻译字段");
};
$album = [];
$trans = [];
$albums = DraftsAlbumModel::VEHICLE_ALBUM_FIELDS;
foreach ($params as $ko => $vo) {
if (in_array($ko, $albums)) {
$album[$ko] = $vo ?? '';
} else {
if (in_array($ko, DraftsAlbumModel::TRANS_FIELDS)) {
$trans[$ko] = $vo ?? '';
} else {
$data[$ko] = $vo ?? '';
}
}
}
Db::beginTransaction();
try {
$params['info_status'] = 0;
$info = $this->update($id, $params);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_vehicle_data_translation 表
(new TranslationService())->queueTranslations(
'vehicle',
$id,
$sourceLocale,
$sourceFields
);
if ($album) {
// $album['vehicle_data_id'] = $info->id;
$albumModel = new DraftsAlbumModel();
if (!$albumModel->updateOrCreate(
['vehicle_data_id' => $id],
$album
)) {
throw new BusinessException('提交失败');
}
}
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
}
-59
View File
@@ -1,59 +0,0 @@
<?php
namespace app\applet\service;
use app\applet\model\DraftsModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class DraftsService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return DraftsModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// 'default' => function ($params) {
// $keyword = $params['keyword'] ?? '';
// return [
// 'filters' => [
// ['field' => '***', 'operator' => '=', 'value' => '***']
// ],
// 'fields' => ['id'],
// 'relations' => [],
// 'custom' => function(Builder $q) use ($keyword) {
// // 示例
// if ($keyword) {
// $q->where(function($q2) use($keyword) {
// $q2->whereHas('***', function($q3) use($keyword) {
// $q3->where('***', 'like', "%{$keyword}%");
// })->orWhereHas('***', function($q4) use($keyword) {
// $q4->where('***', 'like', "%{$keyword}%");
// });
// });
// }
// },
// 'sorts' => [
// ['field' => 'created_at', 'direction' => 'desc']
// ],
// 'pagination' => [
// 'page' => $params['page'] ?? 1,
// 'per_page' => $params['per_page'] ?? 10,
// ]
// ];
// }
];
}
}
+61 -60
View File
@@ -2,7 +2,7 @@
namespace app\applet\service;
use app\applet\model\DraftsModel;
use app\applet\model\DraftsDataModel;
use app\applet\model\VehicleApplyModel;
use app\applet\model\VehicleAlbumModel;
use app\applet\model\VehicleDataModel;
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Model;
use support\exception\BusinessException;
use support\Db;
use app\mzl\modules\translation\service\TranslationService;
use support\Request;
class VehicleDataService extends BaseService
{
@@ -36,7 +37,7 @@ class VehicleDataService extends BaseService
'filters' => $params['filters'],
'fields' => [
'id','vin','price','cover',
'type','energy','model','mileage','vehicle_age',
'type','energy','model','mileage','vehicle_age','info_status',
'views','is_hot','created_at','updated_at', 'brand', 'model', 'user_id', 'location'
],
'relations' => [
@@ -125,69 +126,69 @@ class VehicleDataService extends BaseService
];
public function add(array $params)
{
$sourceLocale = locale();
$sourceFields = $this->extractSourceFields($params);
if (empty($sourceFields)) {
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
};
{
$sourceLocale = locale();
$sourceFields = $this->extractSourceFields($params);
if (empty($sourceFields)) {
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
};
$albums = VehicleAlbumModel::VEHICLE_ALBUM_FIELDS;
foreach ($params as $ko => $vo) {
if (in_array($ko, $albums)) {
$album[$ko] = $vo ?? '';
$albums = VehicleAlbumModel::VEHICLE_ALBUM_FIELDS;
foreach ($params as $ko => $vo) {
if (in_array($ko, $albums)) {
$album[$ko] = $vo ?? '';
} else {
if (in_array($ko, VehicleDataModel::TRANS_FIELDS)) {
$trans[$ko] = $vo ?? '';
} else {
if (in_array($ko, VehicleDataModel::TRANS_FIELDS)) {
$trans[$ko] = $vo ?? '';
} else {
$data[$ko] = $vo ?? '';
}
$data[$ko] = $vo ?? '';
}
}
Db::beginTransaction();
try {
$info = $this->create($data);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_vehicle_data_translation 表
(new TranslationService())->queueTranslations(
'vehicle',
$info->id,
$sourceLocale,
$sourceFields
);
$album['vehicle_data_id'] = $info->id;
$albumModel = new VehicleAlbumModel();
if (!$albumModel->create($album)) {
throw new BusinessException('提交失败');
}
$applyModel = new VehicleApplyModel();
if (!$applyModel->create([
'vehicle_data_id' => $info->id,
'desc' => '提交审核'
])) {
throw new BusinessException('提交失败');
}
if (isset($params['draft_id']) && $params['draft_id']) {
if (!DraftsModel::where('id', $params['draft_id'])->delete()) {
throw new BusinessException('草稿移除失败');
}
}
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
Db::beginTransaction();
try {
$info = $this->create($data);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_vehicle_data_translation 表
(new TranslationService())->queueTranslations(
'vehicle',
$info->id,
$sourceLocale,
$sourceFields
);
$album['vehicle_data_id'] = $info->id;
$albumModel = new VehicleAlbumModel();
if (!$albumModel->create($album)) {
throw new BusinessException('提交失败');
}
$applyModel = new VehicleApplyModel();
if (!$applyModel->create([
'vehicle_data_id' => $info->id,
'desc' => '提交审核'
])) {
throw new BusinessException('提交失败');
}
if (isset($params['draft_id']) && $params['draft_id']) {
if (!DraftsDataModel::where('id', $params['draft_id'])->delete()) {
throw new BusinessException('草稿移除失败');
}
}
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
public function edit(array $params, int $id)
{
$sourceLocale = locale();
@@ -247,7 +248,7 @@ class VehicleDataService extends BaseService
}
if (isset($params['draft_id']) && $params['draft_id']) {
if (!DraftsModel::where('id', $params['draft_id'])->delete()) {
if (!DraftsDataModel::where('id', $params['draft_id'])->delete()) {
throw new BusinessException('草稿移除失败');
}
}
+6 -4
View File
@@ -62,6 +62,8 @@ Route::group('/applet', function() {
Route::post('/miniapp/phone', [\app\applet\controller\UsersController::class, 'miniappPhoneNumber'])->middleware([
\app\mzl\modules\jwt\middleware\JwtMiddleware::class
]);
// 上传
Route::any('/upload/image', [\app\mzl\modules\upload\controller\UploadController::class, 'uploadImage']);
// // 用户 - 信息
// Route::post('/user/info', [\app\applet\controller\UsersController::class, 'one'])->middleware([
@@ -89,15 +91,15 @@ Route::group('/applet', function() {
// Route::post('vehicle/apply/status', [\app\applet\controller\VehicleApplyController::class, 'getOne']);
// // 草稿 - 添加
// Route::post('draft/add', [\app\applet\controller\DraftsController::class, 'addData']);
Route::post('draft/add', [\app\applet\controller\DraftsDataController::class, 'add']);
// // 草稿 - 更新
// Route::post('draft/update/{draftId}', [\app\applet\controller\DraftsController::class, 'updateData']);
Route::post('draft/update/{draftId}', [\app\applet\controller\DraftsDataController::class, 'edit']);
// // 草稿 - 详情
// Route::get('draft/detail/{draftId}', [\app\applet\controller\DraftsController::class, 'getOne']);
Route::get('draft/detail/{draftId}', [\app\applet\controller\DraftsDataController::class, 'show']);
// // 草稿 - 列表
// Route::get('draft/list', [\app\applet\controller\DraftsController::class, 'pageLists']);
// // 草稿 - 删除
// Route::post('draft/delete', [\app\applet\controller\DraftsController::class, 'deleteData']);
// Route::post('draft/delete', [\app\applet\controller\DraftsDataController::class, 'destroy']);
})->middleware([
\app\mzl\modules\jwt\middleware\JwtMiddleware::class
]);