文章
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user