Product.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. use think\Db;
  11. /**
  12. * 商品
  13. */
  14. class Product extends Base
  15. {
  16. protected $noNeedLogin = ['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])->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. // 默认没有收藏
  74. // $data->favorite = false;
  75. // 评价
  76. /*$data['evaluate_data'] = (new Evaluate)->where(['product_id' => $productId])
  77. ->field('COUNT(*) as count, IFNULL(CEIL(AVG(rate)/5*100),0) as avg')
  78. ->find();*/
  79. //优惠券
  80. /*$data->coupon = (new Coupon)->where('endtime', '>', time())
  81. ->where(['switch' => Coupon::SWITCH_ON])->order('weigh DESC')->select();*/
  82. $data->coupon = [];
  83. if($this->auth->isLogin()){
  84. $data->coupon = Db::name('unishop_coupon_user')->alias('cu')
  85. ->field(['c.id','c.title','c.least','c.value','c.starttime','c.endtime'])
  86. ->join('unishop_coupon c','cu.coupon_id = c.id','LEFT')
  87. ->where('cu.user_id',$this->auth->id)
  88. ->where('cu.status',0)
  89. ->where('c.deletetime',NULL)
  90. ->where('c.switch',Coupon::SWITCH_ON)
  91. ->where('c.endtime','>',time())
  92. ->select();
  93. }
  94. // 是否已收藏
  95. /*if ($this->auth->id) {
  96. $data->favorite = (new Favorite)->where(['user_id' => $this->auth->id, 'product_id' => $productId])->count();
  97. }*/
  98. // 购物车数量
  99. // $data->cart_num = (new \addons\unishop\model\Cart)->where(['user_id' => $this->auth->id])->count();
  100. // 评价信息
  101. /*$evaluate = (new Evaluate)->alias('e')
  102. ->join('user u', 'e.user_id = u.id')
  103. ->where(['e.product_id' => $productId, 'toptime' => ['>', Evaluate::TOP_OFF]])
  104. ->field('u.username,u.avatar,e.*')
  105. ->order(['toptime' => 'desc', 'createtime' => 'desc'])->select();
  106. if ($evaluate) {
  107. $data->evaluate_list = collection($evaluate)->append(['createtime_text'])->toArray();
  108. }*/
  109. $data = $data->append(['images_text', 'spec_list', 'spec_table_list'])->toArray();
  110. } catch (Exception $e) {
  111. $this->error($e->getMessage());
  112. }
  113. $this->success('', $data);
  114. }
  115. /**
  116. * @ApiTitle (产品列表)
  117. * @ApiSummary (产品列表)
  118. * @ApiMethod (GET)
  119. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  120. * @ApiParams (name="sid", type="integer", required=false, description="二级分类id")
  121. * @ApiParams (name="fid", type="integer", required=true, description="一级分类id")
  122. * @ApiParams (name="page", type="integer", required=true, description="页面")
  123. * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量")
  124. * @ApiParams (name="by", type="string", required=true, description="排序字段")
  125. * @ApiParams (name="desc", type="string", required=true, description="排序desc,asc")
  126. * @ApiReturn ({"code":1,"msg":"","data":[]})
  127. * @ApiReturnParams (name="title", type="string", description="商品名称")
  128. * @ApiReturnParams (name="image", type="string", description="商品图片")
  129. * @ApiReturnParams (name="sales", type="integer", description="销量")
  130. * @ApiReturnParams (name="sales_price", type="string", description="销售价钱")
  131. * @ApiReturnParams (name="product_id", type="string", description="商品id")
  132. */
  133. public function lists()
  134. {
  135. $page = input('page', 1);
  136. $pagesize = input('pagesize', 20);
  137. $by = input('by', 'weigh');
  138. $desc = input('desc', 'desc');
  139. // $sid = input('sid'); // 二级分类Id
  140. $fid = input('fid'); // 一级分类Id
  141. $productModel = new productModel();
  142. /*if ($fid && !$sid) {
  143. $categoryModel = new \addons\unishop\model\Category();
  144. $sArr = $categoryModel->where('pid', $fid)->field('id')->select();
  145. $sArr = array_column($sArr, 'id');
  146. array_push($sArr, $fid);
  147. $productModel->where('category_id', 'in', $sArr);
  148. } else {
  149. $sid && $productModel->where(['category_id' => $sid]);
  150. }*/
  151. $fid && $productModel->where(['category_id' => $fid]);
  152. $result = $productModel
  153. ->where(['switch' => productModel::SWITCH_ON])
  154. ->page($page, $pagesize)
  155. ->order($by, $desc)
  156. ->field('id,title,info,image,sales_price,use_spec')
  157. ->select();
  158. if ($result) {
  159. $result = collection($result)->toArray();
  160. } else {
  161. $result = [];
  162. }
  163. $this->success('', $result);
  164. }
  165. /**
  166. * @ApiTitle (收藏/取消)
  167. * @ApiSummary (收藏/取消)
  168. * @ApiMethod (GET)
  169. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  170. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  171. * @ApiParams (name="id", type="string", required=true, description="商品id")
  172. * @ApiReturn ({"code":1,"msg":"","data":true})
  173. */
  174. public function favorite()
  175. {
  176. $id = input('id', 0);
  177. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  178. $user_id = $this->auth->id;
  179. $favoriteModel = Favorite::get(function ($query) use ($id, $user_id) {
  180. $query->where(['user_id' => $user_id, 'product_id' => $id]);
  181. });
  182. if ($favoriteModel) {
  183. Favorite::destroy($favoriteModel->id);
  184. } else {
  185. $product = productModel::withTrashed()->where(['id' => $id, 'switch' => productModel::SWITCH_ON])->find();
  186. if (!$product) {
  187. $this->error('参数错误');
  188. }
  189. $favoriteModel = new Favorite();
  190. $favoriteModel->user_id = $user_id;
  191. $favoriteModel->product_id = $id;
  192. $product = $product->getData();
  193. $data['image'] = $product['image'];
  194. $data['market_price'] = $product['market_price'];
  195. $data['product_id'] = Hashids::encodeHex($product['id']);
  196. $data['sales_price'] = $product['sales_price'];
  197. $data['title'] = $product['title'];
  198. $favoriteModel->snapshot = json_encode($data);
  199. $favoriteModel->save();
  200. }
  201. $this->success('', true);
  202. }
  203. /**
  204. * @ApiTitle (收藏列表)
  205. * @ApiSummary (收藏列表)
  206. * @ApiMethod (GET)
  207. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  208. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  209. * @ApiParams (name="page", type="integer", required=true, description="页面")
  210. * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量")
  211. * @ApiReturn ({"code":1,"msg":"","data":[]})
  212. *
  213. * @ApiReturnParams (name="id", type="integer", description="收藏id")
  214. * @ApiReturnParams (name="user_id", type="integer", description="用户id")
  215. * @ApiReturnParams (name="product.image", type="string", description="商品图片")
  216. * @ApiReturnParams (name="product.title", type="string", description="商品名称")
  217. * @ApiReturnParams (name="product.sales_price", type="string", description="商品销售价钱")
  218. * @ApiReturnParams (name="product.market_price", type="string", description="商品市场价钱")
  219. * @ApiReturnParams (name="product.product_id", type="string", description="商品id")
  220. * @ApiReturnParams (name="status", type="integer", description="是否还有效")
  221. *
  222. */
  223. public function favoriteList()
  224. {
  225. $page = input('page', 1);
  226. $pageSize = input('pagesize', 20);
  227. $list = (new Favorite)->where(['user_id' => $this->auth->id])->with(['product'])->page($page, $pageSize)->select();
  228. $list = collection($list)->toArray();
  229. foreach ($list as &$item) {
  230. if (!empty($item['product'])) {
  231. $item['status'] = 1;
  232. } else {
  233. $item['status'] = 0;
  234. $item['product'] = json_decode($item['snapshot'],true);
  235. $image = $item['product']['image'];
  236. $item['product']['image'] = Config::getImagesFullUrl($image);
  237. }
  238. unset($item['snapshot']);
  239. }
  240. $this->success('', $list);
  241. }
  242. /**
  243. * @ApiTitle (商品评论)
  244. * @ApiSummary (商品评论)
  245. * @ApiMethod (GET)
  246. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  247. * @ApiParams (name="product_id", type="string", required=true, description="商品id")
  248. * @ApiParams (name="page", type="integer", required=true, description="页面")
  249. * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量")
  250. * @ApiReturn ({"code":1,"msg":"","data":[]})
  251. *
  252. * @ApiReturnParams (name="id", type="integer", description="评论id")
  253. * @ApiReturnParams (name="username", type="string", description="评论人名称")
  254. * @ApiReturnParams (name="avatar", type="string", description="评论人头像")
  255. * @ApiReturnParams (name="product_id", type="string", description="商品id")
  256. * @ApiReturnParams (name="comment", type="string", description="评论内容")
  257. * @ApiReturnParams (name="rate", type="integer", description="星星")
  258. * @ApiReturnParams (name="order_id", type="string", description="订单id")
  259. * @ApiReturnParams (name="spec", type="string", description="评论的规格")
  260. * @ApiReturnParams (name="anonymous", type="integer", description="是否匿名")
  261. * @ApiReturnParams (name="toptime", type="integer", description="是否置顶")
  262. * @ApiReturnParams (name="createtime_text", type="string", description="评论时间")
  263. *
  264. */
  265. public function evaluate()
  266. {
  267. $page = input('page', 1);
  268. $pageSize = input('pagesize', 20);
  269. $productId = input('product_id');
  270. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  271. // 评价信息
  272. $evaluate = (new Evaluate)->alias('e')
  273. ->join('user u', 'e.user_id = u.id')
  274. ->where(['e.product_id' => $productId])
  275. ->field('u.username,u.avatar,e.*')
  276. ->order(['toptime' => 'desc', 'createtime' => 'desc'])
  277. ->page($page, $pageSize)
  278. ->select();
  279. if ($evaluate) {
  280. $evaluate = collection($evaluate)->append(['createtime_text'])->toArray();
  281. }
  282. $this->success('', $evaluate);
  283. }
  284. }