85 lines
2.9 KiB
PHP
85 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace app\console\service;
|
|
|
|
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;
|
|
|
|
class CommentService extends BaseService
|
|
{
|
|
/**
|
|
* 定义模型
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getModel(): string
|
|
{
|
|
return CommentModel::class;
|
|
}
|
|
|
|
/**
|
|
* 多场景配置
|
|
*/
|
|
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,
|
|
// ]
|
|
// ];
|
|
// }
|
|
];
|
|
}
|
|
|
|
public static function createComment($articleId, $articleContent, $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->save();
|
|
}
|
|
|
|
return $comment->toArray();
|
|
}
|
|
|
|
} |