wesmiler 1 день назад
Родитель
Сommit
be6eaf3a06

+ 85 - 0
app/Http/Controllers/Api/v1/CommentController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers\Api\v1;
+
+use App\Http\Controllers\Api\webApp;
+use App\Services\Api\PostRecordService;
+
+/**
+ * 商品评论管理
+ * @package App\Http\Controllers\Api
+ */
+class CommentController 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);
+    }
+
+    /**
+     * 列表
+     * @return array
+     */
+    public function list()
+    {
+        $params =request()->post();
+        $pageSize = request()->post('pageSize', 15);
+        $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, PostRecordService::make()->getErrorData());
+            } 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 ($result = PostRecordService::make()->submit($this->userId, $params)) {
+                return showJson(PostRecordService::make()->getError(), true, $result);
+            } else {
+                $errorCode = PostRecordService::make()->getErrorCode();
+                return showJson(PostRecordService::make()->getError(), false,'',$errorCode);
+            }
+        }  catch (\Exception $exception) {
+            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
+            return showJson(1046, false, $error);
+        }
+    }
+
+    /**
+     * 删除
+     * @return mixed
+     */
+    public function delete()
+    {
+        return PostRecordService::make()->delete();
+    }
+}

+ 196 - 0
app/Services/Api/CommentService.php

@@ -0,0 +1,196 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services\Api;
+
+use App\Models\GoodsCommentModel;
+use App\Models\OrderModel;
+use App\Services\BaseService;
+use App\Services\RedisService;
+
+/**
+ * 商品评论-服务类
+ * @author laravel开发员
+ * @since 2020/11/11
+ * @package App\Services\Api
+ */
+class CommentService extends BaseService
+{
+    // 静态对象
+    protected static $instance = null;
+
+    /**
+     * 构造函数
+     * @author laravel开发员
+     * @since 2020/11/11
+     */
+    public function __construct()
+    {
+        $this->model = new GoodsCommentModel();
+    }
+
+    /**
+     * 静态入口
+     */
+    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('goods_comments.id','desc');
+        $list = $query->select(['goods_comments.*'])->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 = ['goods_comments.mark' => 1];
+        return $this->model->with(['user'])->from('goods_comments')
+            ->where(function ($query) use($params){
+
+                $status = isset($params['status'])? $params['status'] : 0;
+                $type = isset($params['type'])? $params['type'] : 0;
+                $userId = isset($params['user_id'])? $params['user_id'] : 0;
+                $goodsId = isset($params['goods_id'])? $params['goods_id'] : 0;
+                $orderId = isset($params['order_id'])? $params['order_id'] : 0;
+
+                if($status>0){
+                   $query->where('goods_comments.status',$status);
+                }
+
+                if($userId>0){
+                    $query->where('goods_comments.user_id',$userId);
+                }
+
+                if($orderId>0){
+                    $query->where('goods_comments.order_id',$orderId);
+                }
+
+                if($goodsId>0){
+                    $query->where('goods_comments.goods_ids','like',"{$goodsId},");
+                }
+
+                if($type>0){
+                    $query->where('goods_comments.type',$type);
+                }
+            })->where($where);
+    }
+
+    /**
+     * 评论
+     * @param $userId
+     * @param $params
+     * @return array|false
+     */
+    public function submit($userId, $params)
+    {
+        $orderId = isset($params['order_id']) ? intval($params['order_id']) : 0;
+        $score = isset($params['score']) ? intval($params['score']) : 0;
+        $content = isset($params['content']) ? trim($params['content']) : '';
+        $albums = isset($params['albums']) ? $params['albums'] : [];
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
+            return false;
+        }
+
+        if($score<=0 || $score>5){
+            $this->error = '请选择评分';
+            return false;
+        }
+
+        if(empty($content)){
+            $this->error = '请填写评价内容';
+            return false;
+        }
+
+        if($userId<=0){
+            $this->error = '请先登录';
+            $this->errorCode = 403;
+            return false;
+        }
+
+        $cacheKey = "caches:goods:comment_{$userId}_{$orderId}";
+        if(RedisService::get($cacheKey.'_lock')){
+            $this->error = '请不要频繁提交';
+            return false;
+        }
+
+        RedisService::set($cacheKey, $params, rand(20,30));
+        $info = OrderModel::with(['user','orderGoods'])->where(['id'=>$orderId,'mark'=>1])->first();
+        $orderGoods = isset($info['orderGoods'])?$info['orderGoods']:[];
+        if (empty($info)) {
+            RedisService::clear($cacheKey.'_lock');
+            $this->error = '该订单不存在';
+            return false;
+        }
+
+        // 是否已经评价
+        if(GoodsCommentModel::where(['order_id'=>$orderId,'user_id'=>$userId,'mark'=>1])){
+            RedisService::clear($cacheKey.'_lock');
+            $this->error = '您已经评价过';
+            return false;
+        }
+
+        $goodsIds = array_column($orderGoods,'goods_id');
+        $data = [
+            'goods_ids'=>$goodsIds,
+            'order_id'=>$orderId,
+            'user_id'=> $userId,
+            'score'=> $score,
+            'albums'=> $albums,
+            'create_time'=>time(),
+            'content'=> $content,
+            'status'=> 1,
+        ];
+
+        if(!$cid = $this->model->insertGetId($data)){
+           $this->error = '评价失败';
+
+        }
+
+        $this->error = '评价成功';
+        return ['id'=> $cid,'order_id'=>$orderId];
+    }
+
+
+    /**
+     * 删除
+     * @return array
+     */
+    public function delete()
+    {
+        $this->model->where('mark',0)->where('create_time','<=', 600)->delete();
+        return parent::delete(); // TODO: Change the autogenerated stub
+    }
+
+}

+ 4 - 0
routes/api.php

@@ -133,6 +133,10 @@ Route::prefix('v1')->middleware('web.login')->group(function() {
     Route::post('/posts/comment/list', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'list']);
     Route::post('/posts/comment/delete', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'delete']);
 
+    // 商品评论
+    Route::post('/comment/submit', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'submit']);
+    Route::post('/comment/index', [\App\Http\Controllers\Api\v1\PostRecordController::class, 'index']);
+
     // 消息
     Route::post('/message/index', [\App\Http\Controllers\Api\v1\MessageController::class, 'index']);
     Route::post('/message/list', [\App\Http\Controllers\Api\v1\MessageController::class, 'list']);