wesmiler 1 dag geleden
bovenliggende
commit
9e9612c8f4

+ 65 - 0
app/Http/Controllers/Api/v1/PostRecordController.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Http\Controllers\Api\v1;
+
+use App\Http\Controllers\Api\webApp;
+use App\Services\Api\PostRecordService;
+use App\Services\Api\SocialService;
+use App\Services\Common\AdService;
+
+/**
+ * 动态评论管理
+ * @package App\Http\Controllers\Api
+ */
+class PostRecordController extends webApp
+{
+
+    /**
+     * 列表
+     * @return array
+     */
+    public function index()
+    {
+        $params =request()->post();
+        $pageSize = request()->post('pageSize', 15);
+        $params['user_id'] = $this->userId;
+        $datas = PostRecordService::make()->getDataList($params, $pageSize);
+        return message(1010, true, $datas);
+    }
+
+    /**
+     * 点赞
+     */
+    public function like()
+    {
+        $params = request()->post();
+        try {
+            if (PostRecordService::make()->like($this->userId, $params)) {
+                return showJson(PostRecordService::make()->getError(), true);
+            } else {
+                return showJson(PostRecordService::make()->getError(), false);
+            }
+        }  catch (\Exception $exception) {
+            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
+            return showJson(1046, false, $error);
+        }
+    }
+
+    /**
+     * 评论/回复
+     */
+    public function submit()
+    {
+        $params = request()->post();
+        try {
+            if (PostRecordService::make()->comment($this->userId, $params)) {
+                return showJson(PostRecordService::make()->getError(), true);
+            } else {
+                return showJson(PostRecordService::make()->getError(), false);
+            }
+        }  catch (\Exception $exception) {
+            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
+            return showJson(1046, false, $error);
+        }
+    }
+}

+ 0 - 37
app/Http/Controllers/Api/v1/SocialController.php

@@ -81,41 +81,4 @@ class SocialController extends webApp
             return showJson(1046, false, $error);
         }
     }
-
-
-    /**
-     * 点赞
-     */
-    public function like()
-    {
-        $params = request()->post();
-        try {
-            if (SocialService::make()->like($this->userId, $params)) {
-                return showJson(SocialService::make()->getError(), true);
-            } else {
-                return showJson(SocialService::make()->getError(), false);
-            }
-        }  catch (\Exception $exception) {
-            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
-            return showJson(1046, false, $error);
-        }
-    }
-
-    /**
-     * 评论/回复
-     */
-    public function comment()
-    {
-        $params = request()->post();
-        try {
-            if (SocialService::make()->comment($this->userId, $params)) {
-                return showJson(SocialService::make()->getError(), true);
-            } else {
-                return showJson(SocialService::make()->getError(), false);
-            }
-        }  catch (\Exception $exception) {
-            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
-            return showJson(1046, false, $error);
-        }
-    }
 }

+ 258 - 0
app/Services/Api/PostRecordService.php

@@ -0,0 +1,258 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services\Api;
+
+use App\Models\PostModel;
+use App\Models\PostRecordModel;
+use App\Services\BaseService;
+use App\Services\RedisService;
+
+/**
+ * 动态评论/点赞-服务类
+ * @author laravel开发员
+ * @since 2020/11/11
+ * @package App\Services\Api
+ */
+class PostRecordService extends BaseService
+{
+    // 静态对象
+    protected static $instance = null;
+
+    /**
+     * 构造函数
+     * @author laravel开发员
+     * @since 2020/11/11
+     */
+    public function __construct()
+    {
+        $this->model = new PostRecordModel();
+    }
+
+    /**
+     * 静态入口
+     */
+    public static function make()
+    {
+        if (!self::$instance) {
+            self::$instance = new static();
+        }
+        return self::$instance;
+    }
+
+    /**
+     * @param $params
+     * @param int $pageSize
+     * @return array
+     */
+    public function getDataList($params, $pageSize = 15)
+    {
+        $query = $this->getQuery($params);
+        $query->orderBy('posts_records.id','desc');
+        $list = $query->select(['posts_records.*'])->paginate($pageSize > 0 ? $pageSize : 9999999);
+        $list = $list? $list->toArray() :[];
+
+        return [
+            'pageSize'=> $pageSize,
+            'total'=>isset($list['total'])? $list['total'] : 0,
+            'list'=> isset($list['data'])? $list['data'] : []
+        ];
+    }
+
+    /**
+     * 查询
+     * @param $params
+     * @return mixed
+     */
+    public function getQuery($params)
+    {
+        $where = ['posts_records.mark' => 1];
+        $status = isset($params['status'])? $params['status'] : 0;
+        $type = isset($params['type'])? $params['type'] : 0;
+        $userId = isset($params['user_id'])? $params['user_id'] : 0;
+
+        if($status>0){
+            $where['posts_records.status'] = $status;
+        }
+
+        if($type>0){
+            $where['posts_records.type'] = $type;
+        }
+
+
+        return $this->model->with(['user'])->from('posts_records')
+            ->where($where)
+            ->where(function ($query) use($params){
+                $keyword = isset($params['keyword'])? $params['keyword'] : '';
+                if($keyword){
+                    $query->where('posts_records.title','like',"%{$keyword}%")
+                        ->orWhere('posts_records.tags','like',"%{$keyword}%");
+                }
+
+                $tag = isset($params['tag'])? $params['tag'] : '';
+                if($tag){
+                    $query->where('posts_records.tags','like',"%{$tag},%");
+                }
+            });
+    }
+
+    /**
+     * 评论
+     * @param $userId
+     * @param $params
+     * @return array|false
+     */
+    public function submit($userId, $params)
+    {
+        $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
+        $replyId = isset($params['reply_id']) ? intval($params['reply_id']) : 0;
+        $replyUid = isset($params['reply_uid']) ? intval($params['reply_uid']) : 0;
+        $description = isset($params['description']) && $params['description']? trim($params['description']) : '';
+        $albums = isset($params['albums']) ? get_format_images($params['albums'], '') : '';
+        if(empty($sourceId)){
+            $this->error = '请选择评论的动态';
+            return false;
+        }
+
+        if(empty($description)){
+            $this->error = '评论内容不能为空';
+            return false;
+        }
+
+        $cacheKey = "caches:members:comment_{$userId}_{$sourceId}";
+        if(RedisService::get($cacheKey.'_lock')){
+            $this->error = '请不要频繁提交';
+            return false;
+        }
+
+        RedisService::set($cacheKey, $params, rand(20,30));
+        $info = PostModel::with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
+        $postUserId = isset($info['user_id'])?$info['user_id']:0;
+        $userInfo = isset($info['user'])?$info['user']:[];
+        if (empty($info) || $sourceId<=0 || $postUserId<=0) {
+            RedisService::clear($cacheKey.'_lock');
+            $this->error = '该动态已不存在';
+            return false;
+        }
+
+        $replyInfo = $this->model->with(['user'])->where(['id'=>$replyId,'mark'=>1])->first();
+        if ($replyId>0 && empty($replyInfo)) {
+            RedisService::clear($cacheKey.'_lock');
+            $this->error = '回复内容不存在';
+            return false;
+        }
+
+        $data = [
+            'source_id'=>$sourceId,
+            'reply_id'=>$replyId,
+            'reply_uid'=>$replyUid,
+            'user_id'=> $userId,
+            'albums'=> $albums,
+            'type'=>1,
+            'create_time'=>time(),
+            'description'=> $description,
+            'status'=> 1,
+        ];
+
+        if($sourceId !=  $this->model->insertGetId($data)){
+           $this->error = $replyId>0?'回复失败':'评论失败';
+
+        }
+
+        $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
+        $msgData = [
+            'from_uid'=> $userId,
+            'type'=> 4,
+            'title'=>'互动消息',
+            'description'=> $replyId? "回复{$nickname}评论了您的动态评论": "用户{$nickname}评论了您的动态",
+            'content'=> json_encode(['source_id'=>$sourceId,'page'=>'/pagesSub/pages/posts/detail?id='.$sourceId.'&replyId='.$replyId]),
+            'msg_type'=>4,
+        ];
+
+        // 互动消息
+        MessageService::make()->pushMessage($postUserId, $msgData);
+        $this->error = $replyId>0?'回复成功':'评论成功';
+        return ['id' => $sourceId];
+    }
+
+
+    /**
+     * 点赞
+     * @param $userId
+     * @param $params
+     * @return array|false
+     */
+    public function like($userId, $params)
+    {
+        $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
+        $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
+        $info = PostModel::with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
+        $postUserId = isset($info['user_id'])?$info['user_id']:0;
+        $userInfo = isset($info['user'])?$info['user']:[];
+        if (empty($info) || $sourceId<=0 || $postUserId<=0) {
+            $this->error = '该内容已不存在';
+            return false;
+        }
+
+        if (!in_array($status,[1,2])) {
+            $this->error = '操作状态错误';
+            return false;
+        }
+
+        $record = $this->model->where(['source_id'=>$sourceId,'user_id'=>$userId,'type'=>2,'mark'=>1])->first();
+        $recordId = isset($record['id'])?$record['id'] : 0;
+        $recordStatus = isset($record['status'])?$record['status'] : 0;
+        if($recordId && $recordStatus == $status){
+            $this->error = '您已点赞过';
+            return false;
+        }
+
+        $data = [
+            'source_id'=>$sourceId,
+            'user_id'=>$userId,
+            'type'=>2,
+            'create_time'=>time(),
+            'description'=> '点赞',
+            'status'=> $status,
+        ];
+
+        $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
+        $msgData = [
+            'from_uid'=> $userId,
+            'type'=> 4,
+            'title'=>'互动消息',
+            'description'=> "用户{$nickname}点赞了您的笔记",
+            'msg_type'=>1,
+        ];
+        if($recordId){
+            $this->model->where(['id'=>$recordId])->update(['status'=>$status,'update_time'=>time()]);
+            // 互动消息
+            if($status == 1){
+                MessageService::make()->pushMessage($postUserId, $msgData);
+            }
+            $this->error = $status==1? '点赞成功':'取消点赞成功';
+            return ['id' => $sourceId];
+        }else{
+            if(!$sourceId = $this->model->insertGetId($data)){
+                $this->error = $status==1? '点赞失败':'取消点赞失败';
+                return false;
+            }
+
+            // 互动消息
+            if($status == 1){
+                MessageService::make()->pushMessage($postUserId, $msgData);
+            }
+            $this->error = $status==1? '点赞成功':'取消点赞成功';
+            return ['id' => $sourceId];
+        }
+    }
+
+}

+ 0 - 152
app/Services/Api/SocialService.php

@@ -205,156 +205,4 @@ class SocialService extends BaseService
         RedisService::clear($cacheKey."_lock");
         return ['id' => $id];
     }
-
-    /**
-     * 点赞
-     * @param $userId
-     * @param $params
-     * @return array|false
-     */
-    public function like($userId, $params)
-    {
-        $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
-        $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
-        $info = $this->model->with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
-        $postUserId = isset($info['user_id'])?$info['user_id']:0;
-        $userInfo = isset($info['user'])?$info['user']:[];
-        if (empty($info) || $sourceId<=0 || $postUserId<=0) {
-            $this->error = '该内容已不存在';
-            return false;
-        }
-
-        if (!in_array($status,[1,2])) {
-            $this->error = '操作状态错误';
-            return false;
-        }
-
-        $record = PostRecordModel::where(['source_id'=>$sourceId,'user_id'=>$userId,'type'=>2,'mark'=>1])->first();
-        $recordId = isset($record['id'])?$record['id'] : 0;
-        $recordStatus = isset($record['status'])?$record['status'] : 0;
-        if($recordId && $recordStatus == $status){
-            $this->error = '您已点赞过';
-            return false;
-        }
-
-        $data = [
-            'source_id'=>$sourceId,
-            'user_id'=>$userId,
-            'type'=>2,
-            'create_time'=>time(),
-            'description'=> '点赞',
-            'status'=> $status,
-        ];
-
-        $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
-        $msgData = [
-            'from_uid'=> $userId,
-            'type'=> 4,
-            'title'=>'互动消息',
-            'description'=> "用户{$nickname}点赞了您的笔记",
-            'msg_type'=>1,
-        ];
-        if($recordId){
-            PostRecordModel::where(['id'=>$recordId])->update(['status'=>$status,'update_time'=>time()]);
-            // 互动消息
-            if($status == 1){
-                MessageService::make()->pushMessage($postUserId, $msgData);
-            }
-            $this->error = $status==1? '点赞成功':'取消点赞成功';
-            return ['id' => $sourceId];
-        }else{
-            if(!$sourceId = PostRecordModel::insertGetId($data)){
-                $this->error = $status==1? '点赞失败':'取消点赞失败';
-                return false;
-            }
-
-            // 互动消息
-            if($status == 1){
-                MessageService::make()->pushMessage($postUserId, $msgData);
-            }
-            $this->error = $status==1? '点赞成功':'取消点赞成功';
-            return ['id' => $sourceId];
-        }
-    }
-
-
-    /**
-     * 评论
-     * @param $userId
-     * @param $params
-     * @return array|false
-     */
-    public function comment($userId, $params)
-    {
-        $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
-        $replyId = isset($params['reply_id']) ? intval($params['reply_id']) : 0;
-        $replyUid = isset($params['reply_uid']) ? intval($params['reply_uid']) : 0;
-        $description = isset($params['description']) && $params['description']? trim($params['description']) : '';
-        $albums = isset($params['albums']) ? get_format_images($params['albums'], '') : '';
-        if(empty($sourceId)){
-            $this->error = '请选择评论的动态';
-            return false;
-        }
-
-        if(empty($description)){
-            $this->error = '评论内容不能为空';
-            return false;
-        }
-
-        $cacheKey = "caches:members:comment_{$userId}_{$sourceId}";
-        if(RedisService::get($cacheKey.'_lock')){
-            $this->error = '请不要频繁提交';
-            return false;
-        }
-
-        RedisService::set($cacheKey, $params, rand(20,30));
-        $info = $this->model->with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
-        $postUserId = isset($info['user_id'])?$info['user_id']:0;
-        $userInfo = isset($info['user'])?$info['user']:[];
-        if (empty($info) || $sourceId<=0 || $postUserId<=0) {
-            RedisService::clear($cacheKey.'_lock');
-            $this->error = '该动态已不存在';
-            return false;
-        }
-
-        $replyInfo = PostRecordModel::with(['user'])->where(['id'=>$replyId,'mark'=>1])->first();
-        if ($replyId>0 && empty($replyInfo)) {
-            RedisService::clear($cacheKey.'_lock');
-            $this->error = '回复内容不存在';
-            return false;
-        }
-
-        $data = [
-            'source_id'=>$sourceId,
-            'reply_id'=>$replyId,
-            'reply_uid'=>$replyUid,
-            'user_id'=> $userId,
-            'albums'=> $albums,
-            'type'=>1,
-            'create_time'=>time(),
-            'description'=> $description,
-            'status'=> 1,
-        ];
-
-        if($sourceId !=  PostRecordModel::insertGetId($data)){
-           $this->error = $replyId>0?'回复失败':'评论失败';
-
-        }
-
-        $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
-        $msgData = [
-            'from_uid'=> $userId,
-            'type'=> 4,
-            'title'=>'互动消息',
-            'description'=> $replyId? "回复{$nickname}评论了您的动态评论": "用户{$nickname}评论了您的动态",
-            'content'=> json_encode(['source_id'=>$sourceId,'page'=>'/pagesSub/pages/posts/detail?id='.$sourceId.'&replyId='.$replyId]),
-            'msg_type'=>4,
-        ];
-
-        // 互动消息
-        MessageService::make()->pushMessage($postUserId, $msgData);
-        $this->error = $replyId>0?'回复成功':'评论成功';
-        return ['id' => $sourceId];
-    }
-
 }

+ 3 - 4
routes/api.php

@@ -117,10 +117,9 @@ Route::prefix('v1')->middleware('web.login')->group(function() {
     // 笔记
     Route::get('/posts/info', [\App\Http\Controllers\Api\v1\SocialController::class, 'info']);
     Route::post('/posts/submit', [\App\Http\Controllers\Api\v1\SocialController::class, 'submit']);
-    Route::post('/posts/like', [\App\Http\Controllers\Api\v1\SocialController::class, 'like']);
-    Route::post('/posts/comment', [\App\Http\Controllers\Api\v1\SocialController::class, 'comment']);
-    Route::post('/posts/comment/list', [\App\Http\Controllers\Api\v1\SocialController::class, 'commentList']);
-
+    Route::post('/posts/like', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'like']);
+    Route::post('/posts/comment/submit', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'submit']);
+    Route::post('/posts/comment/list', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'index']);
 
     // 消息
     Route::post('/message/index', [\App\Http\Controllers\Api\v1\MessageController::class, 'index']);