Product.php 14 KB

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