Product.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = 'price ASC';
  60. if($sort == 1) $order = 'price desc';
  61. $map['a.status'] = $productTransfer::Normal;
  62. if(!empty($type_id)) $map['a.type_id'] = $type_id;
  63. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  64. $list = $productTransfer
  65. ->alias('a')
  66. ->join("product_list b", "a.product_id = b.id", "left")
  67. ->group('a.product_id')
  68. ->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')
  69. ->where($map)
  70. ->order($order)
  71. ->paginate($this->pageSize);
  72. $list = $this->getProductOrderList($list, $this->auth->id, true, $orderLogic);
  73. $this->success('', $list);
  74. }
  75. /**
  76. * 寄售转让收藏列表
  77. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  78. * type_id 分类
  79. * key_val 商品名称搜索
  80. */
  81. public function getTransferCollectList(ProductTransfer $productTransfer, OrderLogic $orderLogic)
  82. {
  83. $sort = $this->request->post('sort/d', '');
  84. $type_id = $this->request->post('type_id/s', '');
  85. $key_val = $this->request->post('key_val/s', '');
  86. $order = 'b.weigh desc';
  87. if($sort == 1) $order = 'price DESC';
  88. if($sort == 2) $order = 'price ASC';
  89. $map['a.status'] = $productTransfer::Normal;
  90. if(!empty($type_id)) $map['a.type_id'] = $type_id;
  91. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  92. $list = $productTransfer->alias('a')
  93. ->join("product_list b", "a.product_id = b.id", "left")
  94. ->join("user_collect c", "c.product_id = a.product_id and c.user_id = {$this->auth->id}")
  95. ->group('a.product_id')
  96. ->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')
  97. ->where('a.status', $productTransfer::Normal)
  98. ->where($map)
  99. ->order($order)
  100. ->paginate($this->pageSize);
  101. $list = $this->getProductOrderList($list, $this->auth->id, false, $orderLogic);
  102. $this->success('', $list);
  103. }
  104. /**
  105. * 寄售转让详情
  106. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  107. * type_id 分类
  108. * key_val 商品名称搜索
  109. */
  110. public function getTransferDetail(ProductLists $productLists, OrderLogic $orderLogic)
  111. {
  112. $ids = $this->request->post('ids/d', '');
  113. $list = $productLists->with('producttransfer,collect')
  114. ->where('product_list.id', $ids)
  115. ->where('product_list.status', $productLists::Normal)
  116. ->field('id,'.$this->lan .'_name as name,thum,details')
  117. ->find();
  118. if(empty($list)) $this->error('数据不存在');
  119. $list->issue = $orderLogic::getProductIssue($list->id); //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的
  120. $list->circulation = $orderLogic::getProductCirculation($list->id); //流通: 所有用户的持有量
  121. $this->success('', $list);
  122. }
  123. //获取持有产品订单列表
  124. public function getHoldProductOrderList(OrderLogic $orderLogic)
  125. {
  126. $productId = $this->request->post('product_id/d', '');
  127. if(empty($productId)) $this->error('参数错误');
  128. $list = $orderLogic::getUserProductOrder($this->auth->id, $productId, $this->lan);
  129. $this->success('', $list);
  130. }
  131. private static function getProductOrderList(object $list, int $uid, bool $isCollect, OrderLogic $orderLogic): object
  132. {
  133. if($list){
  134. $userCollect = Loader::model('UserCollect');
  135. foreach ($list as &$item) {
  136. if($isCollect) $item['collect'] = $userCollect::where('user_id', $uid)->where('product_id', $item->product_id)->count();
  137. $item['issue'] = $orderLogic::getProductIssue($item->product_id); //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的
  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. }