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
+3 -1
View File
@@ -2,6 +2,7 @@
namespace app\apis\controller; namespace app\apis\controller;
use app\apis\model\CommentModel;
use support\Request; use support\Request;
use support\Response; use support\Response;
use app\apis\model\ArticleModel; use app\apis\model\ArticleModel;
@@ -47,11 +48,12 @@ class ArticleController extends BaseController
if ($item['cover']) { if ($item['cover']) {
$item['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['cover']); $item['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['cover']);
} }
$item['comment'] = CommentModel::where(['article_id' => $item['id'], ['created_at', '<=', time()]])->get();
foreach ($item['comment'] as &$comment) { foreach ($item['comment'] as &$comment) {
if ($comment['avatar']) { if ($comment['avatar']) {
$comment['avatar_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($comment['avatar']); $comment['avatar_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($comment['avatar']);
} }
$comment['created_at'] = date('Y-m-d H:i:s', $comment['created_at']);
} }
unset($comment); unset($comment);
} }
+12
View File
@@ -2,6 +2,8 @@
namespace app\apis\model; namespace app\apis\model;
use app\console\model\AdminsModel;
use app\console\model\CommentModel;
use support\Model; use support\Model;
/** /**
@@ -53,4 +55,14 @@ class ArticleModel extends Model
* 不允许自动填充模型字段 * 不允许自动填充模型字段
*/ */
protected $guarded = []; protected $guarded = [];
public function comment()
{
return $this->hasMany(CommentModel::class, 'article_id', 'id');
}
public function creater()
{
return $this->belongsTo(AdminsModel::class, 'user_id','id');
}
} }
+2 -3
View File
@@ -31,9 +31,7 @@ class ArticleService extends BaseService
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'], 'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
'relations' => [ 'relations' => [
'creater' => ['id', 'nickname',], 'creater' => ['id', 'nickname',],
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',], // 'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
// 'translation' => ['model_id', 'name'],
// 'brand.translation' => ['brand_id', 'name']
], ],
'sorts' => [ 'sorts' => [
['field' => 'is_top', 'direction' => 'desc'], ['field' => 'is_top', 'direction' => 'desc'],
@@ -54,6 +52,7 @@ class ArticleService extends BaseService
], ],
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'], 'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
'relations' => [ 'relations' => [
'creater' => ['id', 'nickname',],
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',], 'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
// 'translation' => ['model_id', 'name'] // 'translation' => ['model_id', 'name']
], ],
+2 -1
View File
@@ -72,7 +72,8 @@ class ArticleController extends BaseController
$params['filters'] = []; $params['filters'] = [];
$params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => 1]; $params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => 1];
$params['filters'][] = ['field' => 'locale', 'operator' => '=', 'value' => locale()]; $params['filters'][] = ['field' => 'locale', 'operator' => '=', 'value' => locale()];
$params['filters'][] = ['field' => 'publish_time', 'operator' => '<', 'value' => time()];
} }
return $params; return $params;
+34 -1
View File
@@ -58,7 +58,8 @@ class CommentController extends BaseController
$params = parent::prepareParams($request, $scene); $params = parent::prepareParams($request, $scene);
if ($scene === 'default') { if ($scene === 'default') {
$params['filters'] = [];
$params['filters'][] = ['field' => 'article_id', 'operator' => '=', 'value' => $request->input('id')];
} }
return $params; return $params;
@@ -78,4 +79,36 @@ class CommentController extends BaseController
return $this->errorResponse($th->getMessage()); return $this->errorResponse($th->getMessage());
} }
} }
public function detail(Request $request, int $id, string $scene = 'show'): Response
{
try {
$request->setGet(['id'=>$id]);
return parent::one($request, $scene);
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage());
}
}
public function add(Request $request): Response
{
try {
return $this->getService()->add($request->all())
? $this->successResponse()
: $this->errorResponse();
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage());
}
}
public function edit(Request $request, int $id): Response
{
try {
return $this->getService()->edit($request->all(), $id)
? $this->successResponse()
: $this->errorResponse();
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage());
}
}
} }
+8 -8
View File
@@ -32,7 +32,7 @@ class ArticleService extends BaseService
return [ return [
'filters' => $params['filters'], 'filters' => $params['filters'],
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'], 'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
'relations' => [ 'relations' => [
'creater' => ['id', 'nickname',], 'creater' => ['id', 'nickname',],
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',], 'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
// 'translation' => ['model_id', 'name'], // 'translation' => ['model_id', 'name'],
@@ -56,7 +56,7 @@ class ArticleService extends BaseService
['field' => 'id', 'operator' => '=', 'value' => $params['id']] ['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'], 'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
'relations' => [ 'relations' => [
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',], 'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
// 'translation' => ['model_id', 'name'] // 'translation' => ['model_id', 'name']
], ],
@@ -68,9 +68,9 @@ class ArticleService extends BaseService
public function add(array $params) public function add(array $params)
{ {
$params['locale'] = locale(); $params['locale'] = locale();
if(!empty($params['publish_time'])){ if (!empty($params['publish_time'])) {
$params['publish_time'] = strtotime($params['publish_time']); $params['publish_time'] = strtotime($params['publish_time']);
}else{ } else {
$params['publish_time'] = time(); $params['publish_time'] = time();
} }
$num = $params['num'] ?? 0; $num = $params['num'] ?? 0;
@@ -83,8 +83,8 @@ class ArticleService extends BaseService
throw new BusinessException('提交失败'); throw new BusinessException('提交失败');
} }
if($num > 0){ if ($num > 0) {
CommentService::createComment($info->id,$params['content'],$num); CommentService::createComment($info->id, $params['content'], $params['publish_time'], $num);
} }
Db::commit(); Db::commit();
@@ -109,8 +109,8 @@ class ArticleService extends BaseService
throw new BusinessException('提交失败'); throw new BusinessException('提交失败');
} }
if($num > 0){ if ($num > 0) {
CommentService::createComment($id,$params['content'],$num); CommentService::createComment($id, $params['content'], $params['publish_time'], $num);
} }
Db::commit(); Db::commit();
return true; return true;
+77 -30
View File
@@ -2,11 +2,14 @@
namespace app\console\service; namespace app\console\service;
use app\console\model\CommentAvatarModel;
use app\console\model\CommentModel; use app\console\model\CommentModel;
use app\mzl\common\org\XunfeiSpark; use app\mzl\common\org\XunfeiSpark;
use app\mzl\common\services\BaseService; use app\mzl\common\services\BaseService;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use support\Db;
use support\exception\BusinessException;
class CommentService extends BaseService class CommentService extends BaseService
{ {
@@ -26,39 +29,82 @@ class CommentService extends BaseService
public function queryScenes(): array public function queryScenes(): array
{ {
return [ return [
// 'default' => function ($params) { 'default' => function ($params) {
// $keyword = $params['keyword'] ?? ''; return [
// return [ 'filters' => $params['filters'],
// 'filters' => [ 'fields' => ['id', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at'],
// ['field' => '***', 'operator' => '=', 'value' => '***'] 'relations' => [
// ], ],
// 'fields' => ['id'], 'sorts' => [
// 'relations' => [], ['field' => 'id', 'direction' => 'desc'],
// 'custom' => function(Builder $q) use ($keyword) { ],
// // 示例 'pagination' => [
// if ($keyword) { 'page' => $params['page'] ?? 1,
// $q->where(function($q2) use($keyword) { 'per_page' => $params['per_page'] ?? 10,
// $q2->whereHas('***', function($q3) use($keyword) { ]
// $q3->where('***', 'like', "%{$keyword}%"); ];
// })->orWhereHas('***', function($q4) use($keyword) { },
// $q4->where('***', 'like', "%{$keyword}%"); 'show' => function ($params) {
// }); return [
// }); 'filters' => [
// } ['field' => 'status', 'operator' => '=', 'value' => '1'],
// }, ['field' => 'id', 'operator' => '=', 'value' => $params['id']]
// 'sorts' => [ ],
// ['field' => 'created_at', 'direction' => 'desc'] 'fields' => ['id', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at'],
// ], 'relations' => [
// 'pagination' => [ ],
// 'page' => $params['page'] ?? 1, ];
// 'per_page' => $params['per_page'] ?? 10, }
// ]
// ];
// }
]; ];
} }
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++) { for ($i = 1; $i <= $num; $i++) {
@@ -76,6 +122,7 @@ class CommentService extends BaseService
$comment->article_id = $articleId; $comment->article_id = $articleId;
$comment->name = $user['name'] ?? 'Anonymous user'; $comment->name = $user['name'] ?? 'Anonymous user';
$comment->avatar = $user['avatar'] ?? ''; $comment->avatar = $user['avatar'] ?? '';
$comment->created_at = $publishTime + rand(1, 86400*3);
$comment->save(); $comment->save();
} }
+9 -2
View File
@@ -53,8 +53,8 @@ Route::group('/apis', function() {
Route::any('/energy/preset', [\app\console\controller\EnergyController::class, 'presets']); Route::any('/energy/preset', [\app\console\controller\EnergyController::class, 'presets']);
//文章列表 //文章列表
Route::any('/article/list', [\app\console\controller\ArticleController::class, 'index']); Route::any('/article/list', [\app\apis\controller\ArticleController::class, 'index']);
Route::any('/article/{id}/detail', [\app\console\controller\ArticleController::class, 'detail']); Route::any('/article/{id}/detail', [\app\apis\controller\ArticleController::class, 'detail']);
}); });
// ====== ====== ====== 网站 ====== ====== ====== // ====== ====== ====== 网站 ====== ====== ======
@@ -226,6 +226,13 @@ Route::group('/console', function() {
Route::any('/article/{id}/update', [\app\console\controller\ArticleController::class, 'edit']); Route::any('/article/{id}/update', [\app\console\controller\ArticleController::class, 'edit']);
Route::any('/article/{id}/delete', [\app\console\controller\ArticleController::class, 'destroy']); Route::any('/article/{id}/delete', [\app\console\controller\ArticleController::class, 'destroy']);
Route::any('/article/{id}/detail', [\app\console\controller\ArticleController::class, 'detail']); Route::any('/article/{id}/detail', [\app\console\controller\ArticleController::class, 'detail']);
//评论管理
Route::any('/comment/list', [\app\console\controller\CommentController::class, 'index']);
Route::any('/comment/add', [\app\console\controller\CommentController::class, 'add']);
Route::any('/comment/{id}/update', [\app\console\controller\CommentController::class, 'edit']);
Route::any('/comment/{id}/delete', [\app\console\controller\CommentController::class, 'destroy']);
Route::any('/comment/{id}/detail', [\app\console\controller\CommentController::class, 'detail']);
}); });
// ====== ====== ====== 后台管理 ====== ====== ====== // ====== ====== ====== 后台管理 ====== ====== ======