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.
 
 
 
 
 
 

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