4d94614d40
2、收藏搜索 3、帖子、评论删除
159 lines
4.4 KiB
PHP
159 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\MessageNotice;
|
|
use app\common\model\Post;
|
|
use app\common\validate\PostCommentValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\PostComment;
|
|
use support\Redis;
|
|
use support\Request;
|
|
|
|
class PostCommentService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = PostComment::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = PostCommentValidate::class;
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
/**
|
|
* 构造 CrudUtil 查询配置
|
|
*/
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'default':
|
|
$config['where']['status'] = 1;
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'index':
|
|
$config['where']['status'] = 1;
|
|
$config['order']['id'] = 'desc';
|
|
|
|
// 帖子
|
|
if (isset($p['post_id']) && !empty($p['post_id'])) {
|
|
$config['where']['post_id'] = $p['post_id'];
|
|
}
|
|
// 用户
|
|
if (isset($p['user_id']) && !empty($p['user_id'])) {
|
|
$config['where']['user_id'] = $p['user_id'];
|
|
}
|
|
// 只查询根评论
|
|
if (isset($p['parent_id']) && $p['parent_id'] == 0) {
|
|
$config['where']['parent_id'] = 0;
|
|
}
|
|
// 根评论ID
|
|
if (isset($p['parent_id']) && !empty($p['parent_id'])) {
|
|
$config['where']['root_id'] = $p['parent_id'];
|
|
}
|
|
// 根评论ID
|
|
if (isset($p['received_id']) && !empty($p['received_id'])) {
|
|
$config['where']['id'] = ['not in', $p['received_id']];
|
|
}
|
|
// 根评论ID
|
|
if (isset($p['status']) && !empty($p['status'])) {
|
|
$config['where']['status'] = $p['status'];
|
|
}
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
$config['where']['status'] = 1;
|
|
break;
|
|
case 'store':
|
|
break;
|
|
case 'update':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 添加前置处理
|
|
*/
|
|
public function beforeServiceStore($data, Request $request)
|
|
{
|
|
$data['user_id'] = $request->user->uid;;
|
|
$data['root_id'] = 0;
|
|
if(!empty($data['parent_id'])){
|
|
$data['root_id'] = PostComment::where('id', $data['parent_id'])->value('root_id');
|
|
if($data['root_id'] == 0){
|
|
$data['root_id'] = $data['parent_id'];
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 添加后置处理
|
|
*/
|
|
public function afterServiceStore($res)
|
|
{
|
|
$data = [];
|
|
$data['post_id'] = $res['post_id'];
|
|
$data['receiver_user_id'] = Post::where(['id' => $data['post_id']])->value('user_id');
|
|
$data['sender_user_id'] = $res['user_id'];
|
|
$data['comment_id'] = $res['id'];
|
|
$data['type'] = 3;
|
|
MessageNotice::create($data);
|
|
$post = Post::where(['id' => $data['post_id']])->first();
|
|
$post['comment'] = $post['comment']+1;
|
|
$post->save();
|
|
}
|
|
|
|
/**
|
|
* 添加后置处理
|
|
*/
|
|
public function serviceDelete(Request $request,$scene = 'admin')
|
|
{
|
|
$param = $request->all();
|
|
|
|
if (empty($param['id'])) {
|
|
throw new \Exception("参数错误");
|
|
}
|
|
|
|
$where = [];
|
|
$where['id'] = $param['id'];
|
|
$comment = PostComment::where($where)->first();
|
|
|
|
if($scene == 'mini' && $comment['user_id'] != $request->user->uid){
|
|
throw new \Exception("只能删除本人评论");
|
|
}
|
|
|
|
$post = Post::where(['id' => $comment['post_id']])->first();
|
|
$post['comment'] = $post['comment']-1;
|
|
$post->save();
|
|
|
|
$count = $comment->delete();
|
|
|
|
return $count;
|
|
}
|
|
} |