Product.php 2.4 KB

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