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.

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