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.
 
 
 
 
 
 

221 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. print_r($data);
  71. die();
  72. $this->success('', $data);
  73. } catch (Exception $e) {
  74. $this->error($e->getMessage());
  75. }
  76. }
  77. /**
  78. * 产品列表
  79. * 注:这里后期需要做缓存
  80. */
  81. public function lists()
  82. {
  83. $page = $this->request->get('page', 1);
  84. $pagesize = $this->request->get('pagesize', 20);
  85. $by = $this->request->get('by', 'weigh');
  86. $desc = $this->request->get('desc', 'desc');
  87. $sid = $this->request->get('sid'); // 二级分类Id
  88. $fid = $this->request->get('fid'); // 一级分类Id
  89. $productModel = new productModel();
  90. if ($fid && !$sid) {
  91. $categoryModel = new \addons\unishop\model\Category();
  92. $sArr = $categoryModel->where('pid', $fid)->field('id')->select();
  93. $sArr = array_column($sArr, 'id');
  94. array_push($sArr, $fid);
  95. $productModel->where('category_id', 'in', $sArr);
  96. } else {
  97. $sid && $productModel->where(['category_id' => $sid]);
  98. }
  99. $result = $productModel
  100. ->where(['switch' => productModel::SWITCH_ON])
  101. ->page($page, $pagesize)
  102. ->order($by, $desc)
  103. ->field('id,title,image,sales_price,sales,real_sales')
  104. ->select();
  105. if ($result) {
  106. $result = collection($result)->toArray();
  107. $this->success('', $result);
  108. } else {
  109. $this->error('没有更多数据');
  110. }
  111. }
  112. /**
  113. * 收藏
  114. * @param int $id 产品id
  115. */
  116. public function favorite()
  117. {
  118. $id = $this->request->get('id', 0);
  119. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  120. $user_id = $this->auth->id;
  121. $favoriteModel = Favorite::get(function ($query) use ($id, $user_id) {
  122. $query->where(['user_id' => $user_id, 'product_id' => $id]);
  123. });
  124. if ($favoriteModel) {
  125. Favorite::destroy($favoriteModel->id);
  126. } else {
  127. $product = productModel::withTrashed()->where(['id' => $id, 'switch' => productModel::SWITCH_ON])->find();
  128. if (!$product) {
  129. $this->error('参数错误');
  130. }
  131. $favoriteModel = new Favorite();
  132. $favoriteModel->user_id = $user_id;
  133. $favoriteModel->product_id = $id;
  134. $product = $product->getData();
  135. $data['image'] = $product['image'];
  136. $data['market_price'] = $product['market_price'];
  137. $data['product_id'] = Hashids::encodeHex($product['id']);
  138. $data['sales_price'] = $product['sales_price'];
  139. $data['title'] = $product['title'];
  140. $favoriteModel->snapshot = json_encode($data);
  141. $favoriteModel->save();
  142. }
  143. $this->success('', true);
  144. }
  145. /**
  146. * 收藏列表
  147. */
  148. public function favoriteList()
  149. {
  150. $page = $this->request->get('page', 1);
  151. $pageSize = $this->request->get('pagesize', 20);
  152. $list = (new Favorite)->where(['user_id' => $this->auth->id])->with(['product'])->page($page, $pageSize)->select();
  153. $list = collection($list)->toArray();
  154. foreach ($list as &$item) {
  155. if (!empty($item['product'])) {
  156. $item['status'] = 1;
  157. } else {
  158. $item['status'] = 0;
  159. $item['product'] = json_decode($item['snapshot'],true);
  160. $image = $item['product']['image'];
  161. $item['product']['image'] = Config::getImagesFullUrl($image);
  162. }
  163. unset($item['snapshot']);
  164. }
  165. $this->success('', $list);
  166. }
  167. /**
  168. * 商品评论
  169. */
  170. public function evaluate()
  171. {
  172. $page = $this->request->get('page', 1);
  173. $pageSize = $this->request->get('pagesize', 20);
  174. $productId = $this->request->get('product_id');
  175. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  176. // 评价信息
  177. $evaluate = (new Evaluate)->alias('e')
  178. ->join('user u', 'e.user_id = u.id')
  179. ->where(['e.product_id' => $productId])
  180. ->field('u.username,u.avatar,e.*')
  181. ->order(['toptime' => 'desc', 'createtime' => 'desc'])
  182. ->page($page, $pageSize)
  183. ->select();
  184. if ($evaluate) {
  185. $evaluate = collection($evaluate)->append(['createtime_text'])->toArray();
  186. }
  187. $this->success('', $evaluate);
  188. }
  189. }