Mall.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\GoodsCate;
  4. use app\common\model\GoodsOrder;
  5. use app\common\model\ShoppingTrolley;
  6. use app\common\model\StoreGoods;
  7. use app\common\model\TopSearch;
  8. use app\common\model\UserCollect;
  9. use app\common\model\UserMessage;
  10. use app\common\model\UserSearch;
  11. use app\common\model\UserTrack;
  12. use app\common\service\CheckPower;
  13. use app\common\service\UserSynth;
  14. use library\tools\Data;
  15. use think\Db;
  16. /**
  17. * @title 商城
  18. * @controller Mall
  19. * @group base
  20. */
  21. class Mall extends Base
  22. {
  23. // 需要登录的
  24. protected $need_login = [
  25. 'goodsCollect',
  26. 'getGoodsHistory',
  27. 'getGoodsCollectList',
  28. 'getOwnGoodsList',
  29. 'batchesDel',
  30. ];
  31. public function initialize(){
  32. parent::initialize();
  33. parent::setUid();
  34. }
  35. /**
  36. * @title 获取商品全部分类
  37. * @desc 获取商品分类
  38. * @author qc
  39. * @url /api/Mall/getGoodsCate
  40. * @method GET
  41. * @header name:Authorization require:1 desc:Token
  42. * @return name:id type:int default:-- desc:分类id
  43. * @return name:title type:srting default:-- desc:标题
  44. * @return name:lev type:srting default:-- desc:分类等级(1,2,3,4)
  45. * @return name:children type:array default:-- desc:下级分类(数据同上)
  46. */
  47. public function getGoodsCate()
  48. {
  49. $list = GoodsCate::getCateTree();
  50. $this->success('ok',['list'=>$list]);
  51. }
  52. /**
  53. * @title 获取商品二级分类
  54. * @desc 获取商品二级分类
  55. * @author qc
  56. * @url /api/Mall/getSecondCate
  57. * @method GET
  58. * @header name:Authorization require:1 desc:Token
  59. * @param name:id type:int default:-- desc:一级分类id
  60. * @return name:title type:srting default:-- desc:标题
  61. * @return name:lev type:srting default:-- desc:分类等级(1,2)
  62. */
  63. public function getSecondCate()
  64. {
  65. $list = GoodsCate::field('id,title,pid,lev')->where(['status'=>1,'is_deleted'=>0,'pid'=>input('get.id')])->order('sort desc ,id desc')->select()->toArray();
  66. $this->success('ok',['list'=>$list]);
  67. }
  68. /**
  69. * @title 获取第三级分类
  70. * @desc 获取第三级分类
  71. * @author qc
  72. * @url /api/Mall/getThirdCate
  73. * @method GET
  74. * @header name:Authorization require:1 desc:Token
  75. * @param name:fid type:int default:-- desc:一级分类id
  76. * @param name:pid type:int default:-- desc:二级分类id【不传查一级分类下全部的三级分类】
  77. * @return name:id type:int default:-- desc:分类id
  78. * @return name:title type:string default:0 desc:分类名称
  79. * @return name:lev type:int default:0 desc:分类等级
  80. * @return name:children type:array default:0 desc:下级分类
  81. */
  82. public function getThirdCate()
  83. {
  84. $where = [];
  85. $where[]=['is_deleted','=',0];
  86. $where[]=['pid','=',input('fid')];
  87. if(input('pid')) $where[] = ['id','=',input('pid')];
  88. $list = GoodsCate::where($where)->field('id,title,pid,lev,logo')->order('sort desc,id asc')->select()->toArray();
  89. foreach ($list as &$v) {
  90. $v['children'] = GoodsCate::where([['is_deleted','=',0],['pid','=',$v['id']]])->field('id,title,pid,lev,logo')->order('sort desc,id asc')->select()->toArray();
  91. }
  92. $this->success('ok',['list'=>$list]);
  93. }
  94. /**
  95. * @title 商城商品推荐
  96. * @desc 商城商品推荐
  97. * @author qc
  98. * @method GET
  99. * @url /api/Mall/getRecommendList
  100. * @header name:Authorization require:1 desc:Token
  101. * @param name:page type:int default:0 desc:页数
  102. * @param name:page_num type:int default:20 desc:每页数
  103. * @param name:isShape type:bool default:true desc:展示样式(传什么返回什么)
  104. * @param name:rand_user type:string default:-- desc:随机id(非登录状态必传,page=1时生成(a123),其他页数(a123),一直查询到没有数据)
  105. * @return name:id type:int default:-- desc:商品id
  106. * @return name:name type:string default:-- desc:商品名称
  107. * @return name:cover type:string default:-- desc:封面
  108. * @return name:sale_num type:int default:-- desc:销量
  109. * @return name:virtual type:int default:-- desc:虚拟销量
  110. * @return name:label_name type:array default:-- desc:标签
  111. * @return name:low_price type:float default:-- desc:价格
  112. * @return name:min_price type:float default:-- desc:最低价格
  113. * @return name:max_price type:float default:-- desc:最高价格
  114. */
  115. public function getRecommendList()
  116. {
  117. $user_id = $this->user_id ? $this->user_id : input('rand_user');
  118. if(!$user_id) $this->error('参数错误');
  119. $list = UserSynth::getRecommendList($user_id,'goods',$this->page,$this->page_num,[],input('sort_type',1));
  120. $list['isShape'] = input('isShape',true);
  121. if($list['code'] != 200) $this->error($list['msg']);
  122. foreach ($list['list'] as &$v) {
  123. $v['isShape'] = input('isShape',true);
  124. }
  125. $this->success('ok',$list);
  126. }
  127. /**
  128. * @title 商品合集列表
  129. * @desc 获取商品列表
  130. * @author qc
  131. * @url /api/Mall/getGoodsList
  132. * @method GET
  133. * @header name:Authorization require:1 desc:Token
  134. * @param name:page type:int default:1 desc:页数
  135. * @param name:page_num type:int default:20 desc:每页数
  136. * @param name:isShape type:bool default:true desc:展示样式(传什么返回什么)
  137. * @param name:third_classify type:int require:0 default:-- desc:三级分类id
  138. * @param name:sort_type type:int default:1 desc:排序--1默认2时间升序3时间降序
  139. * @return name:name type:string default:-- desc:商品名称
  140. * @return name:cover type:string default:-- desc:商品封面图
  141. * @return name:label_name type:array default:-- desc:标签
  142. * @return name:low_price type:float default:-- desc:最低售价
  143. * @return name:sale_num type:int default:-- desc:销量
  144. * @return name:virtual type:int default:-- desc:虚拟销量
  145. * @return name:min_price type:float default:-- desc:最低价格
  146. * @return name:max_price type:float default:-- desc:最高价格
  147. * @return name:total_num type:int default:-- desc:总数
  148. */
  149. public function getGoodsList(){
  150. $input = input('get.');
  151. $sel_where = [];
  152. $sel_where[] = ['status','=',1];
  153. $sel_where[] = ['is_deleted','=',0];
  154. $sort_type = input('get.sort_type',1);
  155. if(isset_full($input,'first_classify'))$sel_where[] = ['first_classify','=',$input['first_classify']];
  156. if(isset_full($input,'second_classify'))$sel_where[] = ['second_classify','=',$input['second_classify']];
  157. if(isset_full($input,'third_classify'))$sel_where[] = ['third_classify','=',$input['third_classify']];
  158. //if(!isset($sel_where['third_classify'])) $this->error('参数错误');
  159. switch ($sort_type) {
  160. case 1:
  161. $order = ['sort'=>'desc','id'=>'asc'];
  162. break;
  163. case 2:
  164. $order = ['id'=>'asc','sort'=>'desc'];
  165. break;
  166. case 3:
  167. $order = ['id'=>'desc','sort'=>'desc'];
  168. break;
  169. }
  170. $class = GoodsCate::where('id',$input['third_classify'])->find();
  171. if($class){
  172. $class_name = $class['title'];
  173. }else{
  174. $class_name = null;
  175. }
  176. $list = StoreGoods::with(['itemList'])
  177. ->field('id,name,label,sale_num,cover,specs')
  178. ->where($sel_where)->limit($this->off_set,$this->page_num)
  179. ->order($order)
  180. ->select()->toArray();
  181. foreach ($list as &$v){
  182. $v['isShape'] = input('isShape',true);
  183. $v['virtual'] = StoreGoods::getVirtualNum($v['id']);
  184. //$v['low_original'] = empty($v['item_list']) ? 0 : min(array_column($v['item_list'],'original_price'));
  185. $v['min_price'] = empty($v['item_list']) ? 0 : min(array_column($v['item_list'],'sell_price'));
  186. $v['max_price'] = empty($v['item_list']) ? 0 : max(array_column($v['item_list'],'sell_price'));
  187. //$v['total_num'] = empty($v['item_list']) ? 0 :array_sum(array_column($v['item_list'],'base_stock'));
  188. $v['specs'] = json_decode($v['specs'],true);
  189. $v['label_name'] = $v['label'] ? explode(',',trim($v['label'],',')): null;
  190. //unset($v['item_list']);
  191. }
  192. $total_num = StoreGoods::where($sel_where)->count();
  193. $this->success('ok',['list'=>$list,'isShape'=>input('isShape',true),'class_name'=>$class_name,'total_count'=>$total_num,'page_num'=>$this->page_num]);
  194. }
  195. /**
  196. * @title 获取商品详情
  197. * @desc 获取商品详情
  198. * @author qc
  199. * @url /api/Mall/getGoodsDetail
  200. * @method GET
  201. * @header name:Authorization require:1 desc:Token
  202. * @param name:goods_id type:int require:1 default:0 desc:商品id
  203. * @return name:name type:string default:-- desc:商品名称
  204. * @return name:cover type:string default:-- desc:商品封面图
  205. * @return name:desc type:string default:-- desc:商品简述(副标题)
  206. * @return name:min_price type:float default:-- desc:最低价格
  207. * @return name:max_price type:float default:-- desc:最高价格
  208. * @return name:detail type:string default:-- desc:详情(富文本)
  209. * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
  210. * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
  211. * @return name:postage type:float default:-- desc:运费(freight_type=0)
  212. * @return name:is_collect type:int default:-- desc:是否收藏(1是0否)
  213. * @return name:label_name type:array default:-- desc:标签
  214. * @return name:sale_num type:int default:-- desc:销量
  215. * @return name:virtual type:int default:-- desc:虚拟销量
  216. * @return name:image_arr type:array default:-- desc:轮播图片
  217. * @return name:content_arr type:array default:-- desc:详情图
  218. * @return name:item_list.spec_info type:array default:-- desc:规格型号设置;
  219. * @return name:item_list.goods_no type:string default:-- desc:规格货号
  220. * @return name:item_list.stock type:int default:-- desc:剩余库存
  221. * @return name:item_list.original_price type:float default:-- desc:原价
  222. * @return name:item_list.sell_price type:float default:-- desc:售价
  223. * @return name:item_list.spec_cover type:string default:-- desc:规格图
  224. * @return name:had_join type:array default:-- desc:已选规格
  225. * @return name:had_join.goods_spec type:array default:-- desc:规格
  226. */
  227. public function getGoodsDetail()
  228. {
  229. $goods_id = input('goods_id');
  230. $detail = StoreGoods::with(['itemList'])->field('id,name,label,status,is_deleted,sale_num,image,content,specs,cover')->where('id',$goods_id)->find()->toArray();
  231. if($detail['status'] == 0 || $detail['is_deleted'] != 0) $this->error('商品已下架');
  232. $detail['min_price'] = empty($detail['item_list']) ? 0 : min(array_column($detail['item_list'],'sell_price'));
  233. $detail['max_price'] = empty($detail['item_list']) ? 0 : max(array_column($detail['item_list'],'sell_price'));
  234. $detail['is_collect'] = UserCollect::checkCollectByType($this->user_id,6,$detail['id']);
  235. $detail['label_name'] = $detail['label'] ? explode(',',trim($detail['label'],',')): null;
  236. $detail['image_arr'] = $detail['image'] ? explode('|',$detail['image']): null;
  237. $detail['content_arr'] = $detail['content'] ? explode('|',$detail['content']): null;
  238. $detail['virtual'] = StoreGoods::getVirtualNum($detail['id']);
  239. //$detail['lists'] = json_decode($detail['lists'],true);
  240. $detail['specs'] = json_decode($detail['specs'],true);
  241. $detail['had_join'] = ShoppingTrolley::getTrolleyList(['t.user_id'=>$this->user_id,'t.goods_id'=>$detail['id']]);
  242. UserTrack::crateTrack($this->user_id,6,$goods_id,0);
  243. if($this->user_id){
  244. TopSearch::seIncHotNum($goods_id,'goods');
  245. }
  246. $this->success('oK',['goods_info'=>$detail]);
  247. }
  248. /**
  249. * @title 收藏||取消收藏
  250. * @desc 用户收藏
  251. * @author qc
  252. * @method POST
  253. * @url /api/Mall/goodsCollect
  254. * @header name:Authorization require:1 desc:Token
  255. * @param name:id type:int require:1 default:-- desc:商品id
  256. * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
  257. */
  258. public function goodsCollect(){
  259. $data = input();
  260. if($data['status'] == 0){
  261. UserCollect::where('user_id',$this->user_id)->where('coll_type',6)->where('coll_id',$data['id'])->delete();
  262. $coll_status = 0;
  263. }else{
  264. Data::save('UserCollect', [
  265. 'user_id'=>$this->user_id,
  266. 'coll_type'=>6,
  267. 'coll_id'=>$data['id'],
  268. 'children_id'=>0,
  269. 'create_int'=>time()],'user_id',['user_id'=>$this->user_id,'coll_type'=>6, 'coll_id'=>$data['id'],'children_id'=>0]);
  270. $coll_status = 1;
  271. }
  272. // $coll_status = UserCollect::plateCollect($this->user_id,6,input('post.id',0));
  273. $coll_status ? $this->success('收藏成功',['status'=>1]) : $this->success('取消收藏成功',['status'=>0]);
  274. }
  275. /**
  276. * @title 商品浏览记录
  277. * @desc 商品浏览记录
  278. * @author qc
  279. * @url /api/Mall/getGoodsHistory
  280. * @method GET
  281. * @header name:Authorization require:1 desc:Token
  282. * @param name:page type:int default:1 desc:页数
  283. * @param name:page_num type:int default:20 desc:每页数
  284. * @param name:name type:string require:0 default:-- desc:商品名称
  285. * @return name:name type:string default:-- desc:商品名称
  286. * @return name:cover type:string default:-- desc:商品封面图
  287. * @return name:desc type:string default:-- desc:商品简述(副标题)
  288. * @return name:low_price type:float default:-- desc:最低售价
  289. * @return name:is_normal type:int default:-- desc:是否是正常记录(0已删除或是已禁用1可以正常跳转详情)
  290. */
  291. public function getGoodsHistory()
  292. {
  293. $sel_where = [];
  294. $sel_where[] = ['t.user_id','=',$this->user_id];
  295. $sel_where[] = ['t.type','=',6];
  296. $title = input('get.name');
  297. if($title) $sel_where[] = ['i.name','like','%'.$title.'%'];
  298. $list = UserTrack::where($sel_where)
  299. ->alias('t')
  300. ->field('t.id,t.update_int,t.date,t.first_id goods_id,i.name,i.cover,i.low_price')
  301. ->leftJoin('StoreGoods i','t.first_id = i.id')
  302. ->order('update_int desc')
  303. ->limit($this->off_set,$this->page_num)
  304. ->select()->toArray();
  305. foreach ($list as &$v) {
  306. $v['time'] = date("Y-m-d H:i:s",$v['update_int']);
  307. $v['is_normal'] = CheckPower::checkModulesPower($v['goods_id'],0,$this->request->controller());
  308. }
  309. $total_num = UserTrack::where($sel_where) ->alias('t') ->leftJoin('StoreGoods i','t.first_id = i.id')->count();
  310. $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
  311. }
  312. /**
  313. * @title 我的收藏列表
  314. * @desc 商品收藏列表
  315. * @author qc
  316. * @url /api/Mall/getGoodsCollectList
  317. * @method GET
  318. * @header name:Authorization require:1 desc:Token
  319. * @param name:page type:int default:1 desc:页数
  320. * @param name:page_num type:int default:20 desc:每页数
  321. * @param name:name type:string require:0 default:-- desc:商品名称
  322. * @return name:name type:string default:-- desc:商品名称
  323. * @return name:cover type:string default:-- desc:商品封面图
  324. * @return name:desc type:string default:-- desc:商品简述(副标题)
  325. * @return name:low_price type:float default:-- desc:最低售价
  326. * @return name:is_normal type:int default:-- desc:是否是正常记录(0已删除或是已禁用1可以正常跳转详情)
  327. */
  328. public function getGoodsCollectList()
  329. {
  330. $sel_where = [];
  331. $title = input('get.name');
  332. if($title) $sel_where[] = ['i.name','like','%'.$title.'%'];
  333. $sel_where[] = ['t.user_id','=',$this->user_id];
  334. $sel_where[] = ['t.coll_type','=',6];
  335. $list = UserCollect::where($sel_where)
  336. ->alias('t')
  337. ->field('t.id,t.create_at,t.coll_id goods_id,i.name,i.cover,i.low_price')
  338. ->leftJoin('StoreGoods i','t.coll_id = i.id')
  339. ->order('id desc')
  340. ->limit($this->off_set,$this->page_num)
  341. ->select()->toArray();
  342. array_walk($list,function (&$v){
  343. $v['is_normal'] = CheckPower::checkModulesPower($v['goods_id'],0,$this->request->controller());
  344. });
  345. $total_num = UserCollect::where($sel_where) ->alias('t') ->leftJoin('StoreGoods i','t.coll_id = i.id')->count();
  346. $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
  347. }
  348. /**
  349. * @title 我的发布
  350. * @desc 我的发布
  351. * @return void :name type:string default:-- desc:商品名称
  352. * @throws \think\db\exception\DataNotFoundException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. * @throws \think\exception\DbException
  355. * @author qc
  356. * @url /api/Mall/getOwnGoodsList
  357. * @method GET
  358. * @header name:Authorization require:1 desc:Token
  359. */
  360. public function getOwnGoodsList()
  361. {
  362. $list = StoreGoods::field('id,name,desc,create_at,low_price,cover')
  363. ->where(['user_id'=>$this->user_id])
  364. ->order(['sort'=>'desc','id'=>'desc'])
  365. ->limit($this->off_set,$this->page_num)
  366. ->select()->toArray();
  367. array_walk($list,function (&$v){
  368. $v['is_normal'] = CheckPower::checkModulesPower($v['id'],0,$this->request->controller());
  369. });
  370. $total_num = StoreGoods::where(['user_id'=>$this->user_id])->count();
  371. $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
  372. }
  373. /**
  374. * @title 批量删除||批量取消
  375. * @desc 删除||取消
  376. * @author qc
  377. * @method POST
  378. * @url /api/Mall/batchesDel
  379. * @header name:Authorization require:1 desc:Token
  380. * @param name:id type:string default:1 desc:记录id【多个用逗号隔开=>'1,2'】
  381. * @param name:type type:string default:1 desc:类型【1我的收藏】
  382. */
  383. public function batchesDel()
  384. {
  385. $type = input('post.type',1);
  386. $del_where = [];
  387. $del_where[] = ['id','in',input('post.id')];
  388. $del_where[] = ['user_id','=',$this->user_id];
  389. if(in_array($type,[1])) UserCollect::where($del_where)->delete();
  390. $this->success('操作成功');
  391. }
  392. /**
  393. * @title 获取角标数量
  394. * @desc 获取角标数量
  395. * @author qc
  396. * @method GET
  397. * @url /api/Mall/getMarkNum
  398. * @header name:Authorization require:1 desc:Token
  399. * @return name:trolley_num type:int default:-- desc:购物车商品数量
  400. * @return name:no_pay_num type:int default:-- desc:未支付订单数量
  401. * @return name:no_express_num type:int default:-- desc:未发货订单数量
  402. * @return name:express_num type:int default:-- desc:发货订单数量
  403. * @return name:finish_num type:int default:-- desc:已完成订单数量
  404. * @return name:refund_num type:int default:-- desc:退款/售后订单数量
  405. */
  406. public function getMarkNum()
  407. {
  408. $trolley_num = ShoppingTrolley::where(['user_id'=>$this->user_id])->sum('num');
  409. $no_pay_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>0,'cancel_state'=>0,'status'=>0])->count();
  410. // $no_express_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>0,'cancel_state'=>0])
  411. // ->where('refund_state','in','0')
  412. // ->where('status',1)
  413. // ->count();
  414. $no_express_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>0,'cancel_state'=>0])
  415. ->where('status',1)
  416. ->where('refund_state','in','0')
  417. ->count();
  418. $express_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>1,'cancel_state'=>0])
  419. ->where('refund_state','in','0,1,2')
  420. ->count();
  421. // $finish_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>2,'cancel_state'=>0])
  422. // ->where('refund_state','in','0,1,2')
  423. // ->count();
  424. $finish_num = 0;
  425. $refund_num = GoodsOrder::where(['o.user_id'=>$this->user_id,'o.is_deleted'=>0,'o.pay_state'=>1,'o.cancel_state'=>0])->alias('o')
  426. ->leftJoin('StoreOrderRefund r','r.order_id = o.id')
  427. ->where('o.refund_state','in','1,2,3')
  428. ->where('r.status','in','0,1,2,4')
  429. ->count();
  430. $this->success('ok',compact(['trolley_num','no_pay_num','no_express_num','express_num','finish_num','refund_num']));
  431. }
  432. }