Home.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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,'.$this->lan.'_title as title,img_url,createtime')
  38. ->where('type_id', $announcement::Banner)
  39. ->where('status', $announcement::NORMAL)
  40. ->limit(0,10)
  41. ->order('id DESC,weigh desc')
  42. ->select();
  43. // 最新公告
  44. $resp['notice_list'] = $announcement
  45. ->field('id,'.$this->lan.'_title as title,img_url,createtime')
  46. ->where('type_id', $announcement::Announ)
  47. ->where('status', $announcement::NORMAL)
  48. ->limit(0,10)
  49. ->order('id DESC,weigh desc')
  50. ->select();
  51. //分类
  52. $resp['type_list'] = $productsModel->where('status', $productsModel::NORMAL)
  53. ->field('id,'.$this->lan.'_title as title')
  54. ->order('sort desc')
  55. ->select();
  56. $this->success('', $resp);
  57. }
  58. /**
  59. * 首页数据
  60. */
  61. public function all(ProductPopular $productPopular, ProductTransfer $productTransfer)
  62. {
  63. //更新库存
  64. $productPopular::setUpdateStatus();
  65. // 热销
  66. $resp['product_list'] = $productPopular->with('productsList')->where('product_popular.status', '<', $productPopular::STOP)->limit(2)
  67. ->order('start_time asc,weigh desc')->select();
  68. // 转让
  69. $resp['transfer_list'] = $productTransfer->alias('a')
  70. ->join("product_list b", "a.product_id = b.id", "left")
  71. ->join("user u", "a.user_id = u.id", "left")
  72. ->join("products d", "b.type_id = d.id", "left")
  73. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,price,u.nickname,d.'.$this->lan.'_title as title')
  74. ->where('a.status', $productTransfer::NORMAL)
  75. ->limit(2)
  76. ->order('a.id DESC')
  77. ->select();
  78. //抵押
  79. $resp['mortgage_list'] = array();
  80. $this->success('', $resp);
  81. }
  82. /**
  83. * 热销详情
  84. * @param int $uid
  85. * @param string $fee
  86. * @param string $txHsh
  87. * @return void
  88. */
  89. public function getPopularInfo(ProductPopular $productPopular, ProductOrder $productOrder)
  90. {
  91. try {
  92. $ids = $this->request->post('ids/d', 0);
  93. $product_id = $this->request->post('product_id/d', 0);
  94. if(empty($ids)) throw new Exception(__("Parameter error"));
  95. $times = array();
  96. $order = array();
  97. //详情
  98. $info = $productPopular::getProductInfo($product_id, $ids, $this->lan);
  99. if(!empty($info)) {
  100. $times = $productPopular
  101. ->where('product_id', $info['product_id'])
  102. ->field('title,price, start_time, end_time, status')
  103. ->order('weigh desc')->limit(10)->select();
  104. }
  105. if(!empty($info)) {
  106. $order = $productOrder->alias('a')
  107. ->join("user u", "a.user_id = u.id", "left")
  108. ->where('order_id', $ids)->field('u.address, u.avatar, a.create_time')
  109. ->order('a.id desc')->limit(10)->select();
  110. }
  111. } catch (Exception $e) {
  112. $this->error( $e->getMessage());
  113. }
  114. $this->success('', compact('info', 'times', 'order'));
  115. }
  116. /**
  117. * 转让详情
  118. * @param int $uid
  119. * @param string $fee
  120. * @param string $txHsh
  121. * @return void
  122. */
  123. public function getTransferInfo(ProductTransfer $productTransfer)
  124. {
  125. try {
  126. $ids = $this->request->post('ids/d', 0);
  127. if(empty($ids)) throw new Exception(__("Parameter error"));
  128. $info = $productTransfer->alias('a')
  129. ->join("product_list b", "a.product_id = b.id", "left")
  130. ->join("product_area d", "d.id = a.area_id", "left") //地区
  131. ->field('a.id,a.product_id,a.area_id,'.'b.'.$this->lan.'_name as name,b.images as img_url,price,fees,b.details,d.address')
  132. ->where('a.id', $ids)
  133. ->find();
  134. } catch (Exception $e) {
  135. $this->error( $e->getMessage());
  136. }
  137. $this->success('', $info);
  138. }
  139. /**
  140. * 获取地址
  141. * @return void
  142. * @throws Exception
  143. */
  144. public function getProductAddres(ProductArea $productarea)
  145. {
  146. $search = $this->request->post('search/s', '');
  147. $pid = $this->request->post('product_id/d', 0);
  148. if(empty($pid)) $this->error(__("Parameter error"));
  149. $map = array();
  150. if(!empty($search)) $map['address'] = ['like', '%'.$search.'%'];
  151. $list = $productarea
  152. ->field('id,province,city,area,county,address')
  153. ->where('product_id', $pid)
  154. ->where('status', $productarea::NORMAL)
  155. ->where($map)
  156. ->paginate($this->pageSize);
  157. // 提交事务
  158. $this->success('', $list);
  159. }
  160. }