Product.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductsModel;
  5. use app\common\model\ProductPopular;
  6. use app\common\model\ProductTransfer;
  7. class Product extends Api
  8. {
  9. protected array $noNeedLogin = ['*'];
  10. protected string $lan = '';
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->lan = $this->request->getLan();
  15. }
  16. /**
  17. * 热销列表
  18. */
  19. public function getPopularList(ProductsModel $productsModel, ProductPopular $productPopular)
  20. {
  21. $list = $productsModel->where('status', 1)->column('id,'.$this->lan.'_title as title');
  22. $resp = array();
  23. foreach ($list as $kk =>$val) {
  24. $pro = $productPopular->alias('a')
  25. ->join("product_list b", "a.product_id = b.id", "left")
  26. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,price,cost_price,stock,num,start_time,end_time')
  27. ->where('a.status', '<', $productPopular::STOP)
  28. ->where('b.type_id', $kk)
  29. ->order('a.weigh desc')
  30. ->select();
  31. $resp[] = ['type_id'=>$kk,'title'=>$val, 'product'=>$pro];
  32. }
  33. $this->success('', $resp);
  34. }
  35. /**
  36. * 转让列表
  37. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  38. * type_id 分类
  39. * key_val 商品名称搜索
  40. */
  41. public function getTransferList(ProductTransfer $productTransfer)
  42. {
  43. $sort = $this->request->post('sort/d', '');
  44. $type_id = $this->request->post('type_id/s', '');
  45. $key_val = $this->request->post('key_val/s', '');
  46. $order = 'a.id DESC';
  47. $map = [];
  48. if($sort == 1) $order = 'a.price DESC';
  49. if($sort == 2) $order = 'a.price ASC';
  50. if($type_id > 0) $map['b.type_id'] = ['=', $type_id];
  51. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  52. $list = $productTransfer->alias('a')
  53. ->join("product_list b", "a.product_id = b.id", "left")
  54. ->join("user u", "a.user_id = u.id", "left")
  55. ->join("products d", "b.type_id = d.id", "left")
  56. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,price,u.nickname,d.'.$this->lan.'_title as title')
  57. ->where('a.status', $productTransfer::NORMAL)
  58. ->where($map)
  59. ->order($order)
  60. ->paginate($this->pageSize);
  61. $this->success('', $list);
  62. }
  63. }