作者 景龙
1 个管道 的构建 通过 耗费 0 秒

修改浏览记录接口

  1 +<?php
  2 +
  3 +namespace app\admin\model;
  4 +
  5 +use think\Model;
  6 +
  7 +class Browse extends Model
  8 +{
  9 + // 表名
  10 + protected $name = 'browse';
  11 +
  12 + // 自动写入时间戳字段
  13 + protected $autoWriteTimestamp = 'int';
  14 +
  15 + // 定义时间戳字段名
  16 + protected $createTime = 'createtime';
  17 + protected $updateTime = 'updatetime';
  18 +
  19 + // 追加属性
  20 + protected $append = [
  21 + 'deletetime_text'
  22 + ];
  23 +
  24 +
  25 +
  26 +
  27 +
  28 +
  29 + public function getDeletetimeTextAttr($value, $data)
  30 + {
  31 + $value = $value ? $value : (isset($data['deletetime']) ? $data['deletetime'] : '');
  32 + return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  33 + }
  34 +
  35 + protected function setDeletetimeAttr($value)
  36 + {
  37 + return $value && !is_numeric($value) ? strtotime($value) : $value;
  38 + }
  39 +
  40 +
  41 +}
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\api\controller; 3 namespace app\api\controller;
4 4
  5 +use app\admin\model\Browse;
5 use app\common\controller\Api; 6 use app\common\controller\Api;
6 use app\admin\model\Search; 7 use app\admin\model\Search;
7 use think\Db; 8 use think\Db;
@@ -253,6 +254,7 @@ class Goods extends Api @@ -253,6 +254,7 @@ class Goods extends Api
253 foreach($data as &$value){ 254 foreach($data as &$value){
254 $value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']); 255 $value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
255 } 256 }
  257 + $this->broGoods($goods_id);
256 $this->success('成功', ['data'=>$data,'comment_count'=>$comment_count]); 258 $this->success('成功', ['data'=>$data,'comment_count'=>$comment_count]);
257 }else{ 259 }else{
258 $this->error('请求方式错误'); 260 $this->error('请求方式错误');
@@ -294,28 +296,53 @@ class Goods extends Api @@ -294,28 +296,53 @@ class Goods extends Api
294 public function guessLikes(){ 296 public function guessLikes(){
295 if($this->request->isGet()){ 297 if($this->request->isGet()){
296 //随机取出1条检索的历史 298 //随机取出1条检索的历史
297 - $keywords = Db::table('gc_search')  
298 - ->where(['uid'=>$this->user_id,'is_search'=>$this->is_search[0]])  
299 - ->field('keywords')  
300 - ->orderRaw('rand()')  
301 - ->limit(1)  
302 - ->find(); 299 + $bro_num = Db::table('gc_browse')
  300 + ->where(['uid'=>$this->user_id])
  301 + ->order('bro_num desc')
  302 + ->select();
303 $where['p.status'] = $this->normal; 303 $where['p.status'] = $this->normal;
304 $where['u.status'] = 'normal'; 304 $where['u.status'] = 'normal';
305 - if($keywords){  
306 - $where['p.title'] = ['like','%'.$keywords['keywords'].'%']; 305 + if($bro_num){
  306 + $g_Ids = array_column($bro_num,'g_id');
  307 + $data = Db::table('gc_product')
  308 + ->alias('p')
  309 + ->join('gc_user u','p.uid = u.id','LEFT')
  310 + ->where($where)
  311 + ->whereIn('p.id',$g_Ids)
  312 + ->field('p.id,p.title,p.price,p.images')
  313 + ->limit(20)
  314 + ->order('p.id desc')
  315 + ->select();
  316 + }else{
  317 + $data = Db::table('gc_product')
  318 + ->alias('p')
  319 + ->join('gc_user u','p.uid = u.id','LEFT')
  320 + ->where($where)
  321 + ->field('p.id,p.title,p.price,p.images')
  322 + ->limit(20)
  323 + ->order('p.id desc')
  324 + ->select();
307 } 325 }
308 - $data = Db::table('gc_product')  
309 - ->alias('p')  
310 - ->join('gc_user u','p.uid = u.id','LEFT')  
311 - ->where($where)  
312 - ->field('p.id,p.title,p.price,p.images')  
313 - ->limit(20)  
314 - ->order('p.id desc')  
315 - ->select();  
316 $this->success('成功', $data); 326 $this->success('成功', $data);
317 }else{ 327 }else{
318 $this->error('请求方式错误'); 328 $this->error('请求方式错误');
319 } 329 }
320 } 330 }
  331 +
  332 + //浏览记录表
  333 + private function broGoods($id){
  334 + $broModel = new Browse();
  335 + $bro_num = Db::table('gc_browse')
  336 + ->where(['uid'=>$this->user_id,'g_id'=>$id])
  337 + ->find();
  338 + $data['g_id'] = $id;
  339 + $data['uid'] = $this->user_id;
  340 + if($bro_num){
  341 + $data['bro_num'] = $bro_num['bro_num']+1;
  342 + $broModel->where(['g_id'=>$id,'uid'=>$this->user_id])->update($data);
  343 + }else{
  344 + $data['bro_num'] = 1;
  345 + $broModel::create($data);
  346 + }
  347 + }
321 } 348 }