Mall.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\GoodsCate;
  4. use app\common\model\GoodsSeason;
  5. use app\common\model\GoodsServe;
  6. use app\common\model\StoreGoods;
  7. use app\common\model\User;
  8. use app\common\model\UserCollect;
  9. use app\common\model\UserSearch;
  10. use app\common\model\UserTrack;
  11. use app\common\service\Activity;
  12. use library\tools\Data;
  13. use think\Db;
  14. /**
  15. * @title 商城
  16. * @controller Mall
  17. * @group base
  18. */
  19. class Mall extends Base
  20. {
  21. // 需要登录的
  22. protected $need_login = ['goodsCollect'];
  23. public function initialize(){
  24. parent::initialize();
  25. parent::setUid();
  26. }
  27. /**
  28. * @title 获取商品分类
  29. * @desc 获取商品分类
  30. * @author qc
  31. * @url /api/Mall/getGoodsCate
  32. * @method GET
  33. * @header name:Authorization require:1 desc:Token
  34. * @return name:id type:int default:-- desc:分类id
  35. * @return name:title type:srting default:-- desc:标题
  36. * @return name:lev type:srting default:-- desc:分类等级(1,2,3,4)
  37. * @return name:children type:array default:-- desc:下级分类(数据同上)
  38. */
  39. public function getGoodsCate()
  40. {
  41. $list = GoodsCate::getCateTree();
  42. $this->success('ok',['list'=>$list]);
  43. }
  44. /**
  45. * @title 获取商品二级分类
  46. * @desc 获取商品二级分类
  47. * @author qc
  48. * @url /api/Mall/getSecondCate
  49. * @method GET
  50. * @header name:Authorization require:1 desc:Token
  51. * @param name:id type:int default:-- desc:一级分类id
  52. * @return name:title type:srting default:-- desc:标题
  53. * @return name:lev type:srting default:-- desc:分类等级(1,2,3,4)
  54. */
  55. public function getSecondCate()
  56. {
  57. $list = GoodsCate::field('id,title,pid,lev')->where(['status'=>1,'is_deleted'=>0,'pid'=>input('get.id')])->order('sort desc ,id desc')->select()->toArray();
  58. $this->success('ok',['list'=>$list]);
  59. }
  60. /**
  61. * @title 商城商品推荐
  62. * @desc 商城商品推荐
  63. * @author qc
  64. * @method GET
  65. * @url /api/Mall/getRecommendList
  66. * @header name:Authorization require:1 desc:Token
  67. * @param name:page type:int default:0 desc:页数
  68. * @param name:page_num type:int default:20 desc:每页数
  69. * @return name:id type:int default:-- desc:id
  70. * @return name:name type:string default:-- desc:商品名称
  71. * @return name:cover type:string default:-- desc:封面
  72. * @return name:low_price type:float default:-- desc:价格
  73. */
  74. public function getRecommendList()
  75. {
  76. $search_log = UserSearch::getSearchTitle($this->user_id,7);
  77. $search_arr = [];
  78. $where_str = 'b.id > 0';
  79. foreach ($search_log as $t){
  80. $search_arr[] = " b.name like '".'%'.$t."%'" .' ';
  81. }
  82. if(!empty($search_arr)) $where_str = implode(' OR ',$search_arr);
  83. $field = 'a.id,name,cover,low_price,IFNULL(is_recommend,0) is_recommend ';
  84. $table = 'dd_store_goods';
  85. $order_by_two = 'id';
  86. $sql = "SELECT $field FROM " .$table . " a LEFT JOIN ( SELECT id,IF(id,1,1) is_recommend FROM $table b WHERE ( $where_str )) AS c ON c.id = a.id WHERE a.status =1 AND a.is_deleted = 0 ORDER BY is_recommend DESC , $order_by_two desc LIMIT {$this->off_set},{$this->page_num}";
  87. $list = Db::query($sql);
  88. $this->success('ok',['list'=>$list]);
  89. }
  90. /**
  91. * @title 获取商品列表
  92. * @desc 获取商品列表
  93. * @author qc
  94. * @url /api/Mall/getGoodsList
  95. * @method GET
  96. * @header name:Authorization require:1 desc:Token
  97. * @param name:page type:int default:1 desc:页数
  98. * @param name:page_num type:int default:20 desc:每页数
  99. * @param name:goods_name type:string require:0 default:-- desc:商品名称
  100. * @param name:first_classify type:int require:0 default:-- desc:一级分类id
  101. * @param name:second_classify type:int require:0 default:-- desc:二级分类id
  102. * @param name:sort_type type:int default:1 desc:排序--1默认2时间升序3时间降序
  103. * @return name:name type:string default:-- desc:商品名称
  104. * @return name:cover type:string default:-- desc:商品封面图
  105. * @return name:desc type:string default:-- desc:商品简述(副标题)
  106. * @return name:image type:string default:-- desc:商品展示图[多张|隔开]
  107. * @return name:content type:string default:-- desc:商品详情图[多张|隔开]
  108. * @return name:low_price type:float default:-- desc:最低售价
  109. * @return name:max_price type:float default:-- desc:最高售价
  110. * @return name:low_original type:float default:-- desc:最低原价格
  111. * @return name:total_num type:int default:-- desc:商品总数量
  112. * @return name:detail type:int default:-- desc:商品详情
  113. * @return name:first_classify type:int require:0 default:-- desc:一级分类id
  114. * @return name:second_classify type:int require:0 default:-- desc:二级分类id
  115. * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
  116. * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
  117. * @return name:postage type:float default:-- desc:运费(freight_type=0)
  118. * @return name:item_list type:array default:-- desc:商品多规格列表
  119. * @return name:item_list.spec_info type:array default:-- desc:规格型号设置['规格组id'=>'规格id','规格组id'=>'规格id'];
  120. * @return name:item_list.goods_no type:string default:-- desc:规格货号
  121. * @return name:item_list.goods_title:string default:-- desc:规格标题
  122. * @return name:item_list.cover type:string default:-- desc:规格封面
  123. * @return name:item_list.stock type:int default:-- desc:剩余库存
  124. * @return name:item_list.original_price type:float default:-- desc:原价
  125. * @return name:item_list.sell_price type:float default:-- desc:售价
  126. * @return name:item_list.virtual type:int default:-- desc:虚拟销量
  127. * @return name:item_list.weight type:float default:-- desc:重量【kg】
  128. * @return name:total_num type:int default:-- desc:总数
  129. */
  130. public function getGoodsList(){
  131. $all_cate = GoodsCate::field('id,title')->select()->toArray();
  132. $all_cate = array_column($all_cate,null,'id');
  133. $input = input('get.');
  134. $sel_where = [];
  135. $sel_where[] = ['status','=',1];
  136. $sel_where[] = ['is_deleted','=',0];
  137. $sort_type = input('get.sort_type',1);
  138. if(isset_full($input,'goods_name')) {
  139. $sel_where[] = ['name','like',"%".$input['goods_name']."%"];
  140. if($this->user_id) UserSearch::saveSearchTitle($this->user_id,$input['goods_name'],7);
  141. }
  142. if(isset_full($input,'first_classify'))$sel_where[] = ['first_classify','=',$input['first_classify']];
  143. if(isset_full($input,'second_classify'))$sel_where[] = ['second_classify','=',$input['second_classify']];
  144. switch ($sort_type) {
  145. case 1:
  146. $order = ['sort'=>'desc','id'=>'desc'];
  147. break;
  148. case 2:
  149. $order = ['id'=>'asc','sort'=>'desc'];
  150. break;
  151. case 3:
  152. $order = ['id'=>'desc','sort'=>'desc'];
  153. break;
  154. }
  155. $list = StoreGoods::with(['itemList'])
  156. ->field('specs,lists',true)
  157. ->where($sel_where)->limit($this->off_set,$this->page_num)
  158. ->order($order)
  159. ->select()->toArray();
  160. foreach ($list as &$v){
  161. $v['first_classify_name'] = isset($all_cate[$v['first_classify']]['title']) ? $all_cate[$v['first_classify']]['title']:'';
  162. $v['second_classify_name'] = isset($all_cate[$v['second_classify']]['title']) ? $all_cate[$v['second_classify']]['title']:'';
  163. $v['low_original'] = empty($v['item_list']) ? 0 : min(array_column($v['item_list'],'original_price'));
  164. $v['low_price'] = empty($v['item_list']) ? 0 : min(array_column($v['item_list'],'sell_price'));
  165. $v['max_price'] = empty($v['item_list']) ? 0 : max(array_column($v['item_list'],'sell_price'));
  166. $v['total_num'] = empty($v['item_list']) ? 0 :array_sum(array_column($v['item_list'],'base_stock'));
  167. }
  168. $total_num = StoreGoods::where($sel_where)->count();
  169. $this->success('ok',['list'=>$list,'total_num'=>$total_num]);
  170. }
  171. /**
  172. * @title 获取商品详情
  173. * @desc 获取商品详情
  174. * @author qc
  175. * @url /api/Mall/getGoodsDetail
  176. * @method GET
  177. * @header name:Authorization require:1 desc:Token
  178. * @param name:goods_id type:int require:1 default:0 desc:商品id
  179. * @return name:name type:string default:-- desc:商品名称
  180. * @return name:cover type:string default:-- desc:商品封面图
  181. * @return name:desc type:string default:-- desc:商品简述(副标题)
  182. * @return name:low_price type:float default:-- desc:最低价格
  183. * @return name:max_price type:float default:-- desc:最高价格(下划线公用)
  184. * @return name:low_seckill type:float default:-- desc:最低秒杀价格(有秒杀活动)
  185. * @return name:low_group type:float default:-- desc:最低拼团价格(有拼团活动)
  186. * @return name:low_discount type:float default:-- desc:最低折扣价格(有折扣活动)
  187. * @return name:discount type:float default:-- desc:商品折扣(没有折扣为10,有折扣活动,例如75折,值为7.5)
  188. * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
  189. * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
  190. * @return name:postage type:float default:-- desc:运费(freight_type=0)
  191. * @return name:is_collect type:int default:-- desc:是否收藏(1是0否)
  192. * @return name:item_list type:array default:-- desc:商品多规格列表
  193. * @return name:item_list.spec_info type:array default:-- desc:规格型号设置;
  194. * @return name:item_list.goods_no type:string default:-- desc:规格货号
  195. * @return name:item_list.stock type:int default:-- desc:剩余库存
  196. * @return name:item_list.original_price type:float default:-- desc:原价
  197. * @return name:item_list.sell_price type:float default:-- desc:售价
  198. */
  199. public function getGoodsDetail()
  200. {
  201. $goods_id = input('goods_id');
  202. $detail = StoreGoods::with(['itemList'])->field('specs,lists',true)->where('id',$goods_id)->find()->toArray();
  203. $detail['max_price'] = max(array_column($detail['item_list'],'original_price'));
  204. $detail['is_collect'] = UserCollect::checkCollectByType($this->user_id,6,$detail['id']);
  205. UserTrack::crateTrack($this->user_id,6,$goods_id,0);
  206. $this->success('oK',['goods_info'=>$detail]);
  207. }
  208. /**
  209. * @title 收藏||取消收藏
  210. * @desc 用户收藏
  211. * @author qc
  212. * @method POST
  213. * @url /api/Mall/goodsCollect
  214. * @header name:Authorization require:1 desc:Token
  215. * @param name:id type:int require:1 default:-- desc:商品id
  216. * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
  217. */
  218. public function goodsCollect(){
  219. $coll_status = UserCollect::plateCollect($this->user_id,6,input('post.id',0));
  220. $coll_status ? $this->success('收藏成功',['status'=>1]) : $this->success('取消收藏成功',['status'=>0]);
  221. }
  222. /**
  223. * @title 商品浏览记录
  224. * @desc 商品浏览记录
  225. * @author qc
  226. * @url /api/Mall/getGoodsHistory
  227. * @method GET
  228. * @header name:Authorization require:1 desc:Token
  229. * @param name:page type:int default:1 desc:页数
  230. * @param name:page_num type:int default:20 desc:每页数
  231. * @param name:name type:string require:0 default:-- desc:商品名称
  232. * @return name:name type:string default:-- desc:商品名称
  233. * @return name:cover type:string default:-- desc:商品封面图
  234. * @return name:desc type:string default:-- desc:商品简述(副标题)
  235. * @return name:low_price type:float default:-- desc:最低售价
  236. */
  237. public function getGoodsHistory()
  238. {
  239. $sel_where = [];
  240. $sel_where[] = ['t.user_id','=',$this->user_id];
  241. $sel_where[] = ['t.type','=',6];
  242. $title = input('get.name');
  243. if($title) $sel_where[] = ['i.name','like','%'.$title.'%'];
  244. $list = UserTrack::where($sel_where)
  245. ->alias('t')
  246. ->field('t.id,t.update_int,t.date,t.first_id goods_id,i.name,i.cover,i.low_price')
  247. ->leftJoin('StoreGoods i','t.first_id = i.id')
  248. ->order('update_int desc')
  249. ->limit($this->off_set,$this->page_num)
  250. ->select()->toArray();
  251. foreach ($list as &$v) {
  252. $v['time'] = date("Y-m-d H:i:s",$v['update_int']);
  253. }
  254. $this->success('ok',['list'=>$list]);
  255. }
  256. /**
  257. * @title 商品收藏列表
  258. * @desc 商品收藏列表
  259. * @author qc
  260. * @url /api/Mall/getGoodsCollectList
  261. * @method GET
  262. * @header name:Authorization require:1 desc:Token
  263. * @param name:page type:int default:1 desc:页数
  264. * @param name:page_num type:int default:20 desc:每页数
  265. * @param name:name type:string require:0 default:-- desc:商品名称
  266. * @return name:name type:string default:-- desc:商品名称
  267. * @return name:cover type:string default:-- desc:商品封面图
  268. * @return name:desc type:string default:-- desc:商品简述(副标题)
  269. * @return name:low_price type:float default:-- desc:最低售价
  270. */
  271. public function getGoodsCollectList()
  272. {
  273. $sel_where = [];
  274. $title = input('get.name');
  275. if($title) $sel_where[] = ['i.name','like','%'.$title.'%'];
  276. $sel_where[] = ['t.user_id','=',$this->user_id];
  277. $sel_where[] = ['t.coll_type','=',6];
  278. $list = UserCollect::where($sel_where)
  279. ->alias('t')
  280. ->field('t.id,t.create_at,t.coll_id goods_id,i.name,i.cover,i.low_price')
  281. ->leftJoin('StoreGoods i','t.coll_id = i.id')
  282. ->order('id desc')
  283. ->limit($this->off_set,$this->page_num)
  284. ->select()->toArray();
  285. $this->success('ok',['list'=>$list]);
  286. }
  287. /**
  288. * @title 获取我发布的商品列表
  289. * @desc 获取我发布的商品列表
  290. * @author qc
  291. * @url /api/Mall/getOwnGoodsList
  292. * @method GET
  293. * @header name:Authorization require:1 desc:Token
  294. * @param name:page type:int default:1 desc:页数
  295. * @param name:page_num type:int default:20 desc:每页数
  296. * @param name:name type:string require:0 default:-- desc:商品名称
  297. * @return name:name type:string default:-- desc:商品名称
  298. * @return name:cover type:string default:-- desc:商品封面图
  299. * @return name:desc type:string default:-- desc:商品简述(副标题)
  300. * @return name:low_price type:float default:-- desc:最低售价
  301. */
  302. public function getOwnGoodsList()
  303. {
  304. $list = StoreGoods::field('id,name,desc,create_at,low_price')
  305. ->where(['user_id'=>$this->user_id])
  306. ->order(['sort'=>'desc','id'=>'desc'])
  307. ->limit($this->off_set,$this->page_num)
  308. ->select()->toArray();
  309. $this->success('ok',['list'=>$list]);
  310. }
  311. }