This commit is contained in:
2025-06-27 11:19:40 +08:00
parent f52023b842
commit 33340cd93d
9 changed files with 718 additions and 9 deletions
@@ -0,0 +1,116 @@
<?php
namespace app\console\controller;
use support\Request;
use support\Response;
use app\console\model\ArticleModel;
use app\console\service\ArticleService;
use app\console\validate\ArticleValidator;
use app\mzl\common\controller\BaseController;
class ArticleController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = ArticleValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new ArticleService(new ArticleModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default' || $scene === 'show') {
$item['publish_time'] = date('Y-m-d H:i:s', $item['publish_time']);
}
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'] = [];
$params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => 1];
$params['filters'][] = ['field' => 'locale', 'operator' => '=', 'value' => locale()];
}
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 detail(Request $request, int $id, string $scene = 'show'): Response
{
try {
$request->setGet(['id'=>$id]);
return parent::one($request, $scene);
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage());
}
}
public function add(Request $request): Response
{
try {
$request->setPost('user_id', $request->user['id']);
return $this->getService()->add($request->all())
? $this->successResponse()
: $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());
}
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace app\console\model;
use support\Model;
/**
* ArticleModel
*/
class ArticleModel 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_article';
/**
* 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', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at', 'deleted_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+108
View File
@@ -0,0 +1,108 @@
<?php
namespace app\console\service;
use app\console\model\ArticleModel;
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 ArticleService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return ArticleModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
'relations' => [
// 'translation' => ['model_id', 'name'],
// 'brand.translation' => ['brand_id', 'name']
],
'sorts' => [
['field' => 'is_top', 'direction' => 'desc'],
['field' => 'sort', 'direction' => 'desc'],
['field' => 'publish_time', 'direction' => 'desc'],
],
'pagination' => [
'page' => $params['page'] ?? 1,
'per_page' => $params['per_page'] ?? 10,
]
];
},
'show' => function ($params) {
return [
'filters' => [
['field' => 'status', 'operator' => '=', 'value' => '1'],
['field' => 'id', 'operator' => '=', 'value' => $params['id']]
],
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
'relations' => [
// 'translation' => ['model_id', 'name']
],
];
}
];
}
public function add(array $params)
{
$params['locale'] = locale();
if(!empty($params['publish_time'])){
$params['publish_time'] = strtotime($params['publish_time']);
}else{
$params['publish_time'] = time();
}
Db::beginTransaction();
try {
$info = $this->create($params);
if (!$info) {
throw new BusinessException('提交失败');
}
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
public function edit(array $params, int $id)
{
$params['locale'] = locale();
$params['publish_time'] = strtotime($params['publish_time']);
Db::beginTransaction();
try {
$info = $this->update($id, $params);
if (!$info) {
throw new BusinessException('提交失败');
}
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace app\console\validate;
use think\Validate;
class ArticleValidator extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}