Goods.php 12 KB

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