132 lines
4.0 KiB
PHP
132 lines
4.0 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return CommentModel::class;
|
|
}
|
|
|
|
/**
|
|
* 多场景配置
|
|
*/
|
|
public function queryScenes(): array
|
|
{
|
|
return [
|
|
'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 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++) {
|
|
// 生成模拟用户
|
|
$user = \app\mzl\common\utils\ForeignUserGenerator::generateRandomName();
|
|
$user['avatar'] = \app\mzl\common\utils\ForeignUserGenerator::generateAvatar();
|
|
|
|
// 调用讯飞星火生成评论内容
|
|
$param = [['role' => 'user', 'content' => $articleContent]];
|
|
$commentContent = \app\mzl\common\org\XunfeiSpark::comment($param);
|
|
|
|
// 存入数据库
|
|
$comment = new \app\console\model\CommentModel();
|
|
$comment->content = $commentContent;
|
|
$comment->article_id = $articleId;
|
|
$comment->name = $user['name'] ?? 'Anonymous user';
|
|
$comment->avatar = $user['avatar'] ?? '';
|
|
$comment->created_at = $publishTime + rand(1, 86400*3);
|
|
$comment->save();
|
|
}
|
|
|
|
return $comment->toArray();
|
|
}
|
|
|
|
} |