Merge branch 'feature/article' into develop

This commit is contained in:
2025-07-08 09:59:37 +08:00
18 changed files with 1018 additions and 3 deletions
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace app\apis\controller;
use support\Request;
use support\Response;
use app\apis\model\AdModel;
use app\apis\service\AdService;
use app\apis\validate\AdValidator;
use app\mzl\common\controller\BaseController;
class AdController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdService(new AdModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default') {
}
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') {
$params['filters'] = [];
}
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());
}
}
}
@@ -0,0 +1,81 @@
<?php
namespace app\apis\controller;
use support\Request;
use support\Response;
use app\apis\model\AdTranslationModel;
use app\apis\service\AdTranslationService;
use app\apis\validate\AdTranslationValidator;
use app\mzl\common\controller\BaseController;
class AdTranslationController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdTranslationValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdTranslationService(new AdTranslationModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default') {
}
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());
}
}
}
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace app\apis\model;
use support\Model;
/**
* AdModel
*/
class AdModel 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_ad';
/**
* 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', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
/**
* 单条翻译
*/
public function translation()
{
return $this->hasOne(AdTranslationModel::class, 'ad_id')->where('locale', locale());
}
/**
* 多条翻译
*/
public function translations()
{
return $this->hasMany(AdTranslationModel::class, 'ad_id');
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace app\apis\model;
use support\Model;
/**
* AdTranslationModel
*/
class AdTranslationModel 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_ad_translation';
/**
* 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', 'ad_id', 'locale', 'alt', 'title', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace app\apis\service;
use app\apis\model\AdModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AdService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['ad_id', 'alt', 'title'],
],
'sorts' => [
['field' => 'sort', 'direction' => 'desc'],
['field' => 'created_at', 'direction' => 'desc'],
],
'pagination' => [
'page' => $params['page'] ?? 1,
'per_page' => $params['per_page'] ?? 8,
]
];
},
];
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace app\apis\service;
use app\apis\model\AdTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AdTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdTranslationModel::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,
// ]
// ];
// }
];
}
}
@@ -0,0 +1,29 @@
<?php
namespace app\apis\validate;
use think\Validate;
class AdTranslationValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace app\apis\validate;
use think\Validate;
class AdValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+106
View File
@@ -0,0 +1,106 @@
<?php
namespace app\console\controller;
use support\Request;
use support\Response;
use app\console\model\AdModel;
use app\console\service\AdService;
use app\console\validate\AdValidator;
use app\mzl\common\controller\BaseController;
class AdController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdService(new AdModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default') {
}
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') {
$params['filters'] = [];
}
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());
}
}
public function add(Request $request): Response
{
try {
$request->setPost('creater', $request->user['id']);
$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 {
$request->setPost('id',$id);
$request->setPost('creater', $request->user['id']);
return $this->getService()->edit($request->all(), $id)
? $this->successResponse()
: $this->errorResponse();
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine());
}
}
}
@@ -0,0 +1,81 @@
<?php
namespace app\console\controller;
use support\Request;
use support\Response;
use app\console\model\AdTranslationModel;
use app\console\service\AdTranslationService;
use app\console\validate\AdTranslationValidator;
use app\mzl\common\controller\BaseController;
class AdTranslationController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdTranslationValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdTranslationService(new AdTranslationModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default') {
}
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());
}
}
}
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace app\console\model;
use support\Model;
/**
* AdModel
*/
class AdModel 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_ad';
/**
* 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', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
/**
* 单条翻译
*/
public function translation()
{
return $this->hasOne(AdTranslationModel::class, 'ad_id')->where('locale', locale());
}
/**
* 多条翻译
*/
public function translations()
{
return $this->hasMany(AdTranslationModel::class, 'ad_id');
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace app\console\model;
use support\Model;
/**
* AdTranslationModel
*/
class AdTranslationModel 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_ad_translation';
/**
* 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', 'ad_id', 'locale', 'alt', 'title', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+120
View File
@@ -0,0 +1,120 @@
<?php
namespace app\console\service;
use app\console\model\AdModel;
use app\console\model\AdTranslationModel;
use app\console\model\ModelTranslationModel;
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 AdService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['ad_id', 'alt', 'title'],
],
'sorts' => [
['field' => 'sort', 'direction' => 'desc'],
['field' => 'created_at', 'direction' => 'desc'],
],
'pagination' => [
'page' => $params['page'] ?? 1,
'per_page' => $params['per_page'] ?? 8,
]
];
},
];
}
protected array $translatable = [
'alt', 'title'
];
public function add(array $params)
{
$sourceLocale = locale();
$sourceFields = $this->extractSourceFields($params);
if (empty($sourceFields)) {
throw new \InvalidArgumentException("请传递至少一条 {$sourceLocale} 语言的翻译字段");
};
Db::beginTransaction();
try {
$info = $this->create($params);
if (!$info) {
throw new BusinessException('提交失败');
}
// 异步翻译并写入 hs_ad_translation 表
(new TranslationService())->queueTranslations(
'ad',
$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_ad_translation 表
AdTranslationModel::updateOrCreate(
[
'ad_id' => $id,
'locale' => $sourceLocale,
],
$sourceFields
);
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
}
@@ -0,0 +1,59 @@
<?php
namespace app\console\service;
use app\console\model\AdTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AdTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdTranslationModel::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,
// ]
// ];
// }
];
}
}
@@ -0,0 +1,29 @@
<?php
namespace app\console\validate;
use think\Validate;
class AdTranslationValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace app\console\validate;
use think\Validate;
class AdValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+3 -3
View File
@@ -63,9 +63,9 @@ return [
'translation_model' => \app\console\model\VehicleNewAlbumTranslationModel::class, 'translation_model' => \app\console\model\VehicleNewAlbumTranslationModel::class,
'foreign_key' => 'album_id', 'foreign_key' => 'album_id',
], ],
'vehicleNewContent' => [ 'ad' => [
'translation_model' => \app\console\model\VehicleNewContentTranslationModel::class, 'translation_model' => \app\console\model\AdTranslationModel::class,
'foreign_key' => 'content_id', 'foreign_key' => 'ad_id',
], ],
'vehicleNewAttributes' => [ 'vehicleNewAttributes' => [
'translation_model' => \app\console\model\VehicleNewAttributesTranslationModel::class, 'translation_model' => \app\console\model\VehicleNewAttributesTranslationModel::class,
+9
View File
@@ -60,6 +60,9 @@ Route::group('/apis', function() {
Route::any('/article/{id}/detail', [\app\apis\controller\ArticleController::class, 'detail']); Route::any('/article/{id}/detail', [\app\apis\controller\ArticleController::class, 'detail']);
Route::any('/article/home', [\app\apis\controller\ArticleController::class, 'home']); Route::any('/article/home', [\app\apis\controller\ArticleController::class, 'home']);
//广告管理
Route::any('/ad/list', [\app\apis\controller\AdController::class, 'index']);
//图片代理 //图片代理
Route::any('/upload/image/proxy', [\app\mzl\modules\upload\controller\UploadController::class, 'proxy']); Route::any('/upload/image/proxy', [\app\mzl\modules\upload\controller\UploadController::class, 'proxy']);
}); });
@@ -266,6 +269,12 @@ Route::group('/console', function() {
Route::any('/comment/{id}/update', [\app\console\controller\CommentController::class, 'edit']); Route::any('/comment/{id}/update', [\app\console\controller\CommentController::class, 'edit']);
Route::any('/comment/{id}/delete', [\app\console\controller\CommentController::class, 'destroy']); Route::any('/comment/{id}/delete', [\app\console\controller\CommentController::class, 'destroy']);
Route::any('/comment/{id}/detail', [\app\console\controller\CommentController::class, 'detail']); Route::any('/comment/{id}/detail', [\app\console\controller\CommentController::class, 'detail']);
//广告管理
Route::any('/ad/list', [\app\console\controller\AdController::class, 'index']);
Route::any('/ad/add', [\app\console\controller\AdController::class, 'add']);
Route::any('/ad/{id}/update', [\app\console\controller\AdController::class, 'edit']);
Route::any('/ad/{id}/delete', [\app\console\controller\AdController::class, 'destroy']);
}); });
// ====== ====== ====== 后台管理 ====== ====== ====== // ====== ====== ====== 后台管理 ====== ====== ======