Merge branch 'feature/article' into develop
This commit is contained in:
@@ -46,7 +46,13 @@ class ArticleController extends BaseController
|
||||
if ($item['cover']) {
|
||||
$item['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['cover']);
|
||||
}
|
||||
foreach ($item['comment'] as &$comment) {
|
||||
|
||||
if ($comment['avatar']) {
|
||||
$comment['avatar_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($comment['avatar']);
|
||||
}
|
||||
}
|
||||
unset($comment);
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\console\model\CommentModel;
|
||||
use app\console\service\CommentService;
|
||||
use app\console\validate\CommentValidator;
|
||||
use app\mzl\common\controller\BaseController;
|
||||
|
||||
class CommentController extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* 不需要验证token的接口
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedJwt = [];
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected mixed $validator = CommentValidator::class;
|
||||
|
||||
/**
|
||||
* 返回业务 Service 实例
|
||||
*/
|
||||
protected function getService()
|
||||
{
|
||||
return new CommentService(new CommentModel());
|
||||
}
|
||||
|
||||
/**
|
||||
* 额外字段处理 (根据场景处理)
|
||||
* @param array $item
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function postProcessItem(array $item, string $scene): array
|
||||
{
|
||||
|
||||
if ($scene === 'default') {
|
||||
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数预处理 (根据场景处理)
|
||||
* @param Request $request
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareParams(Request $request, string $scene): array
|
||||
{
|
||||
$params = parent::prepareParams($request, $scene);
|
||||
|
||||
if ($scene === 'default') {
|
||||
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return Response 响应对象
|
||||
*/
|
||||
public function index(Request $request, string $scene = 'default'): Response
|
||||
{
|
||||
try {
|
||||
return parent::index($request, $scene);
|
||||
} catch (\Throwable $th) {
|
||||
return $this->errorResponse($th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,11 @@ class ArticleModel extends Model
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
public function comment()
|
||||
{
|
||||
return $this->hasMany(CommentModel::class, 'article_id', 'id');
|
||||
}
|
||||
|
||||
public function creater()
|
||||
{
|
||||
return $this->belongsTo(AdminsModel::class, 'user_id','id');
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* CommentModel
|
||||
*/
|
||||
class CommentAvatarModel extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'hs_comment_avatar';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* 模型的数据字段的保存格式。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = [];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* CommentModel
|
||||
*/
|
||||
class CommentModel extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'hs_comment';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* 模型的数据字段的保存格式。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -34,6 +34,7 @@ class ArticleService extends BaseService
|
||||
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'creater' => ['id', 'nickname',],
|
||||
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
||||
// 'translation' => ['model_id', 'name'],
|
||||
// 'brand.translation' => ['brand_id', 'name']
|
||||
],
|
||||
@@ -56,6 +57,7 @@ class ArticleService extends BaseService
|
||||
],
|
||||
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
||||
// 'translation' => ['model_id', 'name']
|
||||
],
|
||||
];
|
||||
@@ -71,6 +73,8 @@ class ArticleService extends BaseService
|
||||
}else{
|
||||
$params['publish_time'] = time();
|
||||
}
|
||||
$num = $params['num'] ?? 0;
|
||||
unset($params['num']);
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
@@ -79,6 +83,10 @@ class ArticleService extends BaseService
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
if($num > 0){
|
||||
CommentService::createComment($info->id,$params['content'],$num);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
@@ -91,6 +99,8 @@ class ArticleService extends BaseService
|
||||
{
|
||||
$params['locale'] = locale();
|
||||
$params['publish_time'] = strtotime($params['publish_time']);
|
||||
$num = $params['num'] ?? 0;
|
||||
unset($params['num']);
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
@@ -99,6 +109,9 @@ class ArticleService extends BaseService
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
if($num > 0){
|
||||
CommentService::createComment($id,$params['content'],$num);
|
||||
}
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class CommentValidator extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user