2025-06-27 11:19:40 +08:00
|
|
|
<?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'],
|
2025-06-27 17:04:25 +08:00
|
|
|
'relations' => [
|
2025-06-27 13:48:58 +08:00
|
|
|
'creater' => ['id', 'nickname',],
|
2025-06-27 15:24:58 +08:00
|
|
|
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
2025-06-27 11:19:40 +08:00
|
|
|
// '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'],
|
2025-06-27 17:04:25 +08:00
|
|
|
'relations' => [
|
2025-06-27 15:24:58 +08:00
|
|
|
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
2025-06-27 11:19:40 +08:00
|
|
|
// 'translation' => ['model_id', 'name']
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function add(array $params)
|
|
|
|
|
{
|
|
|
|
|
$params['locale'] = locale();
|
2025-06-27 17:04:25 +08:00
|
|
|
if (!empty($params['publish_time'])) {
|
2025-06-27 11:19:40 +08:00
|
|
|
$params['publish_time'] = strtotime($params['publish_time']);
|
2025-06-27 17:04:25 +08:00
|
|
|
} else {
|
2025-06-27 11:19:40 +08:00
|
|
|
$params['publish_time'] = time();
|
|
|
|
|
}
|
2025-06-27 15:24:58 +08:00
|
|
|
$num = $params['num'] ?? 0;
|
|
|
|
|
unset($params['num']);
|
2025-06-27 11:19:40 +08:00
|
|
|
|
2025-06-30 14:34:23 +08:00
|
|
|
// 将OSS直连地址替换为代理地址
|
|
|
|
|
$ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/';
|
|
|
|
|
$proxyPath = '/console/upload/image/proxy?file=';
|
|
|
|
|
|
|
|
|
|
$params['content'] = str_replace(
|
|
|
|
|
'?Expires=',
|
|
|
|
|
'&Expires=',
|
|
|
|
|
$params['content']
|
|
|
|
|
);
|
|
|
|
|
$params['content'] = str_replace(
|
|
|
|
|
$ossDomain,
|
|
|
|
|
$proxyPath,
|
|
|
|
|
$params['content']
|
|
|
|
|
);
|
|
|
|
|
|
2025-06-27 11:19:40 +08:00
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$info = $this->create($params);
|
|
|
|
|
if (!$info) {
|
|
|
|
|
throw new BusinessException('提交失败');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 17:04:25 +08:00
|
|
|
if ($num > 0) {
|
|
|
|
|
CommentService::createComment($info->id, $params['content'], $params['publish_time'], $num);
|
2025-06-27 15:24:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-27 11:19:40 +08:00
|
|
|
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']);
|
2025-06-27 15:24:58 +08:00
|
|
|
$num = $params['num'] ?? 0;
|
|
|
|
|
unset($params['num']);
|
2025-06-27 11:19:40 +08:00
|
|
|
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$info = $this->update($id, $params);
|
|
|
|
|
if (!$info) {
|
|
|
|
|
throw new BusinessException('提交失败');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 17:04:25 +08:00
|
|
|
if ($num > 0) {
|
|
|
|
|
CommentService::createComment($id, $params['content'], $params['publish_time'], $num);
|
2025-06-27 15:24:58 +08:00
|
|
|
}
|
2025-06-27 11:19:40 +08:00
|
|
|
Db::commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
Db::rollBack();
|
|
|
|
|
throw $th;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|