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' => [
]
];
}