123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace app\api\controller;
- use app\common\model\NewsCate;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\model\UserCollect;
- use app\common\model\InformationArticle;
- use app\common\model\VideoCate;
- use app\common\model\VideoIntro;
- use app\common\model\GoodsCate;
- use think\Db;
- use library\tools\Data;
- /**
- * @title 收藏
- * @controller Collect
- * @group base
- */
- class Collect extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 获取收藏标题设置
- * @desc 获取收藏标题设置
- * @author qc
- * @method GET
- * @url /api/Collect/getCollectSet
- * @header name:Authorization require:1 desc:Token
- * @return name:title type:string default:-- desc:标题
- * @return name:num type:string default:-- desc:收藏数量
- * @return name:type type:string default:-- desc:类型
- * @return name:time_arr type:array default:-- desc:时间搜索数组
- * @return name:cate_arr type:array default:-- desc:收藏分类
- * @return name:cate_arr.time_key type:int default:-- desc:时间筛选查询使用
- * @return name:cate_arr.id type:int default:-- desc:收藏分类id
- * @return name:cate_arr.title type:array default:-- desc:分类名
- * @return name:cate_arr.logo type:string default:-- desc:分类logo
- * @return name:all_num type:int default:-- desc:收藏总量
- */
- public function getCollectSet()
- {
- $time_arr = [
- ['name'=>'7天内' , 'time_key'=>1],
- ['name'=>'30天内' , 'time_key'=>2],
- ['name'=>'90天内', 'time_key'=>3],
- ['name'=>'半年前', 'time_key'=>4],
- ['name'=>'一年前', 'time_key'=>5]];
- $collect_all = [
- ['title'=>'宝贝','type'=>1,'time_arr'=>$time_arr,'cate_arr'=>[]],
- ['title'=>'资讯','type'=>2,'time_arr'=>$time_arr,'cate_arr'=>[]],
- // ['title'=>'视频','type'=>3,'time_arr'=>$time_arr,'cate_arr'=>[]],
- ];
- $all_num = UserCollect::where(['user_id'=>$this->user_id])->count();
- foreach ($collect_all as &$value)
- {
- $coll_list = UserCollect::field('cate_id')->where(['user_id'=>$this->user_id,'coll_type'=>$value['type']])->group('cate_id')->select();
- if(!$coll_list) continue;
- $coll_list = $coll_list->toArray();
- switch ($value['type']){
- case 1:
- $value['cate_arr'] = GoodsCate::field('id,title,logo')->where('id','in',array_column($coll_list,'cate_id'))->select();
- break;
- case 2:
- $value['cate_arr'] = NewsCate::field('id,title,logo')->where('id','in',array_column($coll_list,'cate_id'))->select();
- break;
- case 3:
- $value['cate_arr'] = VideoCate::field('id,title,logo')->where('id','in',array_column($coll_list,'cate_id'))->select();
- break;
- }
- }
- $this->success('ok',['collect'=>$collect_all,'all_num'=>$all_num]);
- }
- /**
- * @title 用户收藏(单个收藏)
- * @desc 用户收藏(单个收藏)
- * @author qc
- * @method POST
- * @url /api/Collect/userCollect
- * @header name:Authorization require:1 desc:Token
- * @param name:coll_type type:int require:1 default:-- desc:类型(1商品2资讯3视频)
- * @param name:coll_id type:int require:1 default:-- desc:收藏id
- * @param name:cate_id type:int require:1 default:-- desc:收藏分类id(字段:first_classify)
- * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
- */
- public function userCollect()
- {
- $coll_type = input('post.coll_type',0);
- $coll_id = input('post.coll_id',0);
- $cate_id = input('post.cate_id',0);
- $collect_check = UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->find();
- if($collect_check) {
- UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->delete();
- $this->success('取消收藏成功',['status'=>0]);
- }else{
- Data::save('UserCollect', [
- 'user_id'=>$this->user_id,
- 'coll_type'=>$coll_type,
- 'coll_id'=>$coll_id,
- 'cate_id'=>$cate_id,
- 'create_int'=>time()],'user_id',['user_id'=>$this->user_id,'coll_type'=>$coll_type, 'coll_id'=>$coll_id]);
- $this->success('收藏成功',['status'=>1]);
- }
- }
- /**
- * @title 用户收藏(批量收藏)
- * @desc 用户收藏(批量收藏)
- * @author qc
- * @method POST
- * @url /api/Collect/userBatchCollect
- * @header name:Authorization require:1 desc:Token
- * @param name:coll_type type:int require:1 default:-- desc:类型(1商品2资讯3视频)
- * @param name:ids type:string require:1 default:-- desc:收藏id(多个逗号隔开)
- */
- public function userBatchCollect()
- {
- $coll_type = input('post.coll_type',1);
- $ids = input('post.ids',0);
- //$ids=explode(',',$ids);
- $insert_data = [];
- switch ($coll_type){
- case 1:
- $batch_data = StoreGoods::field('id,first_classify')->where('id','in',$ids)->select();
- break;
- case 2:
- $batch_data = InformationArticle::field('id,first_classify')->where('id','in',$ids)->select();
- break;
- case 3:
- $batch_data = VideoIntro::field('id,first_classify')->where('id','in',$ids)->select();
- break;
- }
- foreach ($batch_data as $vv){
- $collect_check = UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$vv->id])->find();
- if($collect_check) continue;
- $vv->cate_id = $vv->first_classify;
- $insert_data[] = [
- 'user_id' => $this->user_id,
- 'coll_id' => $vv->id,
- 'coll_type' =>$coll_type,
- 'create_at' =>date('Y-m-d H:i:s'),
- 'create_int' =>time(),
- 'cate_id' =>$vv->cate_id,
- ];
- }
- if(!empty($insert_data)) (new UserCollect())->saveAll($insert_data);
- $this->success('收藏成功');
- }
- /**
- * @title 获取用户收藏列表
- * @desc 获取用户收藏列表
- * @author qc
- * @method GET
- * @url /api/Collect/getUserCollectList
- * @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:coll_type type:int require:1 default:-- desc:类型(1商品2资讯3视频)
- * @param name:cate_id type:int require:1 default:0 desc:收藏分类id
- * @param name:time_key type:int require:1 default:0 desc:时间time_key
- * @return name:id type:int default:-- desc:记录id
- * @return name:coll_id type:int default:-- desc:收藏id
- * @return name:coll_type type:int default:-- desc:收藏类型
- * @return name:goods_name type:string default:-- desc:商品名称(coll_type=1)
- * @return name:cover type:string default:-- desc:商品封面(coll_type=1)
- * @return name:line_money type:string default:-- desc:下划线价格(coll_type=1)
- * @return name:low_price type:float default:-- desc:最低价(coll_type=1)
- * @return name:article_title type:string default:-- desc:文章标题(coll_type=2)
- * @return name:article_cover type:string default:-- desc:文章封面(coll_type=2)
- * @return name:read_num type:int default:-- desc:阅读热人数(coll_type=2)
- * @return name:video_title type:string default:-- desc:视频标题(coll_type=3)
- * @return name:video_url type:string default:-- desc:视频链接(coll_type=3)
- */
- public function getUserCollectList()
- {
- $coll_type = input('get.coll_type',1);
- $cate_id = input('get.cate_id',0);
- $time_key = input('get.time_key',0);
- $sel_where = [];
- $sel_where[] = ['c.coll_type','=',$coll_type];
- $sel_where[] = ['c.user_id','=',$this->user_id];
- if($cate_id)$sel_where[] = ['c.cate_id','=',$cate_id];
- // 时间查询
- switch ($time_key){
- case 1:
- $sel_where[] = ['c.create_int','>',time() - 7 * 86400];
- break;
- case 2:
- $sel_where[] = ['c.create_int','>',time() - 30 * 86400];
- break;
- case 3:
- $sel_where[] = ['c.create_int','>',time() - 90 * 86400];
- break;
- case 4:
- $sel_where[] = ['c.create_int','between',[time()- 182 * 86400,time() - 365 * 86400]];
- break;
- case 5:
- $sel_where[] = ['c.create_int','<',time() - 365 * 86400];
- break;
- }
- switch ($coll_type){
- case 1 :
- $list = Db::name('user_collect')->alias('c')
- ->field('c.id,c.coll_id,c.coll_type,c.cate_id,g.name as goods_name, g.cover , g.low_price')
- ->join('store_goods g','c.coll_id = g.id','LEFT')
- ->where($sel_where)
- ->limit($this->off_set,$this->page_num)
- ->order('c.create_int desc')
- ->select();
- foreach ($list as &$v) {
- $v['line_money']=StoreGoodsItem::where(['goods_id'=>$v['coll_id']])->max('original_price');
- }
- break;
- case 2 :
- $list = Db::name('user_collect')->alias('c')
- ->field('c.id,c.coll_id,c.coll_type,c.cate_id,g.title as article_title ,g.cover as article_cover, g.read_num')
- ->join('information_article g','c.coll_id = g.id','LEFT')
- ->where($sel_where)
- ->limit($this->off_set,$this->page_num)
- ->order('c.create_int desc')
- ->select();
- break;
- case 3 :
- $list = Db::name('user_collect')->alias('c')
- ->field('c.id,c.coll_id,c.coll_type,c.cate_id,g.title as video_title , g.video_url')
- ->join('dd_video_intro g','c.coll_id = g.id','LEFT')
- ->where($sel_where)
- ->limit($this->off_set,$this->page_num)
- ->order('c.create_int desc')
- ->select();
- break;
- }
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 取消收藏
- * @desc 取消收藏
- * @author qc
- * @method POST
- * @url /api/Collect/cancelCollect
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:string : default:'' desc:需要删除的id记录(用逗号隔开)
- */
- public function cancelCollect()
- {
- $ids = input('post.ids','');
- if(!$ids) $this->error('请选择要取消收藏的记录');
- UserCollect::where('id','in',$ids)->where('user_id','=',$this->user_id)->delete();
- $this->success('取消成功');
- }
- /**
- * @title 取消收藏
- * @desc 根据类型和(商品id||资讯id||视频id)取消
- * @author qc
- * @method POST
- * @url /api/Collect/cancelCollectByType
- * @header name:Authorization require:1 desc:Token
- * @param name:coll_type type:int require:1 default:1s desc:类型(1商品2资讯3视频)
- * @param name:coll_id type:int default:1 desc:取消收藏的id(商品||资讯||视频)
- */
- public function cancelCollectByType()
- {
- $coll_type = input('post.coll_type',1);
- $coll_id = input('get.coll_id',0);
- UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->delete();
- $this->success('取消成功');
- }
- }
|