Product.php 14 KB

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