This commit is contained in:
2025-06-27 17:04:25 +08:00
parent c30ff3cca1
commit c8eb5f33ee
8 changed files with 147 additions and 46 deletions
+77 -30
View File
@@ -2,11 +2,14 @@
namespace app\console\service;
use app\console\model\CommentAvatarModel;
use app\console\model\CommentModel;
use app\mzl\common\org\XunfeiSpark;
use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use support\Db;
use support\exception\BusinessException;
class CommentService extends BaseService
{
@@ -26,39 +29,82 @@ class CommentService extends BaseService
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,
// ]
// ];
// }
'default' => function ($params) {
return [
'filters' => $params['filters'],
'fields' => ['id', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at'],
'relations' => [
],
'sorts' => [
['field' => 'id', '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', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at'],
'relations' => [
],
];
}
];
}
public static function createComment($articleId, $articleContent, $num): array
public function add(array $params)
{
$params['locale'] = locale();
$avatar = CommentAvatarModel::where(['avatar' => $params['avatar']])->first();
if (!$avatar) {
CommentAvatarModel::create(['avatar' => $params['avatar']]);
}
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();
$avatar = CommentAvatarModel::where(['avatar' => $params['avatar']])->first();
if (!$avatar) {
CommentAvatarModel::create(['avatar' => $params['avatar']]);
}
Db::beginTransaction();
try {
$info = $this->update($id, $params);
if (!$info) {
throw new BusinessException('提交失败');
}
Db::commit();
return true;
} catch (\Throwable $th) {
Db::rollBack();
throw $th;
}
}
public static function createComment($articleId, $articleContent, $publishTime, $num): array
{
for ($i = 1; $i <= $num; $i++) {
@@ -76,6 +122,7 @@ class CommentService extends BaseService
$comment->article_id = $articleId;
$comment->name = $user['name'] ?? 'Anonymous user';
$comment->avatar = $user['avatar'] ?? '';
$comment->created_at = $publishTime + rand(1, 86400*3);
$comment->save();
}