123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?php
- namespace app\api\controller;
- use app\common\model\GoodsCate;
- use app\common\model\GoodsOrder;
- use app\common\model\ShoppingTrolley;
- use app\common\model\StoreGoods;
- use app\common\model\TopSearch;
- use app\common\model\UserCollect;
- use app\common\model\UserMessage;
- use app\common\model\UserSearch;
- use app\common\model\UserTrack;
- use app\common\service\CheckPower;
- use app\common\service\UserSynth;
- use library\tools\Data;
- use think\Db;
- /**
- * @title 商城
- * @controller Mall
- * @group base
- */
- class Mall extends Base
- {
- // 需要登录的
- protected $need_login = [
- 'goodsCollect',
- 'getGoodsHistory',
- 'getGoodsCollectList',
- 'getOwnGoodsList',
- 'batchesDel',
- ];
- public function initialize(){
- parent::initialize();
- parent::setUid();
- }
- /**
- * @title 获取商品全部分类
- * @desc 获取商品分类
- * @author qc
- * @url /api/Mall/getGoodsCate
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:id type:int default:-- desc:分类id
- * @return name:title type:srting default:-- desc:标题
- * @return name:lev type:srting default:-- desc:分类等级(1,2,3,4)
- * @return name:children type:array default:-- desc:下级分类(数据同上)
- */
- public function getGoodsCate()
- {
- $list = GoodsCate::getCateTree();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 获取商品二级分类
- * @desc 获取商品二级分类
- * @author qc
- * @url /api/Mall/getSecondCate
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int default:-- desc:一级分类id
- * @return name:title type:srting default:-- desc:标题
- * @return name:lev type:srting default:-- desc:分类等级(1,2)
- */
- public function getSecondCate()
- {
- $list = GoodsCate::field('id,title,pid,lev')->where(['status'=>1,'is_deleted'=>0,'pid'=>input('get.id')])->order('sort desc ,id desc')->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 获取第三级分类
- * @desc 获取第三级分类
- * @author qc
- * @url /api/Mall/getThirdCate
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:fid type:int default:-- desc:一级分类id
- * @param name:pid type:int default:-- desc:二级分类id【不传查一级分类下全部的三级分类】
- * @return name:id type:int default:-- desc:分类id
- * @return name:title type:string default:0 desc:分类名称
- * @return name:lev type:int default:0 desc:分类等级
- * @return name:children type:array default:0 desc:下级分类
- */
- public function getThirdCate()
- {
- $where = [];
- $where[]=['is_deleted','=',0];
- $where[]=['pid','=',input('fid')];
- if(input('pid')) $where[] = ['id','=',input('pid')];
- $list = GoodsCate::where($where)->field('id,title,pid,lev,logo')->order('sort desc,id asc')->select()->toArray();
- foreach ($list as &$v) {
- $v['children'] = GoodsCate::where([['is_deleted','=',0],['pid','=',$v['id']]])->field('id,title,pid,lev,logo')->order('sort desc,id asc')->select()->toArray();
- }
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 商城商品推荐
- * @desc 商城商品推荐
- * @author qc
- * @method GET
- * @url /api/Mall/getRecommendList
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:0 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:isShape type:bool default:true desc:展示样式(传什么返回什么)
- * @param name:rand_user type:string default:-- desc:随机id(非登录状态必传,page=1时生成(a123),其他页数(a123),一直查询到没有数据)
- * @return name:id type:int default:-- desc:商品id
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:封面
- * @return name:sale_num type:int default:-- desc:销量
- * @return name:virtual type:int default:-- desc:虚拟销量
- * @return name:label_name type:array default:-- desc:标签
- * @return name:low_price type:float default:-- desc:价格
- * @return name:min_price type:float default:-- desc:最低价格
- * @return name:max_price type:float default:-- desc:最高价格
- */
- public function getRecommendList()
- {
- $user_id = $this->user_id ? $this->user_id : input('rand_user');
- if(!$user_id) $this->error('参数错误');
- $list = UserSynth::getRecommendList($user_id,'goods',$this->page,$this->page_num,[],input('sort_type',1));
- $list['isShape'] = input('isShape',true);
- if($list['code'] != 200) $this->error($list['msg']);
- foreach ($list['list'] as &$v) {
- $v['isShape'] = input('isShape',true);
- }
- $this->success('ok',$list);
- }
- /**
- * @title 商品合集列表
- * @desc 获取商品列表
- * @author qc
- * @url /api/Mall/getGoodsList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:isShape type:bool default:true desc:展示样式(传什么返回什么)
- * @param name:third_classify type:int require:0 default:-- desc:三级分类id
- * @param name:sort_type type:int default:1 desc:排序--1默认2时间升序3时间降序
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面图
- * @return name:label_name type:array default:-- desc:标签
- * @return name:low_price type:float default:-- desc:最低售价
- * @return name:sale_num type:int default:-- desc:销量
- * @return name:virtual type:int default:-- desc:虚拟销量
- * @return name:min_price type:float default:-- desc:最低价格
- * @return name:max_price type:float default:-- desc:最高价格
- * @return name:total_num type:int default:-- desc:总数
- */
- public function getGoodsList(){
- $input = input('get.');
- $sel_where = [];
- $sel_where[] = ['status','=',1];
- $sel_where[] = ['is_deleted','=',0];
- $sort_type = input('get.sort_type',1);
- if(isset_full($input,'first_classify'))$sel_where[] = ['first_classify','=',$input['first_classify']];
- if(isset_full($input,'second_classify'))$sel_where[] = ['second_classify','=',$input['second_classify']];
- if(isset_full($input,'third_classify'))$sel_where[] = ['third_classify','=',$input['third_classify']];
- //if(!isset($sel_where['third_classify'])) $this->error('参数错误');
- switch ($sort_type) {
- case 1:
- $order = ['sort'=>'desc','id'=>'asc'];
- break;
- case 2:
- $order = ['id'=>'asc','sort'=>'desc'];
- break;
- case 3:
- $order = ['id'=>'desc','sort'=>'desc'];
- break;
- }
- $class = GoodsCate::where('id',$input['third_classify'])->find();
- if($class){
- $class_name = $class['title'];
- }else{
- $class_name = null;
- }
- $list = StoreGoods::with(['itemList'])
- ->field('id,name,label,sale_num,cover,specs')
- ->where($sel_where)->limit($this->off_set,$this->page_num)
- ->order($order)
- ->select()->toArray();
- foreach ($list as &$v){
- $v['isShape'] = input('isShape',true);
- $v['virtual'] = StoreGoods::getVirtualNum($v['id']);
- //$v['low_original'] = empty($v['item_list']) ? 0 : min(array_column($v['item_list'],'original_price'));
- $v['min_price'] = empty($v['item_list']) ? 0 : min(array_column($v['item_list'],'sell_price'));
- $v['max_price'] = empty($v['item_list']) ? 0 : max(array_column($v['item_list'],'sell_price'));
- //$v['total_num'] = empty($v['item_list']) ? 0 :array_sum(array_column($v['item_list'],'base_stock'));
- $v['specs'] = json_decode($v['specs'],true);
- $v['label_name'] = $v['label'] ? explode(',',trim($v['label'],',')): null;
- //unset($v['item_list']);
- }
- $total_num = StoreGoods::where($sel_where)->count();
- $this->success('ok',['list'=>$list,'isShape'=>input('isShape',true),'class_name'=>$class_name,'total_count'=>$total_num,'page_num'=>$this->page_num]);
- }
- /**
- * @title 获取商品详情
- * @desc 获取商品详情
- * @author qc
- * @url /api/Mall/getGoodsDetail
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:0 desc:商品id
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面图
- * @return name:desc type:string default:-- desc:商品简述(副标题)
- * @return name:min_price type:float default:-- desc:最低价格
- * @return name:max_price type:float default:-- desc:最高价格
- * @return name:detail type:string default:-- desc:详情(富文本)
- * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
- * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
- * @return name:postage type:float default:-- desc:运费(freight_type=0)
- * @return name:is_collect type:int default:-- desc:是否收藏(1是0否)
- * @return name:label_name type:array default:-- desc:标签
- * @return name:sale_num type:int default:-- desc:销量
- * @return name:virtual type:int default:-- desc:虚拟销量
- * @return name:image_arr type:array default:-- desc:轮播图片
- * @return name:content_arr type:array default:-- desc:详情图
- * @return name:item_list.spec_info type:array default:-- desc:规格型号设置;
- * @return name:item_list.goods_no type:string default:-- desc:规格货号
- * @return name:item_list.stock type:int default:-- desc:剩余库存
- * @return name:item_list.original_price type:float default:-- desc:原价
- * @return name:item_list.sell_price type:float default:-- desc:售价
- * @return name:item_list.spec_cover type:string default:-- desc:规格图
- * @return name:had_join type:array default:-- desc:已选规格
- * @return name:had_join.goods_spec type:array default:-- desc:规格
- */
- public function getGoodsDetail()
- {
- $goods_id = input('goods_id');
- $detail = StoreGoods::with(['itemList'])->field('id,name,label,status,is_deleted,sale_num,image,content,specs,cover')->where('id',$goods_id)->find()->toArray();
- if($detail['status'] == 0 || $detail['is_deleted'] != 0) $this->error('商品已下架');
- $detail['min_price'] = empty($detail['item_list']) ? 0 : min(array_column($detail['item_list'],'sell_price'));
- $detail['max_price'] = empty($detail['item_list']) ? 0 : max(array_column($detail['item_list'],'sell_price'));
- $detail['is_collect'] = UserCollect::checkCollectByType($this->user_id,6,$detail['id']);
- $detail['label_name'] = $detail['label'] ? explode(',',trim($detail['label'],',')): null;
- $detail['image_arr'] = $detail['image'] ? explode('|',$detail['image']): null;
- $detail['content_arr'] = $detail['content'] ? explode('|',$detail['content']): null;
- $detail['virtual'] = StoreGoods::getVirtualNum($detail['id']);
- //$detail['lists'] = json_decode($detail['lists'],true);
- $detail['specs'] = json_decode($detail['specs'],true);
- $detail['had_join'] = ShoppingTrolley::getTrolleyList(['t.user_id'=>$this->user_id,'t.goods_id'=>$detail['id']]);
- UserTrack::crateTrack($this->user_id,6,$goods_id,0);
- if($this->user_id){
- TopSearch::seIncHotNum($goods_id,'goods');
- }
- $this->success('oK',['goods_info'=>$detail]);
- }
- /**
- * @title 收藏||取消收藏
- * @desc 用户收藏
- * @author qc
- * @method POST
- * @url /api/Mall/goodsCollect
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:商品id
- * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
- */
- public function goodsCollect(){
- $data = input();
- if($data['status'] == 0){
- UserCollect::where('user_id',$this->user_id)->where('coll_type',6)->where('coll_id',$data['id'])->delete();
- $coll_status = 0;
- }else{
- Data::save('UserCollect', [
- 'user_id'=>$this->user_id,
- 'coll_type'=>6,
- 'coll_id'=>$data['id'],
- 'children_id'=>0,
- 'create_int'=>time()],'user_id',['user_id'=>$this->user_id,'coll_type'=>6, 'coll_id'=>$data['id'],'children_id'=>0]);
- $coll_status = 1;
- }
- // $coll_status = UserCollect::plateCollect($this->user_id,6,input('post.id',0));
- $coll_status ? $this->success('收藏成功',['status'=>1]) : $this->success('取消收藏成功',['status'=>0]);
- }
- /**
- * @title 商品浏览记录
- * @desc 商品浏览记录
- * @author qc
- * @url /api/Mall/getGoodsHistory
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:name type:string require:0 default:-- desc:商品名称
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面图
- * @return name:desc type:string default:-- desc:商品简述(副标题)
- * @return name:low_price type:float default:-- desc:最低售价
- * @return name:is_normal type:int default:-- desc:是否是正常记录(0已删除或是已禁用1可以正常跳转详情)
- */
- public function getGoodsHistory()
- {
- $sel_where = [];
- $sel_where[] = ['t.user_id','=',$this->user_id];
- $sel_where[] = ['t.type','=',6];
- $title = input('get.name');
- if($title) $sel_where[] = ['i.name','like','%'.$title.'%'];
- $list = UserTrack::where($sel_where)
- ->alias('t')
- ->field('t.id,t.update_int,t.date,t.first_id goods_id,i.name,i.cover,i.low_price')
- ->leftJoin('StoreGoods i','t.first_id = i.id')
- ->order('update_int desc')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- foreach ($list as &$v) {
- $v['time'] = date("Y-m-d H:i:s",$v['update_int']);
- $v['is_normal'] = CheckPower::checkModulesPower($v['goods_id'],0,$this->request->controller());
- }
- $total_num = UserTrack::where($sel_where) ->alias('t') ->leftJoin('StoreGoods i','t.first_id = i.id')->count();
- $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
- }
- /**
- * @title 我的收藏列表
- * @desc 商品收藏列表
- * @author qc
- * @url /api/Mall/getGoodsCollectList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:name type:string require:0 default:-- desc:商品名称
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面图
- * @return name:desc type:string default:-- desc:商品简述(副标题)
- * @return name:low_price type:float default:-- desc:最低售价
- * @return name:is_normal type:int default:-- desc:是否是正常记录(0已删除或是已禁用1可以正常跳转详情)
- */
- public function getGoodsCollectList()
- {
- $sel_where = [];
- $title = input('get.name');
- if($title) $sel_where[] = ['i.name','like','%'.$title.'%'];
- $sel_where[] = ['t.user_id','=',$this->user_id];
- $sel_where[] = ['t.coll_type','=',6];
- $list = UserCollect::where($sel_where)
- ->alias('t')
- ->field('t.id,t.create_at,t.coll_id goods_id,i.name,i.cover,i.low_price')
- ->leftJoin('StoreGoods i','t.coll_id = i.id')
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- array_walk($list,function (&$v){
- $v['is_normal'] = CheckPower::checkModulesPower($v['goods_id'],0,$this->request->controller());
- });
- $total_num = UserCollect::where($sel_where) ->alias('t') ->leftJoin('StoreGoods i','t.coll_id = i.id')->count();
- $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
- }
- /**
- * @title 我的发布
- * @desc 我的发布
- * @return void :name type:string default:-- desc:商品名称
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @author qc
- * @url /api/Mall/getOwnGoodsList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- */
- public function getOwnGoodsList()
- {
- $list = StoreGoods::field('id,name,desc,create_at,low_price,cover')
- ->where(['user_id'=>$this->user_id])
- ->order(['sort'=>'desc','id'=>'desc'])
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- array_walk($list,function (&$v){
- $v['is_normal'] = CheckPower::checkModulesPower($v['id'],0,$this->request->controller());
- });
- $total_num = StoreGoods::where(['user_id'=>$this->user_id])->count();
- $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
- }
- /**
- * @title 批量删除||批量取消
- * @desc 删除||取消
- * @author qc
- * @method POST
- * @url /api/Mall/batchesDel
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:string default:1 desc:记录id【多个用逗号隔开=>'1,2'】
- * @param name:type type:string default:1 desc:类型【1我的收藏】
- */
- public function batchesDel()
- {
- $type = input('post.type',1);
- $del_where = [];
- $del_where[] = ['id','in',input('post.id')];
- $del_where[] = ['user_id','=',$this->user_id];
- if(in_array($type,[1])) UserCollect::where($del_where)->delete();
- $this->success('操作成功');
- }
- /**
- * @title 获取角标数量
- * @desc 获取角标数量
- * @author qc
- * @method GET
- * @url /api/Mall/getMarkNum
- * @header name:Authorization require:1 desc:Token
- * @return name:trolley_num type:int default:-- desc:购物车商品数量
- * @return name:no_pay_num type:int default:-- desc:未支付订单数量
- * @return name:no_express_num type:int default:-- desc:未发货订单数量
- * @return name:express_num type:int default:-- desc:发货订单数量
- * @return name:finish_num type:int default:-- desc:已完成订单数量
- * @return name:refund_num type:int default:-- desc:退款/售后订单数量
- */
- public function getMarkNum()
- {
- $trolley_num = ShoppingTrolley::where(['user_id'=>$this->user_id])->sum('num');
- $no_pay_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>0,'cancel_state'=>0,'status'=>0])->count();
- // $no_express_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>0,'cancel_state'=>0])
- // ->where('refund_state','in','0')
- // ->where('status',1)
- // ->count();
- $no_express_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>0,'cancel_state'=>0])
- ->where('status',1)
- ->where('refund_state','in','0')
- ->count();
- $express_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>1,'cancel_state'=>0])
- ->where('refund_state','in','0,1,2')
- ->count();
- // $finish_num = GoodsOrder::where(['user_id'=>$this->user_id,'is_deleted'=>0,'pay_state'=>1,'express_state'=>2,'cancel_state'=>0])
- // ->where('refund_state','in','0,1,2')
- // ->count();
- $finish_num = 0;
- $refund_num = GoodsOrder::where(['o.user_id'=>$this->user_id,'o.is_deleted'=>0,'o.pay_state'=>1,'o.cancel_state'=>0])->alias('o')
- ->leftJoin('StoreOrderRefund r','r.order_id = o.id')
- ->where('o.refund_state','in','1,2,3')
- ->where('r.status','in','0,1,2,4')
- ->count();
- $this->success('ok',compact(['trolley_num','no_pay_num','no_express_num','express_num','finish_num','refund_num']));
- }
- }
|