Files
bsh_sample/app/common/service/PostCommentService.php
T
2025-12-26 16:20:46 +08:00

214 lines
6.1 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\MessageNotice;
use app\common\model\Post;
use app\common\model\User;
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['scene']) && $p['scene']=='self') {
if(!empty($request->user) && !empty($request->user->uid)){
$config['where']['user_id'] = $request->user->uid;
}
}
// 只查询根评论
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'];
}
}
//安全检查
$wechat = new WechatService();
$tokenData = $wechat->getMiniAccessToken();
$access_token = $tokenData['access_token'];
$user = User::where('id', $request->user->uid)->first();
if(empty($user)){
throw new \Exception('用户不存在');
}
$tokenData = $wechat->checkText($data['content'], $user['openid'], $access_token, 2);
// $tokenData = $wechat->checkText($data['content'], 'o8W4t5cRtq2LuOJUxvBGWQA6HJKQ', $access_token, 2);
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();
$post = Post::where(['id' => $comment['post_id']])->first();
if($scene == 'mini' && ($comment['user_id'] != $request->user->uid || $post['user_id'] == $request->user->uid)){
throw new \Exception("只能删除本人评论");
}
$post['comment'] = $post['comment']-1;
$post->save();
$count = $comment->delete();
return $count;
}
/**
* 删除前置处理
*/
public function beforeServiceDestroy($data, Request $request)
{
$param = $request->all();
if (empty($param['id'])) {
throw new \Exception("参数错误");
}
$where = [];
$where['id'] = $param['id'];
$comment = PostComment::where($where)->first();
$post = Post::where(['id' => $comment['post_id']])->first();
$post['comment'] = $post['comment']-1;
$post->save();
return $data;
}
/** 删除 */
public function serviceDestroy(Request $request, string $scene = 'destroy')
{
$data = $request->all();
$this->preProcess(ucfirst($scene), $data, $request);
$config = $this->buildQueryConfig($request, $scene);
$deleted = $this->logicDeleteBy($config);
return $this->postProcess(ucfirst($scene), $deleted);
}
}