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.
 
 
 
 
 
 

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