Product.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $list= $productLists->where('type_id', $kk)->field('id,thum as img_url,'.$this->lan.'_name as name')->select();
  28. foreach ($list as $key => &$item) {
  29. $pro =$productPopular
  30. ->field('id,product_id,price,cost_price,stock,num,start_time,end_time,status')
  31. ->where('product_id', $item->id)
  32. ->where('status', '<', $productPopular::STOP)
  33. ->order('start_time')
  34. ->find();
  35. if(!empty($pro)){
  36. $item['pro'] = $pro;
  37. }else{
  38. unset($list[$key]);
  39. }
  40. }
  41. $resp[] = ['type_id'=>$kk,'title'=>$val, 'product'=>$list];
  42. }
  43. $this->success('', $resp);
  44. }
  45. /**
  46. * 转让列表
  47. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  48. * type_id 分类
  49. * key_val 商品名称搜索
  50. */
  51. public function getTransferList(ProductTransfer $productTransfer)
  52. {
  53. $sort = $this->request->post('sort/d', '');
  54. $type_id = $this->request->post('type_id/s', '');
  55. $key_val = $this->request->post('key_val/s', '');
  56. $order = 'a.id DESC';
  57. $map = [];
  58. if($sort == 1) $order = 'a.price DESC';
  59. if($sort == 2) $order = 'a.price ASC';
  60. if($type_id > 0) $map['b.type_id'] = ['=', $type_id];
  61. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  62. $list = $productTransfer->alias('a')
  63. ->join("product_list b", "a.product_id = b.id", "left")
  64. ->join("user u", "a.user_id = u.id", "left")
  65. ->join("products d", "b.type_id = d.id", "left")
  66. ->join("product_area dr", "dr.id = a.area_id", "left") //地区
  67. ->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')
  68. ->where('a.status', $productTransfer::NORMAL)
  69. ->where($map)
  70. ->order($order)
  71. ->paginate($this->pageSize);
  72. $this->success('', $list);
  73. }
  74. }