Usercenter.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 用户中心
  7. */
  8. class Usercenter extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //我的搜索历史
  13. public function search_history(){
  14. $list = Db::name('user_search')->where('user_id',$this->auth->id)->order('id desc')->select();
  15. if(!empty($list) && count($list) > 10){
  16. $list = array_chunk($list,10);
  17. $list = $list[0];
  18. $rs_ids = array_column($list,'id');
  19. Db::name('user_search')->where('user_id',$this->auth->id)->where('id','NOT IN',$rs_ids)->delete();
  20. }
  21. $this->success(1,$list);
  22. }
  23. //清空搜索历史
  24. public function search_clear(){
  25. Db::name('user_search')->where('user_id',$this->auth->id)->delete();
  26. $this->success(1);
  27. }
  28. //我的收藏列表
  29. public function my_collect()
  30. {
  31. $where = [
  32. 'p.is_show' => 1,
  33. 'c.user_id' => $this->auth->id,
  34. ];
  35. $list = Db::name('user_collect')->alias('c')
  36. ->field('p.id,p.name,p.image,p.view_number,p.price,c.createtime')
  37. ->join('product p','c.product_id = p.id','LEFT')
  38. ->where($where)
  39. ->order('c.id desc')
  40. ->autopage()->select();
  41. $list = list_domain_image($list,['image']);
  42. $this->success(1,$list);
  43. }
  44. //关注某人
  45. public function collect_one(){
  46. $product_id = input('product_id',0);
  47. if(!$product_id){
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $map = [
  51. 'user_id' => $this->auth->id,
  52. 'product_id' => $product_id,
  53. ];
  54. $check = Db::name('user_collect')->where($map)->find();
  55. if($check){
  56. //取关
  57. $rs = Db::name('user_collect')->where($map)->delete();
  58. $this->success('操作成功');
  59. }
  60. $map['createtime'] = time();
  61. $id = Db::name('user_collect')->insertGetId($map);
  62. $this->success('操作成功');
  63. }
  64. }