Product.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /**
  11. * 商品
  12. */
  13. class Product extends Base
  14. {
  15. protected $noNeedLogin = ['detail', 'lists'];
  16. protected $frequently = ['detail', 'lists'];
  17. /**
  18. * @ApiTitle (产品详情)
  19. * @ApiSummary (产品详情)
  20. * @ApiMethod (GET)
  21. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  22. * @ApiParams (name="id", type="string", required=true, description="商品id")
  23. * @ApiReturn ({"code":1,"msg":"","data":{}})
  24. *
  25. * @ApiReturnParams (name="category_id", type="integer", description="分类id")
  26. * @ApiReturnParams (name="title", type="string", description="商品名称")
  27. * @ApiReturnParams (name="image", type="string", description="商品图片")
  28. * @ApiReturnParams (name="images_text", type="array", description="商品图片组")
  29. * @ApiReturnParams (name="desc", type="string", description="商品详情")
  30. * @ApiReturnParams (name="sales", type="integer", description="销量")
  31. * @ApiReturnParams (name="sales_price", type="string", description="销售价钱")
  32. * @ApiReturnParams (name="market_price", type="string", description="市场价钱")
  33. * @ApiReturnParams (name="product_id", type="string", description="商品id")
  34. * @ApiReturnParams (name="stock", type="integer", description="库存")
  35. * @ApiReturnParams (name="look", type="integer", description="观看量")
  36. * @ApiReturnParams (name="use_spec", type="integer", description="是否使用规格")
  37. * @ApiReturnParams (name="server", type="string", description="支持的服务")
  38. * @ApiReturnParams (name="favorite", type="integer", description="是否已收藏")
  39. * @ApiReturnParams (name="evaluate_data", type="json", description="{count:'评论数据',avg:'好评平均值'}")
  40. * @ApiReturnParams (name="coupon", type="array", description="可用优惠券")
  41. * @ApiReturnParams (name="cart_num", type="integer", description="购物车数量")
  42. * @ApiReturnParams (name="spec_list", type="array", description="规格键值数据")
  43. * @ApiReturnParams (name="spec_table_list", type="array", description="规格值数据")
  44. *
  45. */
  46. public function detail()
  47. {
  48. $productId = input('id');
  49. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  50. try {
  51. $productModel = new productModel();
  52. $data = $productModel->where(['id' => $productId])->cache(10)->find();
  53. if (!$data) {
  54. $this->error(__('Goods not exist'));
  55. }
  56. if ($data['switch'] == productModel::SWITCH_OFF) {
  57. $this->error(__('Goods are off the shelves'));
  58. }
  59. // 真实浏览量加一
  60. $data->real_look++;
  61. $data->look++;
  62. $data->save();
  63. //服务
  64. /*$server = explode(',', $data->server);
  65. $configServer = json_decode(Config::getByName('server')['value'],true);
  66. $serverValue = [];
  67. foreach ($server as $k => $v) {
  68. if (isset($configServer[$v])) {
  69. $serverValue[] = $configServer[$v];
  70. }
  71. }
  72. $data->server = count($serverValue) ? implode(' · ', $serverValue) : '';*/
  73. $data->server = '';
  74. // 默认没有收藏
  75. $data->favorite = false;
  76. // 评价
  77. $data['evaluate_data'] = (new Evaluate)->where(['product_id' => $productId])
  78. ->field('COUNT(*) as count, IFNULL(CEIL(AVG(rate)/5*100),0) as avg')
  79. ->cache(10)->find();
  80. //优惠券
  81. /*$data->coupon = (new Coupon)->where('endtime', '>', time())
  82. ->where(['switch' => Coupon::SWITCH_ON])->cache(10)->order('weigh DESC')->select();*/
  83. $data->coupon = [];
  84. // 是否已收藏
  85. if ($this->auth->id) {
  86. $data->favorite = (new Favorite)->where(['user_id' => $this->auth->id, 'product_id' => $productId])->count();
  87. }
  88. // 购物车数量
  89. $data->cart_num = (new \addons\unishop\model\Cart)->where(['user_id' => $this->auth->id])->sum('number');
  90. // 评价信息
  91. $data->evaluate_list = [];
  92. $evaluate = (new Evaluate)->alias('e')
  93. ->join('user u', 'e.user_id = u.id')
  94. ->where(['e.product_id' => $productId, 'toptime' => ['>', Evaluate::TOP_OFF]])
  95. ->field('u.username,u.avatar,e.*')
  96. ->order(['toptime' => 'desc', 'createtime' => 'desc'])->select();
  97. if ($evaluate) {
  98. $data->evaluate_list = collection($evaluate)->append(['createtime_text'])->toArray();
  99. }
  100. $data = $data->append(['images_text','videothumb', 'spec_list', 'spec_table_list'])->toArray();
  101. unset($data['switch']);
  102. } catch (Exception $e) {
  103. $this->error($e->getMessage());
  104. }
  105. $this->success('', $data);
  106. }
  107. /**
  108. * @ApiTitle (产品列表)
  109. * @ApiSummary (产品列表)
  110. * @ApiMethod (GET)
  111. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  112. * @ApiParams (name="sid", type="integer", required=false, description="二级分类id")
  113. * @ApiParams (name="fid", type="integer", required=true, description="一级分类id")
  114. * @ApiParams (name="page", type="integer", required=true, description="页面")
  115. * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量")
  116. * @ApiParams (name="by", type="string", required=true, description="排序字段")
  117. * @ApiParams (name="desc", type="string", required=true, description="排序desc,asc")
  118. * @ApiReturn ({"code":1,"msg":"","data":[]})
  119. * @ApiReturnParams (name="title", type="string", description="商品名称")
  120. * @ApiReturnParams (name="image", type="string", description="商品图片")
  121. * @ApiReturnParams (name="sales", type="integer", description="销量")
  122. * @ApiReturnParams (name="sales_price", type="string", description="销售价钱")
  123. * @ApiReturnParams (name="product_id", type="string", description="商品id")
  124. */
  125. public function lists()
  126. {
  127. $page = input('page', 1);
  128. $pagesize = input('pagesize', 20);
  129. $by = input('by', 'weigh');
  130. $desc = input('desc', 'desc');
  131. // $sid = input('sid'); // 二级分类Id
  132. $fid = input('fid'); // 一级分类Id
  133. $keyword = input('keyword','');//搜索
  134. $productModel = new productModel();
  135. /* if ($fid && !$sid) {
  136. $categoryModel = new \addons\unishop\model\Category();
  137. $sArr = $categoryModel->where('pid', $fid)->field('id')->select();
  138. $sArr = array_column($sArr, 'id');
  139. array_push($sArr, $fid);
  140. $productModel->where('category_id', 'in', $sArr);
  141. } else {
  142. $sid && $productModel->where(['category_id' => $sid]);
  143. }*/
  144. $fid && $productModel->where(['category_id' => $fid]);
  145. if(!empty($keyword)){
  146. $productModel->where('title', 'LIKE', '%'.$keyword.'%');
  147. }
  148. $result = $productModel
  149. ->where(['switch' => productModel::SWITCH_ON])
  150. ->page($page, $pagesize)
  151. ->order($by, $desc)
  152. ->field('id,title,image,sales_price,market_price')
  153. ->select();
  154. if ($result) {
  155. $result = collection($result)->toArray();
  156. } else {
  157. $result = [];
  158. }
  159. $this->success('', $result);
  160. }
  161. /**
  162. * @ApiTitle (收藏/取消)
  163. * @ApiSummary (收藏/取消)
  164. * @ApiMethod (GET)
  165. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  166. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  167. * @ApiParams (name="id", type="string", required=true, description="商品id")
  168. * @ApiReturn ({"code":1,"msg":"","data":true})
  169. */
  170. public function favorite()
  171. {
  172. $id = input('id', 0);
  173. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  174. $user_id = $this->auth->id;
  175. $favoriteModel = Favorite::get(function ($query) use ($id, $user_id) {
  176. $query->where(['user_id' => $user_id, 'product_id' => $id]);
  177. });
  178. if ($favoriteModel) {
  179. Favorite::destroy($favoriteModel->id);
  180. } else {
  181. $product = productModel::withTrashed()->where(['id' => $id, 'switch' => productModel::SWITCH_ON])->find();
  182. if (!$product) {
  183. $this->error('参数错误');
  184. }
  185. $favoriteModel = new Favorite();
  186. $favoriteModel->user_id = $user_id;
  187. $favoriteModel->product_id = $id;
  188. $product = $product->getData();
  189. $data['image'] = $product['image'];
  190. $data['market_price'] = $product['market_price'];
  191. $data['product_id'] = Hashids::encodeHex($product['id']);
  192. $data['sales_price'] = $product['sales_price'];
  193. $data['title'] = $product['title'];
  194. $favoriteModel->snapshot = json_encode($data);
  195. $favoriteModel->save();
  196. }
  197. $this->success('', true);
  198. }
  199. /**
  200. * @ApiTitle (收藏列表)
  201. * @ApiSummary (收藏列表)
  202. * @ApiMethod (GET)
  203. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  204. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  205. * @ApiParams (name="page", type="integer", required=true, description="页面")
  206. * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量")
  207. * @ApiReturn ({"code":1,"msg":"","data":[]})
  208. *
  209. * @ApiReturnParams (name="id", type="integer", description="收藏id")
  210. * @ApiReturnParams (name="user_id", type="integer", description="用户id")
  211. * @ApiReturnParams (name="product.image", type="string", description="商品图片")
  212. * @ApiReturnParams (name="product.title", type="string", description="商品名称")
  213. * @ApiReturnParams (name="product.sales_price", type="string", description="商品销售价钱")
  214. * @ApiReturnParams (name="product.market_price", type="string", description="商品市场价钱")
  215. * @ApiReturnParams (name="product.product_id", type="string", description="商品id")
  216. * @ApiReturnParams (name="status", type="integer", description="是否还有效")
  217. *
  218. */
  219. public function favoriteList()
  220. {
  221. $page = input('page', 1);
  222. $pageSize = input('pagesize', 20);
  223. $list = (new Favorite)->where(['user_id' => $this->auth->id])->with(['product'])->page($page, $pageSize)->select();
  224. $list = collection($list)->toArray();
  225. foreach ($list as &$item) {
  226. if (!empty($item['product'])) {
  227. $item['status'] = 1;
  228. } else {
  229. $item['status'] = 0;
  230. $item['product'] = json_decode($item['snapshot'],true);
  231. $image = $item['product']['image'];
  232. $item['product']['image'] = Config::getImagesFullUrl($image);
  233. }
  234. unset($item['snapshot']);
  235. }
  236. $this->success('', $list);
  237. }
  238. /**
  239. * @ApiTitle (商品评论)
  240. * @ApiSummary (商品评论)
  241. * @ApiMethod (GET)
  242. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  243. * @ApiParams (name="product_id", type="string", required=true, description="商品id")
  244. * @ApiParams (name="page", type="integer", required=true, description="页面")
  245. * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量")
  246. * @ApiReturn ({"code":1,"msg":"","data":[]})
  247. *
  248. * @ApiReturnParams (name="id", type="integer", description="评论id")
  249. * @ApiReturnParams (name="username", type="string", description="评论人名称")
  250. * @ApiReturnParams (name="avatar", type="string", description="评论人头像")
  251. * @ApiReturnParams (name="product_id", type="string", description="商品id")
  252. * @ApiReturnParams (name="comment", type="string", description="评论内容")
  253. * @ApiReturnParams (name="rate", type="integer", description="星星")
  254. * @ApiReturnParams (name="order_id", type="string", description="订单id")
  255. * @ApiReturnParams (name="spec", type="string", description="评论的规格")
  256. * @ApiReturnParams (name="anonymous", type="integer", description="是否匿名")
  257. * @ApiReturnParams (name="toptime", type="integer", description="是否置顶")
  258. * @ApiReturnParams (name="createtime_text", type="string", description="评论时间")
  259. *
  260. */
  261. public function evaluate()
  262. {
  263. $page = input('page', 1);
  264. $pageSize = input('pagesize', 20);
  265. $productId = input('product_id');
  266. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  267. // 评价信息
  268. $evaluate = (new Evaluate)->alias('e')
  269. ->join('user u', 'e.user_id = u.id')
  270. ->where(['e.product_id' => $productId])
  271. ->field('u.username,u.avatar,e.*')
  272. ->order(['toptime' => 'desc', 'createtime' => 'desc'])
  273. ->page($page, $pageSize)
  274. ->select();
  275. if ($evaluate) {
  276. $evaluate = collection($evaluate)->append(['createtime_text'])->toArray();
  277. }
  278. $this->success('', $evaluate);
  279. }
  280. }