Browse Source

fix: 评论

super-yimizi 1 month ago
parent
commit
1112934857

+ 9 - 11
application/api/controller/Goods.php

@@ -18,6 +18,7 @@ use think\Db;
 use app\common\Service\SkuSpec as SkuSpecService;
 use app\common\Service\Goods\CategoryService;
 use app\common\Service\DiscountService;
+use app\common\Service\Goods\CommentService;
 /**
  * 商品接口
  */
@@ -77,23 +78,20 @@ class Goods extends Base
             $row->sku_spec = $skuSpecData;  // 处理完后重新赋值
         }
         // 
+        unset($item);
 
         //服务保障
-        $row->guarantee = $row->guarantee_ids ? Guarantee::field('id,name,intro')->where('id', 'IN', $row->guarantee_ids)->where('status', 'normal')->select() : [];
+        $row->guarantee = $row->guarantee_ids ? Guarantee::field('id,name,intro')->where('id', 'IN', $row->guarantee_ids)
+        ->where('status', 'normal')->select() : [];
         //属性
         $row->attributes = AttributeValue::getAttributeList($row->attribute_ids);
         //好评度
-        // $row->favor_rate = Comment::degree($id);
+        $commentInfo = CommentService::getCommentCountAndFirstComment($id);
+        $row->comment_count = $commentInfo['total'] ?? 0;
+        $row->comment    = $commentInfo['comment'] ?? null;
 
-        //评论
-        $comment = collection($row->comment)->toArray();
-        foreach ($comment as &$item) {
-            if ($item['user']) {
-                $item['user']['avatar'] = cdnurl($item['user']['avatar'], true);
-            }
-        }
-        $row->setRelation('comment', $comment);
-        unset($item);
+    
+      
         //优惠券
         // $conditions = CouponCondition::getGoodsCondition($id, $row->category_id, $row->brand_id);
         // $sql = "condition_ids IS NULL OR condition_ids=''";

+ 43 - 0
application/api/controller/Share.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace app\api\controller;
+
+use think\Db;
+use app\common\model\Share as ShareModel;
+use app\api\validate\ShareValidate;
+
+class Share extends Base
+{
+
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    public function add()
+    {
+        $params = $this->request->only(['shareId', 'spm', 'page', 'query', 'from', 'platform']);
+
+        $validate = new ShareValidate();
+        if (!$validate->scene('add')->check($params)) {
+            $this->error($validate->getError());
+        }
+
+        $userId = $this->auth->id;
+
+       // $shareInfo = ShareModel::log($userId, $params);
+
+        $this->success("");
+    }
+
+    /**
+     * 查看分享记录
+     */
+    public function index()
+    {
+        $user = auth_user();
+        $logs = ShareModel::with(['user' => function ($query) {
+            return $query->field(['id', 'nickname', 'avatar']);
+        }])->where('share_id', $user->id)->paginate($this->request->param('list_rows', 8));
+
+        $this->success('获取成功', $logs);
+    }
+}

+ 79 - 0
application/api/validate/Share.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace app\api\validate;
+
+use think\Validate;
+use app\common\Enum\ChannelEnum;
+
+class ShareValidate extends Validate
+{
+    protected $rule = [
+        'shareId' => 'require|max:50',
+        'spm' => 'max:100',
+        'page' => 'require',
+        'query' => 'max:500',
+        'from' => 'require',
+        'platform' => 'checkSharePlatform',
+    ];
+
+    protected $message = [
+        'shareId.require' => '分享ID不能为空',
+        'shareId.max' => '分享ID长度不能超过50个字符',
+        'spm.max' => 'SPM参数长度不能超过100个字符',
+        'page.integer' => '页码必须为整数',
+        'page.gt' => '页码必须大于0',
+        'query.max' => '查询参数长度不能超过500个字符',
+        'from.max' => '来源参数长度不能超过50个字符',
+    ];
+
+    protected $scene = [
+        'add' => ['shareId', 'spm', 'page', 'query', 'from', 'platform'],
+    ];
+
+    /**
+     * 验证分享类型
+     * @param $value
+     * @param $rule
+     * @param $data
+     * @return bool|string
+     */
+    public function checkShareType($value, $rule, $data)
+    {
+        $validTypes = ['goods', 'activity', 'page'];
+        if (!in_array($value, $validTypes)) {
+            return '分享类型参数错误';
+        }
+        return true;
+    }
+
+    /**
+     * 验证分享渠道
+     * @param $value
+     * @param $rule
+     * @param $data
+     * @return bool|string
+     */
+    public function checkSharePlatform($value, $rule, $data)
+    {
+        $validChannels = ChannelEnum::getChannelMap();
+        if (!in_array($value, $validChannels)) {
+            return '分享渠道参数错误';
+        }
+        return true;
+    }
+
+    /**
+     * 验证分享URL
+     * @param $value
+     * @param $rule
+     * @param $data
+     * @return bool|string
+     */
+    public function checkShareUrl($value, $rule, $data)
+    {
+        if (!filter_var($value, FILTER_VALIDATE_URL)) {
+            return '分享链接格式不正确';
+        }
+        return true;
+    }
+} 

+ 30 - 0
application/common/Enum/UserShareEnum.php

@@ -0,0 +1,30 @@
+<?php
+namespace app\common\Enum;
+
+
+/**
+ * 全局状态枚举
+ */
+class UserShareEnum
+{
+
+    const FROM_FORWARD = 'forward';
+    const FROM_POSTER = 'poster';
+    const FROM_LINK = 'link';
+
+    public static function getFromList()
+    {
+        return [
+            self::FROM_FORWARD => '直接转发',
+            self::FROM_POSTER => '识别海报',
+            self::FROM_LINK => '分享链接',
+        ];
+    }
+
+    public static function getFromText($from)
+    {
+        return self::getFromList()[$from] ?? '未知';
+    }
+
+    public static function getFromList()
+}

+ 27 - 0
application/common/Service/Goods/CommentService.php

@@ -91,6 +91,33 @@ class CommentService
         return bcmul(bcdiv($favorable, $total, 2), 100);
     }
 
+    //  获取 总计评论数 和 第一个评论数据
+    public static function getCommentCountAndFirstComment($goods_id = 0)
+    {
+        $total = Comment::where('goods_id', $goods_id)
+            ->where('pid', 0)
+            ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
+            ->where('status', StatusEnum::ENABLED)
+            ->count();
+
+        $comment = Comment::where('goods_id', $goods_id)
+            ->with(['user' => function ($query) {
+                $query->field('id,nickname,avatar');
+            }])
+            ->where('pid', 0)
+            ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
+            ->where('status', StatusEnum::ENABLED)
+            ->order('id desc')
+            ->find();
+        if (!empty($comment)) {
+            $comment->user->avatar = cdnurl($comment->user->avatar, true);
+        }
+        return [
+            'total' => $total,
+            'comment' => $comment
+        ];
+    }
+
     /**
      * 获取用户评论列表
      * @param int $user_id 用户ID

+ 2 - 3
application/common/Service/SkuSpec.php

@@ -16,11 +16,10 @@ class SkuSpec
     {
         $list = (new SkuSpecModel())
              ->field('MIN(`id`) AS `id`, MIN(`goods_id`) AS `goods_id`, `spec_id`')
+             ->where('type', $nType)
              ->where('goods_id', $goods_id)            
              ->with([
-                'Spec' => function ($query) use ($nType) {
-                    $query->where('type', $nType);
-                },
+                'Spec',
                 'SkuValue' => function ($query) use ($goods_id) {
                     $query->where('goods_id', $goods_id)
                     ->field('id,goods_id,spec_id,spec_value_id')

+ 19 - 0
application/common/Service/User/UserShare.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace app\common\Service\User;
+
+use app\common\model\user\Share as ShareModel;
+
+class UserShare
+{
+      // 增加分享记录 
+      public static function addShare($userId = 0, $shareId = 0, $from = '', $platform = '')
+      {
+        $shareModel = new ShareModel();
+        $shareModel->user_id = $userId;
+        $shareModel->share_id = $shareId;
+        $shareModel->from = $from;
+        $shareModel->platform = $platform;
+        $shareModel->save();
+      }
+}

+ 40 - 0
application/common/model/user/Share.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace app\common\model\user;
+
+use think\Model;
+use traits\model\SoftDelete;
+
+class Share extends Model
+{
+
+    use SoftDelete;
+
+    
+
+    // 表名
+    protected $name = 'shop_user_share';
+    
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'integer';
+
+    // 定义时间戳字段名
+    protected $createTime = 'createtime';
+    protected $updateTime = 'updatetime';
+    protected $deleteTime = 'deletetime';
+
+    // 追加属性
+    protected $append = [
+
+    ];
+    
+
+    
+
+
+
+
+
+
+
+}

+ 2 - 3
application/common/service/SkuSpec.php

@@ -16,11 +16,10 @@ class SkuSpec
     {
         $list = (new SkuSpecModel())
              ->field('MIN(`id`) AS `id`, MIN(`goods_id`) AS `goods_id`, `spec_id`')
+             ->where('type', $nType)
              ->where('goods_id', $goods_id)            
              ->with([
-                'Spec' => function ($query) use ($nType) {
-                    $query->where('type', $nType);
-                },
+                'Spec',
                 'SkuValue' => function ($query) use ($goods_id) {
                     $query->where('goods_id', $goods_id)
                     ->field('id,goods_id,spec_id,spec_value_id')