You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

219 lines
7.4 KiB

  1. <?php
  2. namespace addons\unishop\controller;
  3. use addons\unishop\extend\Hashids;
  4. use addons\unishop\model\Config;
  5. use addons\unishop\model\Evaluate;
  6. use addons\unishop\model\Favorite;
  7. use addons\unishop\model\Product as productModel;
  8. use addons\unishop\model\Coupon;
  9. use think\Exception;
  10. class Product extends Base
  11. {
  12. protected $noNeedLogin = ['detail', 'lists'];
  13. /**
  14. * 获取产品数据
  15. */
  16. public function detail()
  17. {
  18. $productId = $this->request->get('id');
  19. if (!is_numeric($productId)){
  20. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  21. }
  22. try {
  23. $productModel = new productModel();
  24. $data = $productModel->where(['id' => $productId])->cache(10)->find();
  25. if (!$data) {
  26. $this->error(__('Goods not exist'));
  27. }
  28. if ($data['switch'] == productModel::SWITCH_OFF) {
  29. $this->error(__('Goods are off the shelves'));
  30. }
  31. // 真实浏览量加一
  32. $data->real_look++;
  33. $data->look++;
  34. $data->save();
  35. //服务
  36. $server = explode(',', $data->server);
  37. $configServer = json_decode(Config::getByName('server')['value'],true);
  38. $serverValue = [];
  39. foreach ($server as $k => $v) {
  40. if (isset($configServer[$v])) {
  41. $serverValue[] = $configServer[$v];
  42. }
  43. }
  44. $data->server = count($serverValue) ? implode(' · ', $serverValue) : '';
  45. // 默认没有收藏
  46. $data->favorite = false;
  47. // 评价
  48. $data['evaluate_data'] = (new Evaluate)->where(['product_id' => $productId])
  49. ->field('COUNT(*) as count, IFNULL(CEIL(AVG(rate)/5*100),0) as avg')
  50. ->cache(10)->find();
  51. //优惠券
  52. $data->coupon = (new Coupon)->where('endtime', '>', time())
  53. ->where(['switch' => Coupon::SWITCH_ON])->cache(10)->order('weigh DESC')->select();
  54. // 是否已收藏
  55. if ($this->auth->id) {
  56. $data->favorite = (new Favorite)->where(['user_id' => $this->auth->id, 'product_id' => $productId])->count();
  57. }
  58. // 购物车数量
  59. $data->cart_num = (new \addons\unishop\model\Cart)->where(['user_id' => $this->auth->id])->count();
  60. // 评价信息
  61. $evaluate = (new Evaluate)->alias('e')
  62. ->join('user u', 'e.user_id = u.id')
  63. ->where(['e.product_id' => $productId, 'toptime' => ['>', Evaluate::TOP_OFF]])
  64. ->field('u.username,u.avatar,e.*')
  65. ->order(['toptime' => 'desc', 'createtime' => 'desc'])->select();
  66. if ($evaluate) {
  67. $data->evaluate_list = collection($evaluate)->append(['createtime_text'])->toArray();
  68. }
  69. $data = $data->append(['images_text', "detail_images_text",'spec_list', 'spec_table_list'])->toArray();
  70. $this->success('', $data);
  71. } catch (Exception $e) {
  72. $this->error($e->getMessage());
  73. }
  74. }
  75. /**
  76. * 产品列表
  77. * 注:这里后期需要做缓存
  78. */
  79. public function lists()
  80. {
  81. $page = $this->request->get('page', 1);
  82. $pagesize = $this->request->get('pagesize', 20);
  83. $by = $this->request->get('by', 'weigh');
  84. $desc = $this->request->get('desc', 'desc');
  85. $sid = $this->request->get('sid'); // 二级分类Id
  86. $fid = $this->request->get('fid'); // 一级分类Id
  87. $productModel = new productModel();
  88. if ($fid && !$sid) {
  89. $categoryModel = new \addons\unishop\model\Category();
  90. $sArr = $categoryModel->where('pid', $fid)->field('id')->select();
  91. $sArr = array_column($sArr, 'id');
  92. array_push($sArr, $fid);
  93. $productModel->where('category_id', 'in', $sArr);
  94. } else {
  95. $sid && $productModel->where(['category_id' => $sid]);
  96. }
  97. $result = $productModel
  98. ->where(['switch' => productModel::SWITCH_ON])
  99. ->page($page, $pagesize)
  100. ->order($by, $desc)
  101. ->field('id,title,image,sales_price,sales,real_sales')
  102. ->select();
  103. if ($result) {
  104. $result = collection($result)->toArray();
  105. $this->success('', $result);
  106. } else {
  107. $this->error('没有更多数据');
  108. }
  109. }
  110. /**
  111. * 收藏
  112. * @param int $id 产品id
  113. */
  114. public function favorite()
  115. {
  116. $id = $this->request->get('id', 0);
  117. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  118. $user_id = $this->auth->id;
  119. $favoriteModel = Favorite::get(function ($query) use ($id, $user_id) {
  120. $query->where(['user_id' => $user_id, 'product_id' => $id]);
  121. });
  122. if ($favoriteModel) {
  123. Favorite::destroy($favoriteModel->id);
  124. } else {
  125. $product = productModel::withTrashed()->where(['id' => $id, 'switch' => productModel::SWITCH_ON])->find();
  126. if (!$product) {
  127. $this->error('参数错误');
  128. }
  129. $favoriteModel = new Favorite();
  130. $favoriteModel->user_id = $user_id;
  131. $favoriteModel->product_id = $id;
  132. $product = $product->getData();
  133. $data['image'] = $product['image'];
  134. $data['market_price'] = $product['market_price'];
  135. $data['product_id'] = Hashids::encodeHex($product['id']);
  136. $data['sales_price'] = $product['sales_price'];
  137. $data['title'] = $product['title'];
  138. $favoriteModel->snapshot = json_encode($data);
  139. $favoriteModel->save();
  140. }
  141. $this->success('', true);
  142. }
  143. /**
  144. * 收藏列表
  145. */
  146. public function favoriteList()
  147. {
  148. $page = $this->request->get('page', 1);
  149. $pageSize = $this->request->get('pagesize', 20);
  150. $list = (new Favorite)->where(['user_id' => $this->auth->id])->with(['product'])->page($page, $pageSize)->select();
  151. $list = collection($list)->toArray();
  152. foreach ($list as &$item) {
  153. if (!empty($item['product'])) {
  154. $item['status'] = 1;
  155. } else {
  156. $item['status'] = 0;
  157. $item['product'] = json_decode($item['snapshot'],true);
  158. $image = $item['product']['image'];
  159. $item['product']['image'] = Config::getImagesFullUrl($image);
  160. }
  161. unset($item['snapshot']);
  162. }
  163. $this->success('', $list);
  164. }
  165. /**
  166. * 商品评论
  167. */
  168. public function evaluate()
  169. {
  170. $page = $this->request->get('page', 1);
  171. $pageSize = $this->request->get('pagesize', 20);
  172. $productId = $this->request->get('product_id');
  173. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  174. // 评价信息
  175. $evaluate = (new Evaluate)->alias('e')
  176. ->join('user u', 'e.user_id = u.id')
  177. ->where(['e.product_id' => $productId])
  178. ->field('u.username,u.avatar,e.*')
  179. ->order(['toptime' => 'desc', 'createtime' => 'desc'])
  180. ->page($page, $pageSize)
  181. ->select();
  182. if ($evaluate) {
  183. $evaluate = collection($evaluate)->append(['createtime_text'])->toArray();
  184. }
  185. $this->success('', $evaluate);
  186. }
  187. }