Merge branch 'feature/article' into develop

This commit is contained in:
2025-07-08 14:18:47 +08:00
21 changed files with 1039 additions and 3 deletions
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace app\apis\controller;
use app\apis\model\AdModel;
use support\Request;
use support\Response;
use app\apis\model\AdpModel;
use app\apis\service\AdpService;
use app\apis\validate\AdpValidator;
use app\mzl\common\controller\BaseController;
class AdpController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdpValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdpService(new AdpModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default') {
$item['title'] = isset($item['translation']) ? $item['translation']['title'] : '';
unset($item['translation']);
$ad = AdModel::with('translation')->where(['adp_id' => $item['id'], 'status' => 1])->limit(8)->get();
foreach ($ad as $k => $v) {
$v['alt'] = isset($v['translation']) ? $v['translation']['alt'] : '';
$v['title'] = isset($v['translation']) ? $v['translation']['title'] : '';
unset($v['translation']);
$item['ad'][$k] = $ad;
}
}
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\AdpTranslationModel;
use app\apis\service\AdpTranslationService;
use app\apis\validate\AdpTranslationValidator;
use app\mzl\common\controller\BaseController;
class AdpTranslationController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdpTranslationValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdpTranslationService(new AdpTranslationModel());
}
/**
* 额外字段处理 (根据场景处理)
* @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());
}
}
}
+80
View File
@@ -0,0 +1,80 @@
<?php
namespace app\apis\model;
use support\Model;
/**
* AdpModel
*/
class AdpModel 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_adp';
/**
* 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', 'status', 'sort', 'creater', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
/**
* 单条翻译
*/
public function translation()
{
return $this->hasOne(AdpTranslationModel::class, 'adp_id')->where('locale', locale());
}
/**
* 多条翻译
*/
public function translations()
{
return $this->hasMany(AdpTranslationModel::class, 'adp_id');
}
/**
* 广告
*/
public function ad()
{
return $this->hasMany(AdModel::class, 'adp_id','id');
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace app\apis\model;
use support\Model;
/**
* AdpTranslationModel
*/
class AdpTranslationModel 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_adp_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', 'title', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace app\apis\service;
use app\apis\model\AdpModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AdpService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdpModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['adp_id', 'title'],
'ad' => ['id', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'ad.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'] ?? 10,
]
];
},
];
}
}
@@ -0,0 +1,59 @@
<?php
namespace app\apis\service;
use app\apis\model\AdpTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AdpTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdpTranslationModel::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 AdpTranslationValidator 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 AdpValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+1
View File
@@ -62,6 +62,7 @@ class AdController extends BaseController
if ($scene === 'default') {
$params['filters'] = [];
$params['filters'][] = ['field' => 'adp_id', 'operator' => '=', 'value' => $params['adp_id']];
}
return $params;
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace app\console\controller;
use support\Request;
use support\Response;
use app\console\model\AdpModel;
use app\console\service\AdpService;
use app\console\validate\AdpValidator;
use app\mzl\common\controller\BaseController;
class AdpController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdpValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdpService(new AdpModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default') {
$item['title'] = isset($item['translation']) ? $item['translation']['title'] : '';
unset($item['translation']);
}
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 {
$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);
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\AdpTranslationModel;
use app\console\service\AdpTranslationService;
use app\console\validate\AdpTranslationValidator;
use app\mzl\common\controller\BaseController;
class AdpTranslationController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = AdpTranslationValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new AdpTranslationService(new AdpTranslationModel());
}
/**
* 额外字段处理 (根据场景处理)
* @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());
}
}
}
+1 -1
View File
@@ -47,7 +47,7 @@ class AdModel extends Model
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'];
protected $fillable = ['id', 'adp_id', 'image', 'href', 'status', 'sort', 'creater', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace app\console\model;
use support\Model;
/**
* AdpModel
*/
class AdpModel 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_adp';
/**
* 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', 'status', 'sort', 'creater', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
/**
* 单条翻译
*/
public function translation()
{
return $this->hasOne(AdpTranslationModel::class, 'adp_id')->where('locale', locale());
}
/**
* 多条翻译
*/
public function translations()
{
return $this->hasMany(AdpTranslationModel::class, 'adp_id');
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace app\console\model;
use support\Model;
/**
* AdpTranslationModel
*/
class AdpTranslationModel 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_adp_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', 'adp_id', 'locale', 'title', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+1 -1
View File
@@ -33,7 +33,7 @@ class AdService extends BaseService
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'fields' => ['id', 'adp_id', 'image', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['ad_id', 'alt', 'title'],
],
+120
View File
@@ -0,0 +1,120 @@
<?php
namespace app\console\service;
use app\console\model\AdpModel;
use app\console\model\AdpTranslationModel;
use app\console\model\AdTranslationModel;
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 AdpService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdpModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['adp_id', 'title'],
],
'sorts' => [
['field' => 'sort', 'direction' => 'desc'],
['field' => 'created_at', 'direction' => 'desc'],
],
'pagination' => [
'page' => $params['page'] ?? 1,
'per_page' => $params['per_page'] ?? 10,
]
];
},
];
}
protected array $translatable = [
'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(
'adp',
$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 表
AdpTranslationModel::updateOrCreate(
[
'adp_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\AdpTranslationModel;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AdpTranslationService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdpTranslationModel::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 AdpTranslationValidator 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 AdpValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -63,6 +63,10 @@ return [
'translation_model' => \app\console\model\VehicleNewAlbumTranslationModel::class,
'foreign_key' => 'album_id',
],
'adp' => [
'translation_model' => \app\console\model\AdpTranslationModel::class,
'foreign_key' => 'adp_id',
],
'ad' => [
'translation_model' => \app\console\model\AdTranslationModel::class,
'foreign_key' => 'ad_id',
+7 -1
View File
@@ -61,7 +61,7 @@ Route::group('/apis', function() {
Route::any('/article/home', [\app\apis\controller\ArticleController::class, 'home']);
//广告管理
Route::any('/ad/list', [\app\apis\controller\AdController::class, 'index']);
Route::any('/adp/list', [\app\apis\controller\AdpController::class, 'index']);
//图片代理
Route::any('/upload/image/proxy', [\app\mzl\modules\upload\controller\UploadController::class, 'proxy']);
@@ -270,6 +270,12 @@ Route::group('/console', function() {
Route::any('/comment/{id}/delete', [\app\console\controller\CommentController::class, 'destroy']);
Route::any('/comment/{id}/detail', [\app\console\controller\CommentController::class, 'detail']);
//广告位管理
Route::any('/adp/list', [\app\console\controller\AdpController::class, 'index']);
Route::any('/adp/add', [\app\console\controller\AdpController::class, 'add']);
Route::any('/adp/{id}/update', [\app\console\controller\AdpController::class, 'edit']);
Route::any('/adp/{id}/delete', [\app\console\controller\AdpController::class, 'destroy']);
//广告管理
Route::any('/ad/list', [\app\console\controller\AdController::class, 'index']);
Route::any('/ad/add', [\app\console\controller\AdController::class, 'add']);