Product.php 2.7 KB

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