xieruidong 2 年之前
父节点
当前提交
768482aa71

+ 22 - 0
application/api/controller/mall/Goods.php

@@ -5,6 +5,7 @@ namespace app\api\controller\mall;
 use app\common\controller\Api;
 use app\common\model\Category;
 use app\common\model\GoodsSku;
+use app\common\model\UserCategoryView;
 use app\common\validate\KillService;
 
 /**
@@ -160,4 +161,25 @@ class Goods extends Api
         }
         $this->success('',$info);
     }
+    /**
+     * 记录用户浏览产品时长
+     * @ApiParams (name=goods_id,description=商品ID)
+     * @ApiParams (name=sec,description=时长S)
+     */
+    public function ls(){
+        $user=$this->auth->getUser();
+        $data=input();
+        if(empty($data['id'])){
+            $this->success();
+        }
+        $goods=\app\common\model\Goods::where('id',$data['id'])->find();
+        if(!$goods){
+            $this->success();
+        }
+        $category=$goods->category;
+        if(!$category){
+            $this->success();
+        }
+        UserCategoryView::addSecond($user,$category,$data['sec']??0);
+    }
 }

+ 3 - 0
application/common/model/User.php

@@ -244,6 +244,9 @@ class User extends Model
     public function coupon(){
         return $this->hasMany(UserCoupon::class);
     }
+    public function categoryView(){
+        return $this->hasMany(UserCategoryView::class);
+    }
     public static function recharge($params,Payment $payment){
         self::money($payment['amount'],$payment['uer_id'],MoneyLog::TYPE_CHARGE,'充值');
     }

+ 25 - 0
application/common/model/UserCategoryView.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+
+class UserCategoryView extends Model
+{
+    public static function addSecond(User $user,Category $category,$seconds){
+        if(!is_numeric($seconds)||$seconds<=0){
+            return;
+        }
+        $has=$user->categoryView()->where('category_id',$category['id'])->find();
+        if($has){
+            $has->setInc('seconds',$seconds);
+        }else{
+            $user->categoryView()->save([
+                'category_id'=>$category['id'],
+                'category_name'=>$category['name'],
+                'seconds'=>$seconds,
+            ]);
+        }
+    }
+}