Goods.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\ShopGoods;
  4. use app\data\model\ShopGoodsCart;
  5. use app\data\model\ShopGoodsCate;
  6. use app\data\model\ShopGoodsCollection;
  7. use app\data\model\ShopGoodsMark;
  8. use app\data\service\ExpressService;
  9. use app\data\service\GoodsService;
  10. use think\admin\Controller;
  11. use hg\apidoc\annotation\Title;
  12. use hg\apidoc\annotation\Method;
  13. use hg\apidoc\annotation\Param;
  14. use hg\apidoc\annotation\Returned;
  15. /**
  16. * 商品数据接口
  17. * Class Goods
  18. * @package app\data\controller\api
  19. */
  20. class Goods extends Auth
  21. {
  22. /**
  23. * @Title ("获取分类数据")
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function getCate()
  29. {
  30. $this->success('获取分类成功', ShopGoodsCate::treeData());
  31. }
  32. /**
  33. * 获取标签数据
  34. */
  35. public function getMark()
  36. {
  37. $this->success('获取标签成功', ShopGoodsMark::items());
  38. }
  39. /**
  40. * @Title ("name",desc="商品数据展示")
  41. * @Param ("cateids",desc = ”商品分类id")
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getGoods()
  47. {
  48. // 更新访问统计
  49. $map = $this->_vali(['code.default' => '']);
  50. if ($map['code']) ShopGoods::mk()->where($map)->inc('num_read')->update([]);
  51. // 商品数据处理
  52. $query = ShopGoods::mQuery()->like('name,marks,cateids,payment')->equal('code,vip_entry');
  53. $result = $query->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc')->page(true, false, false, 10);
  54. if (count($result['list']) > 0) GoodsService::bindData($result['list']);
  55. $this->success('获取商品数据', $result);
  56. }
  57. /**
  58. * 获取配送区域
  59. */
  60. public function getRegion()
  61. {
  62. $this->success('获取区域成功', ExpressService::region(3, 1));
  63. }
  64. /**
  65. * @Title("购物车添加")
  66. * @return void
  67. */
  68. public function cart_add()
  69. {
  70. $user = $this->getUser();
  71. $data = $this->_vali([
  72. 'admin_id.default'=>'',
  73. 'item_id.default' => '',
  74. 'num.default' => '',
  75. 'goods_id.default' =>'',
  76. 'admin_id.require' =>'店铺不能为空',
  77. 'item_id.require' => '选择商品规格不能为空!',
  78. 'num.require' => '数量不能为空!',
  79. 'goods_id.require' =>'商品不能为空'
  80. ]);
  81. $add_data=[
  82. 'admin_id'=>$data['admin_id'],
  83. 'goods_id'=>$data['goods_id'],
  84. 'item_id'=>$data['item_id'],
  85. 'user_id'=>$user['id'],
  86. 'num'=>$data['num'],
  87. 'create_time'=>date('Y-m-d H:i:s')
  88. ];
  89. ShopGoodsCart::mk()->insert($add_data);
  90. $this->success('成功加入购物车!');
  91. }
  92. /**
  93. * @Title ("购物车列表")
  94. * @return void
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. *
  99. */
  100. public function goods_cart_list(){
  101. $user = $this->getUser();
  102. $cart_list = ShopGoodsCart::mk()->alias('cart')
  103. ->join('shop_goods_item item','cart.item_id=item.id')
  104. ->join('shop_goods goods','item.goods_code=goods.code')
  105. ->join('system_user admin','admin.id=item.admin_id')
  106. ->where('cart.user_id',$user['id'])
  107. ->field('cart.*,item.goods_spec,goods.cover')
  108. ->paginate();
  109. $this->success('我的购物车列表',$cart_list);
  110. }
  111. /**
  112. * @Title ("修改购物车")
  113. * @Param ("cart_id",desc="购物车id")
  114. * @Param ("num",desc="数量")
  115. */
  116. public function goods_cart_save(){
  117. $user = $this->getUser();
  118. $cart_id= input('cart_id');
  119. $num = input('num');
  120. ShopGoodsCart::mk()->where(array('id'=>$cart_id,'user_id'=>$user['id']))->save(['num'=>$num]);
  121. $this->success('购物车修改完成');
  122. }
  123. /**
  124. * @Title ("删除购物车")
  125. * @Param ("cart_ids",desc="购物车id")
  126. */
  127. public function goods_cart_del(){
  128. $user = $this->getUser();
  129. $cart_id= input('cart_ids');
  130. ShopGoodsCart::mk()->whereIn('id',$cart_id)->where('user_id',$user['id'])->delete();
  131. $this->success('购物车已删除');
  132. }
  133. /**
  134. * @Title ("添加我的收藏")
  135. * @Param ("goods_id",desc="商品id")
  136. */
  137. public function collection(){
  138. $user =$this->getUser();
  139. $goods_id = input('goods_id');
  140. ShopGoodsCollection::mk()->insertGetId(['uuid'=>$user['id'],'goods_id'=>$goods_id,'create_at'=>date('Y-m-d H:i:s')]);
  141. $this->success('商品收藏成功');
  142. }
  143. /**
  144. * @Title ("删除我的收藏")
  145. * @Param ("coll_ids",desc="收藏id")
  146. */
  147. public function del_collection(){
  148. $user = $this->getUser();
  149. $coll_ids = input('coll_ids');
  150. ShopGoodsCollection::mk()->whereIn('id',$coll_ids)->where('uuid',$user['id'])->delete();
  151. $this->success('商品收藏已取消');
  152. }
  153. /**
  154. * @Title ("我的收藏列表")
  155. */
  156. public function collection_list(){
  157. $user = $this->getUser();
  158. $list = ShopGoodsCollection::mk()->where('uuid',$user['id'])->order('id','desc')->paginate();
  159. $this->success('我的收藏列表',$list);
  160. }
  161. }