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;
use app\apis\model\CommentModel;
use support\Request;
use support\Response;
use app\apis\model\ArticleModel;
@@ -47,11 +48,12 @@ class ArticleController extends BaseController
if ($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) {
if ($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);
}
+12
View File
@@ -2,6 +2,8 @@
namespace app\apis\model;
use app\console\model\AdminsModel;
use app\console\model\CommentModel;
use support\Model;
/**
@@ -53,4 +55,14 @@ 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');
}
}
+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'],
'relations' => [
'creater' => ['id', 'nickname',],
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
// 'translation' => ['model_id', 'name'],
// 'brand.translation' => ['brand_id', 'name']
// 'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
],
'sorts' => [
['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'],
'relations' => [
'creater' => ['id', 'nickname',],
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
// 'translation' => ['model_id', 'name']
],
@@ -72,6 +72,7 @@ class ArticleController extends BaseController
$params['filters'] = [];
$params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => 1];
$params['filters'][] = ['field' => 'locale', 'operator' => '=', 'value' => locale()];
$params['filters'][] = ['field' => 'publish_time', 'operator' => '<', 'value' => time()];
}
+34 -1
View File
@@ -58,7 +58,8 @@ class CommentController extends BaseController
$params = parent::prepareParams($request, $scene);
if ($scene === 'default') {
$params['filters'] = [];
$params['filters'][] = ['field' => 'article_id', 'operator' => '=', 'value' => $request->input('id')];
}
return $params;
@@ -78,4 +79,36 @@ class CommentController extends BaseController
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());
}
}
}
+2 -2
View File
@@ -84,7 +84,7 @@ class ArticleService extends BaseService
}
if ($num > 0) {
CommentService::createComment($info->id,$params['content'],$num);
CommentService::createComment($info->id, $params['content'], $params['publish_time'], $num);
}
Db::commit();
@@ -110,7 +110,7 @@ class ArticleService extends BaseService
}
if ($num > 0) {
CommentService::createComment($id,$params['content'],$num);
CommentService::createComment($id, $params['content'], $params['publish_time'], $num);
}
Db::commit();
return true;
+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();
}
+9 -2
View File
@@ -53,8 +53,8 @@ Route::group('/apis', function() {
Route::any('/energy/preset', [\app\console\controller\EnergyController::class, 'presets']);
//文章列表
Route::any('/article/list', [\app\console\controller\ArticleController::class, 'index']);
Route::any('/article/{id}/detail', [\app\console\controller\ArticleController::class, 'detail']);
Route::any('/article/list', [\app\apis\controller\ArticleController::class, 'index']);
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}/delete', [\app\console\controller\ArticleController::class, 'destroy']);
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']);
});
// ====== ====== ====== 后台管理 ====== ====== ======