Home.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\AnnouncementModel;
  5. use app\common\model\ProductTransfer;
  6. use app\common\model\ProductOrder;
  7. use app\common\model\ProductPopular;
  8. use app\common\model\ProductArea;
  9. use app\common\model\ProductsModel;
  10. use app\common\model\Region;
  11. use app\common\model\UserModel;
  12. use Exception;
  13. /**
  14. * 首页接口
  15. */
  16. class Home extends Api
  17. {
  18. protected array $noNeedLogin = ['*'];
  19. protected string $lan = '';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->lan = $this->request->getLan();
  24. }
  25. /**
  26. * 首页基础数据
  27. */
  28. public function base(AnnouncementModel $announcement, ProductsModel $productsModel)
  29. {
  30. $resp = [
  31. 'system_name' => __("System name"),
  32. 'banner_list' => [],
  33. 'notice_list' => []
  34. ];
  35. // banner
  36. $resp['banner_list'] = $announcement
  37. ->field('id,title as title,img_url,createtime')
  38. ->where('type_id', $announcement::Banner)
  39. ->where('status', $announcement::Normal)
  40. ->where('to_lang', $this->lan)
  41. ->limit(0,10)
  42. ->order('id DESC,weigh desc')
  43. ->select();
  44. // 最新公告
  45. $resp['notice_list'] = $announcement
  46. ->field('id,title as title,img_url,createtime')
  47. ->where('type_id', $announcement::Announ)
  48. ->where('status', $announcement::Normal)
  49. ->where('to_lang', $this->lan)
  50. ->limit(0,10)
  51. ->order('id DESC,weigh desc')
  52. ->select();
  53. //分类
  54. $resp['type_list'] = $productsModel->where('status', $productsModel::Normal)
  55. ->field('id,'.$this->lan.'_title as title')
  56. ->order('sort desc')
  57. ->select();
  58. $this->success('', $resp);
  59. }
  60. /**
  61. * 首页数据
  62. */
  63. public function all(ProductPopular $productPopular, ProductTransfer $productTransfer)
  64. {
  65. //更新库存
  66. $productPopular::setUpdateStatus();
  67. $resp['product_list'] = $productPopular->with('productsList')
  68. ->where('product_popular.status', '<', $productPopular::Stop)
  69. ->order('weigh desc,start_time asc')->select(); //$this->pageSize)
  70. $this->success('', $resp);
  71. }
  72. /**
  73. * 热销详情
  74. * @param int $uid
  75. * @param string $fee
  76. * @param string $txHsh
  77. * @return void
  78. */
  79. public function getPopularInfo(ProductPopular $productPopular, ProductOrder $productOrder)
  80. {
  81. try {
  82. $ids = $this->request->post('ids/d', 0);
  83. $product_id = $this->request->post('product_id/d', 0);
  84. if(empty($ids)) throw new Exception(__("Parameter error"));
  85. $times = array();
  86. $order = array();
  87. //详情
  88. $info = $productPopular::getProductInfo($product_id, $ids, $this->lan);
  89. if(!empty($info)) {
  90. $times = $productPopular
  91. ->where('product_id', $info['product_id'])
  92. ->field('title,price, start_time, end_time, status')
  93. ->order('weigh desc')->limit(10)->select();
  94. }
  95. if(!empty($info)) {
  96. $order = $productOrder->alias('a')
  97. ->join("user u", "a.user_id = u.id", "left")
  98. ->where('order_id', $ids)->field('u.address, u.avatar, a.create_time')
  99. ->order('a.id desc')->limit(10)->select();
  100. }
  101. } catch (Exception $e) {
  102. $this->error( $e->getMessage());
  103. }
  104. $this->success('', compact('info', 'times', 'order'));
  105. }
  106. /**
  107. * 获取地址
  108. * @return void
  109. * @throws Exception
  110. */
  111. public function getProductAddres(ProductArea $productarea)
  112. {
  113. $search = $this->request->post('search/s', '');
  114. $pid = $this->request->post('product_id/d', 0);
  115. if(empty($pid)) $this->error(__("Parameter error"));
  116. $map = array();
  117. if(!empty($search)) $map['address'] = ['like', '%'.$search.'%'];
  118. $list = $productarea
  119. ->field('id,province,city,area,county,address')
  120. ->where('product_id', $pid)
  121. ->where('status', $productarea::Normal)
  122. ->where($map)
  123. ->paginate($this->pageSize);
  124. // 提交事务
  125. $this->success('', $list);
  126. }
  127. }