Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\DataMerchants;
  4. use app\data\model\ShopGoods;
  5. use app\data\model\ShopGoodsCart;
  6. use app\data\model\ShopGoodsCate;
  7. use app\data\model\ShopGoodsCollection;
  8. use app\data\model\ShopGoodsMark;
  9. use app\data\model\ShopOrder;
  10. use app\data\model\ShopOrderItem;
  11. use app\data\model\ShopOrderPay;
  12. use app\data\service\ExpressService;
  13. use app\data\service\GoodsService;
  14. use think\admin\Controller;
  15. use hg\apidoc\annotation\Title;
  16. use hg\apidoc\annotation\Method;
  17. use hg\apidoc\annotation\Param;
  18. use hg\apidoc\annotation\Returned;
  19. use hg\apidoc\annotation\Header;
  20. use app\data\model\SystemUser;
  21. /**
  22. * 商品数据接口
  23. * Class Goods
  24. * @package app\data\controller\api
  25. */
  26. class Goods extends Auth
  27. {
  28. /**
  29. * @Title ("用户信息")
  30. * @Method ("get")
  31. * @Param ("admin_id",desc="商家端id")
  32. * @Returned("merchant.head_img",desc="商家头像")
  33. * @Returned("merchant.name",desc="商家名称")
  34. * @Returned("merchant.full_address",desc="详细地址")
  35. * @Returned("merchant.contact_name",desc="联系人")
  36. * @Returned("merchant.contact_phone",desc="联系电话")
  37. * @Returned("merchant.intro",desc="商家简介")
  38. * @Returned("merchant.imgs_videos",desc="图片及视频")
  39. * @Returned("merchant.business_img",desc="营业执照")
  40. * @Returned("merchant.longitude",desc="经度")
  41. * @Returned("merchant.latitude",desc="纬度")
  42. */
  43. public function user_info(){
  44. $admin_id = $this->_vali(['admin_id.require'=>'商家id不能空']);
  45. $merchant=DataMerchants::getByAdmin($admin_id['admin_id']);
  46. $this->success('用户信息',$merchant);
  47. }
  48. /**
  49. * @Title ("获取分类数据")
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getCate()
  55. {
  56. $this->success('获取分类成功', ShopGoodsCate::treeData());
  57. }
  58. /**
  59. * 获取标签数据
  60. */
  61. public function getMark()
  62. {
  63. $this->success('获取标签成功', ShopGoodsMark::items());
  64. }
  65. /**
  66. * @Title ("商品数据展示")
  67. * @Header("api-name",type="string",require=1,default="",desc="访问类型")
  68. * @Header("api-token",type="string",require=1,default="",desc="token")
  69. * @Param ("cateids",desc="商品分类id")
  70. * @Param ("name",desc="搜索名称模糊查询")
  71. * @Param ("sales_order",desc="销量排序 asc desc")
  72. * @Param ("price_order",desc="价格排序 asc desc")
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function getGoods()
  78. {
  79. $sales_order = input('sales_order','desc');
  80. $price_order = input('price_order','desc');
  81. // 更新访问统计
  82. $map = $this->_vali(['code.default' => '']);
  83. if ($map['code']) ShopGoods::mk()->where($map)->inc('num_read')->update([]);
  84. // 商品数据处理
  85. $query = ShopGoods::mQuery()->like('name,marks,cateids,payment')->equal('code,vip_entry');
  86. $result = $query->where(['deleted' => 0, 'status' => 1,'examine'=>1])
  87. ->when($sales_order,function ($quer) use ($sales_order){
  88. $quer->order('stock_sales '.$sales_order);
  89. })
  90. ->when($price_order,function ($quer) use ($price_order){
  91. $quer->order('price_market '.$price_order);
  92. })
  93. //->order('sort desc,id desc')
  94. ->page(true, false, false, 10);
  95. if (count($result['list']) > 0) GoodsService::bindData($result['list']);
  96. $this->success('获取商品数据', $result);
  97. }
  98. /**
  99. * @Title ("商品详情")
  100. * @Param ("goods_id",desc="商品id")
  101. * @return void
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function Goods_info(){
  107. $id = input('goods_id');
  108. $query = ShopGoods::mQuery()->where('id',$id);
  109. $result = $query->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc')->page(true, false, false, 10);
  110. if (count($result['list']) > 0) GoodsService::bindData($result['list']);
  111. $result['list'][0]['admin_name']=SystemUser::mk()->where('id',$result['list'][0]['admin_id'])->value('username');
  112. $result['list'][0]['is_collection']=0;
  113. if(ShopGoodsCollection::mk()->where('goods_id',$result['list'][0]['id'])->where('uuid',$this->uuid)->find()){
  114. $result['list'][0]['is_collection']=1;
  115. }
  116. $this->success('获取商品数据详情', $result['list'][0]);
  117. }
  118. /**
  119. * 获取配送区域
  120. */
  121. public function getRegion()
  122. {
  123. $this->success('获取区域成功', ExpressService::region(3, 1));
  124. }
  125. /**
  126. * @Title ("购物车数量")
  127. * @Method ("get")
  128. * @return void
  129. * @throws \think\db\exception\DbException
  130. */
  131. public function cart_number(){
  132. $user = $this->getUser();
  133. $count = ShopGoodsCart::mk()->where('user_id',$user['id'])->count();
  134. $this->success('购物车数量!',$count);
  135. }
  136. /**
  137. * @Title("购物车添加")
  138. * @Param ("admin_id",desc="商家id")
  139. * @Param ("goods_id",desc="商品id")
  140. * @Param ("item_id",desc="规格id")
  141. * @Param ("num",desc="数量")
  142. * @return void
  143. */
  144. public function cart_add()
  145. {
  146. $user = $this->getUser();
  147. $data = $this->_vali([
  148. 'admin_id.require' =>'店铺不能为空',
  149. 'item_id.require' => '选择商品规格不能为空!',
  150. 'num.require' => '数量不能为空!',
  151. 'goods_id.require' =>'商品不能为空'
  152. ]);
  153. $add_data=[
  154. 'admin_id'=>$data['admin_id'],
  155. 'goods_id'=>$data['goods_id'],
  156. 'item_id'=>$data['item_id'],
  157. 'user_id'=>$user['id'],
  158. 'num'=>$data['num'],
  159. 'create_time'=>date('Y-m-d H:i:s')
  160. ];
  161. $is_have = ShopGoodsCart::mk()->where(['admin_id'=>$data['admin_id'],'goods_id'=>$data['goods_id'],'item_id'=>$data['item_id'],'user_id'=>$user['id']])->find();
  162. if($is_have){
  163. $add_data['num']=$is_have['num']+$data['num'];
  164. ShopGoodsCart::mk()->where('id',$is_have['id'])->save($add_data);
  165. }
  166. else {
  167. ShopGoodsCart::mk()->insert($add_data);
  168. }
  169. $this->success('成功加入购物车!');
  170. }
  171. /**
  172. * @Title ("购物车列表")
  173. * @return void
  174. * @throws \think\db\exception\DataNotFoundException
  175. * @throws \think\db\exception\DbException
  176. * @throws \think\db\exception\ModelNotFoundException
  177. *
  178. */
  179. public function goods_cart_list(){
  180. $user = $this->getUser();
  181. $query = ShopGoodsCart::mQuery()->alias('cart');
  182. $cart_list = $query ->join('system_user admin','admin.id=cart.admin_id')
  183. ->where('cart.user_id',$user['id'])
  184. ->group('cart.admin_id')
  185. ->field('cart.admin_id,admin.nickname')
  186. ->page(true, false, false, 10);
  187. foreach ($cart_list['list'] as $k=>$v){
  188. $cart_list['list'][$k]['goods_item']= ShopGoodsCart::mk()->alias('cart')
  189. ->join('shop_goods goods','cart.goods_id=goods.id')
  190. ->join('shop_goods_item item','cart.item_id=item.id')
  191. ->join('system_user admin','admin.id=cart.admin_id')
  192. ->where('cart.user_id',$user['id'])
  193. ->where('cart.admin_id',$cart_list['list'][$k]['admin_id'])
  194. ->field('cart.*,goods.cover,goods.name,item.price_selling,item.goods_spec')
  195. ->select();
  196. }
  197. $this->success('我的购物车列表',$cart_list);
  198. }
  199. /**
  200. * @Title ("修改购物车")
  201. * @Param ("cart_id",desc="购物车id")
  202. * @Param ("num",desc="数量")
  203. */
  204. public function goods_cart_save(){
  205. $user = $this->getUser();
  206. $cart_id= input('cart_id');
  207. $num = input('num');
  208. ShopGoodsCart::mk()->where(array('id'=>$cart_id,'user_id'=>$user['id']))->save(['num'=>$num]);
  209. $this->success('购物车修改完成');
  210. }
  211. /**
  212. * @Title ("删除购物车")
  213. * @Param ("cart_ids",desc="购物车id")
  214. */
  215. public function goods_cart_del(){
  216. $user = $this->getUser();
  217. $cart_id= input('cart_ids');
  218. ShopGoodsCart::mk()->whereIn('id',$cart_id)->where('user_id',$user['id'])->delete();
  219. $this->success('购物车已删除');
  220. }
  221. /**
  222. * @Title ("添加我的收藏")
  223. * @Param ("goods_id",desc="商品id")
  224. */
  225. public function collection(){
  226. $user =$this->getUser();
  227. $goods_id = input('goods_id');
  228. ShopGoodsCollection::mk()->insertGetId(['uuid'=>$user['id'],'goods_id'=>$goods_id,'create_at'=>date('Y-m-d H:i:s')]);
  229. $this->success('商品收藏成功');
  230. }
  231. /**
  232. * @Title ("删除我的收藏")
  233. * @Param ("coll_ids",desc="收藏id")
  234. */
  235. public function del_collection(){
  236. $user = $this->getUser();
  237. $coll_ids = input('coll_ids');
  238. ShopGoodsCollection::mk()->whereIn('id',$coll_ids)->where('uuid',$user['id'])->delete();
  239. $this->success('商品收藏已取消');
  240. }
  241. /**
  242. * @Title ("详情取消收藏")
  243. * @Param ("goods_id",desc="商品id")
  244. */
  245. public function cancel_collection(){
  246. $user = $this->getUser();
  247. $coll_ids = input('goods_id');
  248. ShopGoodsCollection::mk()->where('goods_id',$coll_ids)->where('uuid',$user['id'])->delete();
  249. $this->success('商品收藏已取消');
  250. }
  251. /**
  252. * @Title ("我的收藏列表")
  253. */
  254. public function collection_list(){
  255. $user = $this->getUser();
  256. $list = ShopGoodsCollection::mk()->alias('c')->join('shop_goods g','c.goods_id=g.id')->where('c.uuid',$user['id'])->order('c.id','desc')->field('c.*,g.name,g.cover,g.price_selling')->paginate();
  257. $this->success('我的收藏列表',$list);
  258. }
  259. }