Product.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. $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. // 新增访问数据
  102. $datamodel = new \addons\unishop\model\Data();
  103. $datamodel->where(["id"=>1])->setInc("view");
  104. if ($result) {
  105. $result = collection($result)->toArray();
  106. $this->success('', $result);
  107. } else {
  108. $this->error('没有更多数据');
  109. }
  110. }
  111. /**
  112. * 收藏
  113. * @param int $id 产品id
  114. */
  115. public function favorite()
  116. {
  117. $id = $this->request->get('id', 0);
  118. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  119. $user_id = $this->auth->id;
  120. $favoriteModel = Favorite::get(function ($query) use ($id, $user_id) {
  121. $query->where(['user_id' => $user_id, 'product_id' => $id]);
  122. });
  123. if ($favoriteModel) {
  124. Favorite::destroy($favoriteModel->id);
  125. } else {
  126. $product = productModel::withTrashed()->where(['id' => $id, 'switch' => productModel::SWITCH_ON])->find();
  127. if (!$product) {
  128. $this->error('参数错误');
  129. }
  130. $favoriteModel = new Favorite();
  131. $favoriteModel->user_id = $user_id;
  132. $favoriteModel->product_id = $id;
  133. $product = $product->getData();
  134. $data['image'] = $product['image'];
  135. $data['market_price'] = $product['market_price'];
  136. $data['product_id'] = Hashids::encodeHex($product['id']);
  137. $data['sales_price'] = $product['sales_price'];
  138. $data['title'] = $product['title'];
  139. $favoriteModel->snapshot = json_encode($data);
  140. $favoriteModel->save();
  141. }
  142. $this->success('', true);
  143. }
  144. /**
  145. * 收藏列表
  146. */
  147. public function favoriteList()
  148. {
  149. $page = $this->request->get('page', 1);
  150. $pageSize = $this->request->get('pagesize', 20);
  151. $list = (new Favorite)->where(['user_id' => $this->auth->id])->with(['product'])->page($page, $pageSize)->select();
  152. $list = collection($list)->toArray();
  153. foreach ($list as &$item) {
  154. if (!empty($item['product'])) {
  155. $item['status'] = 1;
  156. } else {
  157. $item['status'] = 0;
  158. $item['product'] = json_decode($item['snapshot'],true);
  159. $image = $item['product']['image'];
  160. $item['product']['image'] = Config::getImagesFullUrl($image);
  161. }
  162. unset($item['snapshot']);
  163. }
  164. $this->success('', $list);
  165. }
  166. /**
  167. * 商品评论
  168. */
  169. public function evaluate()
  170. {
  171. $page = $this->request->get('page', 1);
  172. $pageSize = $this->request->get('pagesize', 20);
  173. $productId = $this->request->get('product_id');
  174. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  175. // 评价信息
  176. $evaluate = (new Evaluate)->alias('e')
  177. ->join('user u', 'e.user_id = u.id')
  178. ->where(['e.product_id' => $productId])
  179. ->field('u.username,u.avatar,e.*')
  180. ->order(['toptime' => 'desc', 'createtime' => 'desc'])
  181. ->page($page, $pageSize)
  182. ->select();
  183. if ($evaluate) {
  184. $evaluate = collection($evaluate)->append(['createtime_text'])->toArray();
  185. }
  186. $this->success('', $evaluate);
  187. }
  188. }