Product.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Loader ;
  5. use app\common\controller\Api;
  6. use app\common\model\ProductLists;
  7. use app\common\model\ProductTransfer;
  8. use app\common\model\ProductsModel;
  9. use app\common\model\ProductPopular;
  10. use app\api\logic\OrderLogic;
  11. class Product extends Api
  12. {
  13. protected array $noNeedLogin = ['getPopularList'];
  14. protected string $lan = '';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->lan = $this->request->getLan();
  19. }
  20. /**
  21. * 热销列表
  22. */
  23. public function getPopularList(ProductsModel $productsModel, ProductPopular $productPopular, ProductLists $productLists)
  24. {
  25. $item = $productsModel::getProductTypeAll($this->lan);
  26. $resp = array();
  27. foreach ($item as $kk =>$val) {
  28. $list= $productLists->where('type_id', $kk)->field('id,thum as img_url,'.$this->lan.'_name as name')->select();
  29. $i = 0;
  30. $proArr = array();
  31. foreach ($list as &$item) {
  32. $pro =$productPopular
  33. ->field('id,product_id,price,cost_price,stock,(num+init_num) as num,start_time,end_time,status')
  34. ->where('product_id', $item['id'])
  35. ->where('status', '<', $productPopular::Stop)
  36. ->order('start_time')
  37. ->find();
  38. if(!empty($pro)){
  39. $proArr[$i] = $item;
  40. $proArr[$i]['pro'] = $pro;
  41. $i+=1;
  42. }
  43. }
  44. $resp[] = ['type_id'=>$kk,'title'=>$val, 'product'=>$proArr];
  45. }
  46. $this->success('', $resp);
  47. }
  48. /**
  49. * 寄售转让列表
  50. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  51. * type_id 分类
  52. * key_val 商品名称搜索
  53. */
  54. public function getTransferList(ProductTransfer $productTransfer, OrderLogic $orderLogic)
  55. {
  56. $sort = $this->request->post('sort/d', '');
  57. $type_id = $this->request->post('type_id/s', '');
  58. $key_val = $this->request->post('key_val/s', '');
  59. $order = 'b.weigh desc';
  60. if($sort == 1) $order = 'price desc, a.product_id DESC';
  61. if($sort == 2) $order = 'price ASC, a.product_id ASC';
  62. $map['a.status'] = $productTransfer::Normal;
  63. if(!empty($type_id)) $map['b.type_id'] = $type_id;
  64. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  65. $list = $productTransfer
  66. ->alias('a')
  67. ->join("product_list b", "a.product_id = b.id", "left")
  68. ->group('a.product_id')
  69. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,min(a.price) as price,a.product_id,b.type_id,b.issue_num as issue')
  70. ->where($map)
  71. ->order($order)
  72. ->paginate($this->pageSize);
  73. $list = $this->getProductOrderList($list, $this->auth->id, true, $orderLogic);
  74. $this->success('', $list);
  75. }
  76. /**
  77. * 寄售转让收藏列表
  78. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  79. * type_id 分类
  80. * key_val 商品名称搜索
  81. */
  82. public function getTransferCollectList(ProductTransfer $productTransfer, OrderLogic $orderLogic)
  83. {
  84. $sort = $this->request->post('sort/d', '');
  85. $type_id = $this->request->post('type_id/s', '');
  86. $key_val = $this->request->post('key_val/s', '');
  87. $order = 'b.weigh desc';
  88. if($sort == 1) $order = 'price DESC, a.product_id DESC';
  89. if($sort == 2) $order = 'price ASC, a.product_id ASC';
  90. $map['a.status'] = $productTransfer::Normal;
  91. if(!empty($type_id)) $map['b.type_id'] = $type_id;
  92. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  93. $list = $productTransfer->alias('a')
  94. ->join("product_list b", "a.product_id = b.id", "left")
  95. ->join("user_collect c", "c.product_id = a.product_id and c.user_id = {$this->auth->id}")
  96. ->group('a.product_id')
  97. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,min(a.price) as price,a.product_id,b.type_id,b.issue_num as issue')
  98. ->where('a.status', $productTransfer::Normal)
  99. ->where($map)
  100. ->order($order)
  101. ->paginate($this->pageSize);
  102. //统计
  103. $list = $this->getProductOrderList($list, $this->auth->id, false, $orderLogic);
  104. $this->success('', $list);
  105. }
  106. /**
  107. * 寄售转让详情
  108. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  109. * type_id 分类
  110. * key_val 商品名称搜索
  111. */
  112. public function getTransferDetail(ProductLists $productLists, OrderLogic $orderLogic)
  113. {
  114. $ids = $this->request->post('ids/d', '');
  115. $list = $productLists->with('producttransfer,collect')
  116. ->where('product_list.id', $ids)
  117. ->where('product_list.status', $productLists::Normal)
  118. ->field('id,'.$this->lan .'_name as name,issue_num as issue,thum,details,min_buying_token as min_buy_price,max_buying_token as max_buy_price')
  119. ->find();
  120. if(empty($list)) $this->error('数据不存在');
  121. $list->circulation = $orderLogic::getProductCirculation($list->id); //流通: 所有用户的持有量
  122. $this->success('', $list);
  123. }
  124. //获取持有产品订单列表
  125. public function getHoldProductOrderList(OrderLogic $orderLogic)
  126. {
  127. $productId = $this->request->post('product_id/d', '');
  128. if(empty($productId)) $this->error('参数错误');
  129. $list = $orderLogic::getUserProductOrder($this->auth->id, $productId, $this->lan);
  130. $this->success('', $list);
  131. }
  132. private static function getProductOrderList(object $list, int $uid, bool $isCollect, OrderLogic $orderLogic): object
  133. {
  134. if($list){
  135. $userCollect = Loader::model('UserCollect');
  136. foreach ($list as &$item) {
  137. if($isCollect) $item['collect'] = $userCollect::where('user_id', $uid)->where('product_id', $item->product_id)->count();
  138. $item['circulation'] = $orderLogic::getProductCirculation($item->product_id); //流通: 所有用户的持有量
  139. }
  140. }
  141. return $list;
  142. }
  143. //获取分类
  144. public function getPopularType()
  145. {
  146. return $this->success('', ProductsModel::getProductTypeById($this->lan));
  147. }
  148. }