Collect.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\NewsCate;
  4. use app\common\model\StoreGoods;
  5. use app\common\model\StoreGoodsItem;
  6. use app\common\model\UserCollect;
  7. use app\common\model\InformationArticle;
  8. use app\common\model\VideoCate;
  9. use app\common\model\VideoIntro;
  10. use app\common\model\GoodsCate;
  11. use think\Db;
  12. use library\tools\Data;
  13. /**
  14. * @title 收藏
  15. * @controller Collect
  16. * @group base
  17. */
  18. class Collect extends Base
  19. {
  20. public function initialize()
  21. {
  22. parent::initialize();
  23. parent::checkLogin();
  24. }
  25. /**
  26. * @title 获取收藏标题设置
  27. * @desc 获取收藏标题设置
  28. * @author qc
  29. * @method GET
  30. * @url /api/Collect/getCollectSet
  31. * @header name:Authorization require:1 desc:Token
  32. * @return name:title type:string default:-- desc:标题
  33. * @return name:num type:string default:-- desc:收藏数量
  34. * @return name:type type:string default:-- desc:类型
  35. * @return name:time_arr type:array default:-- desc:时间搜索数组
  36. * @return name:cate_arr type:array default:-- desc:收藏分类
  37. * @return name:cate_arr.time_key type:int default:-- desc:时间筛选查询使用
  38. * @return name:cate_arr.id type:int default:-- desc:收藏分类id
  39. * @return name:cate_arr.title type:array default:-- desc:分类名
  40. * @return name:cate_arr.logo type:string default:-- desc:分类logo
  41. * @return name:all_num type:int default:-- desc:收藏总量
  42. */
  43. public function getCollectSet()
  44. {
  45. $time_arr = [
  46. ['name'=>'7天内' , 'time_key'=>1],
  47. ['name'=>'30天内' , 'time_key'=>2],
  48. ['name'=>'90天内', 'time_key'=>3],
  49. ['name'=>'半年前', 'time_key'=>4],
  50. ['name'=>'一年前', 'time_key'=>5]];
  51. $collect_all = [
  52. ['title'=>'宝贝','type'=>1,'time_arr'=>$time_arr,'cate_arr'=>[]],
  53. ['title'=>'资讯','type'=>2,'time_arr'=>$time_arr,'cate_arr'=>[]],
  54. // ['title'=>'视频','type'=>3,'time_arr'=>$time_arr,'cate_arr'=>[]],
  55. ];
  56. $all_num = UserCollect::where(['user_id'=>$this->user_id])->count();
  57. foreach ($collect_all as &$value)
  58. {
  59. $coll_list = UserCollect::field('cate_id')->where(['user_id'=>$this->user_id,'coll_type'=>$value['type']])->group('cate_id')->select();
  60. if(!$coll_list) continue;
  61. $coll_list = $coll_list->toArray();
  62. switch ($value['type']){
  63. case 1:
  64. $value['cate_arr'] = GoodsCate::field('id,title,logo')->where('id','in',array_column($coll_list,'cate_id'))->select();
  65. break;
  66. case 2:
  67. $value['cate_arr'] = NewsCate::field('id,title,logo')->where('id','in',array_column($coll_list,'cate_id'))->select();
  68. break;
  69. case 3:
  70. $value['cate_arr'] = VideoCate::field('id,title,logo')->where('id','in',array_column($coll_list,'cate_id'))->select();
  71. break;
  72. }
  73. }
  74. $this->success('ok',['collect'=>$collect_all,'all_num'=>$all_num]);
  75. }
  76. /**
  77. * @title 用户收藏(单个收藏)
  78. * @desc 用户收藏(单个收藏)
  79. * @author qc
  80. * @method POST
  81. * @url /api/Collect/userCollect
  82. * @header name:Authorization require:1 desc:Token
  83. * @param name:coll_type type:int require:1 default:-- desc:类型(1商品2资讯3视频)
  84. * @param name:coll_id type:int require:1 default:-- desc:收藏id
  85. * @param name:cate_id type:int require:1 default:-- desc:收藏分类id(字段:first_classify)
  86. * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
  87. */
  88. public function userCollect()
  89. {
  90. $coll_type = input('post.coll_type',0);
  91. $coll_id = input('post.coll_id',0);
  92. $cate_id = input('post.cate_id',0);
  93. $collect_check = UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->find();
  94. if($collect_check) {
  95. UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->delete();
  96. $this->success('取消收藏成功',['status'=>0]);
  97. }else{
  98. Data::save('UserCollect', [
  99. 'user_id'=>$this->user_id,
  100. 'coll_type'=>$coll_type,
  101. 'coll_id'=>$coll_id,
  102. 'cate_id'=>$cate_id,
  103. 'create_int'=>time()],'user_id',['user_id'=>$this->user_id,'coll_type'=>$coll_type, 'coll_id'=>$coll_id]);
  104. $this->success('收藏成功',['status'=>1]);
  105. }
  106. }
  107. /**
  108. * @title 用户收藏(批量收藏)
  109. * @desc 用户收藏(批量收藏)
  110. * @author qc
  111. * @method POST
  112. * @url /api/Collect/userBatchCollect
  113. * @header name:Authorization require:1 desc:Token
  114. * @param name:coll_type type:int require:1 default:-- desc:类型(1商品2资讯3视频)
  115. * @param name:ids type:string require:1 default:-- desc:收藏id(多个逗号隔开)
  116. */
  117. public function userBatchCollect()
  118. {
  119. $coll_type = input('post.coll_type',1);
  120. $ids = input('post.ids',0);
  121. //$ids=explode(',',$ids);
  122. $insert_data = [];
  123. switch ($coll_type){
  124. case 1:
  125. $batch_data = StoreGoods::field('id,first_classify')->where('id','in',$ids)->select();
  126. break;
  127. case 2:
  128. $batch_data = InformationArticle::field('id,first_classify')->where('id','in',$ids)->select();
  129. break;
  130. case 3:
  131. $batch_data = VideoIntro::field('id,first_classify')->where('id','in',$ids)->select();
  132. break;
  133. }
  134. foreach ($batch_data as $vv){
  135. $collect_check = UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$vv->id])->find();
  136. if($collect_check) continue;
  137. $vv->cate_id = $vv->first_classify;
  138. $insert_data[] = [
  139. 'user_id' => $this->user_id,
  140. 'coll_id' => $vv->id,
  141. 'coll_type' =>$coll_type,
  142. 'create_at' =>date('Y-m-d H:i:s'),
  143. 'create_int' =>time(),
  144. 'cate_id' =>$vv->cate_id,
  145. ];
  146. }
  147. if(!empty($insert_data)) (new UserCollect())->saveAll($insert_data);
  148. $this->success('收藏成功');
  149. }
  150. /**
  151. * @title 获取用户收藏列表
  152. * @desc 获取用户收藏列表
  153. * @author qc
  154. * @method GET
  155. * @url /api/Collect/getUserCollectList
  156. * @header name:Authorization require:1 desc:Token
  157. * @param name:page type:int default:1 desc:页数
  158. * @param name:page_num type:int : default:20 desc:每页数
  159. * @param name:coll_type type:int require:1 default:-- desc:类型(1商品2资讯3视频)
  160. * @param name:cate_id type:int require:1 default:0 desc:收藏分类id
  161. * @param name:time_key type:int require:1 default:0 desc:时间time_key
  162. * @return name:id type:int default:-- desc:记录id
  163. * @return name:coll_id type:int default:-- desc:收藏id
  164. * @return name:coll_type type:int default:-- desc:收藏类型
  165. * @return name:goods_name type:string default:-- desc:商品名称(coll_type=1)
  166. * @return name:cover type:string default:-- desc:商品封面(coll_type=1)
  167. * @return name:line_money type:string default:-- desc:下划线价格(coll_type=1)
  168. * @return name:low_price type:float default:-- desc:最低价(coll_type=1)
  169. * @return name:article_title type:string default:-- desc:文章标题(coll_type=2)
  170. * @return name:article_cover type:string default:-- desc:文章封面(coll_type=2)
  171. * @return name:read_num type:int default:-- desc:阅读热人数(coll_type=2)
  172. * @return name:video_title type:string default:-- desc:视频标题(coll_type=3)
  173. * @return name:video_url type:string default:-- desc:视频链接(coll_type=3)
  174. */
  175. public function getUserCollectList()
  176. {
  177. $coll_type = input('get.coll_type',1);
  178. $cate_id = input('get.cate_id',0);
  179. $time_key = input('get.time_key',0);
  180. $sel_where = [];
  181. $sel_where[] = ['c.coll_type','=',$coll_type];
  182. $sel_where[] = ['c.user_id','=',$this->user_id];
  183. if($cate_id)$sel_where[] = ['c.cate_id','=',$cate_id];
  184. // 时间查询
  185. switch ($time_key){
  186. case 1:
  187. $sel_where[] = ['c.create_int','>',time() - 7 * 86400];
  188. break;
  189. case 2:
  190. $sel_where[] = ['c.create_int','>',time() - 30 * 86400];
  191. break;
  192. case 3:
  193. $sel_where[] = ['c.create_int','>',time() - 90 * 86400];
  194. break;
  195. case 4:
  196. $sel_where[] = ['c.create_int','between',[time()- 182 * 86400,time() - 365 * 86400]];
  197. break;
  198. case 5:
  199. $sel_where[] = ['c.create_int','<',time() - 365 * 86400];
  200. break;
  201. }
  202. switch ($coll_type){
  203. case 1 :
  204. $list = Db::name('user_collect')->alias('c')
  205. ->field('c.id,c.coll_id,c.coll_type,c.cate_id,g.name as goods_name, g.cover , g.low_price')
  206. ->join('store_goods g','c.coll_id = g.id','LEFT')
  207. ->where($sel_where)
  208. ->limit($this->off_set,$this->page_num)
  209. ->order('c.create_int desc')
  210. ->select();
  211. foreach ($list as &$v) {
  212. $v['line_money']=StoreGoodsItem::where(['goods_id'=>$v['coll_id']])->max('original_price');
  213. }
  214. break;
  215. case 2 :
  216. $list = Db::name('user_collect')->alias('c')
  217. ->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')
  218. ->join('information_article g','c.coll_id = g.id','LEFT')
  219. ->where($sel_where)
  220. ->limit($this->off_set,$this->page_num)
  221. ->order('c.create_int desc')
  222. ->select();
  223. break;
  224. case 3 :
  225. $list = Db::name('user_collect')->alias('c')
  226. ->field('c.id,c.coll_id,c.coll_type,c.cate_id,g.title as video_title , g.video_url')
  227. ->join('dd_video_intro g','c.coll_id = g.id','LEFT')
  228. ->where($sel_where)
  229. ->limit($this->off_set,$this->page_num)
  230. ->order('c.create_int desc')
  231. ->select();
  232. break;
  233. }
  234. $this->success('ok',['list'=>$list]);
  235. }
  236. /**
  237. * @title 取消收藏
  238. * @desc 取消收藏
  239. * @author qc
  240. * @method POST
  241. * @url /api/Collect/cancelCollect
  242. * @header name:Authorization require:1 desc:Token
  243. * @param name:ids type:string : default:'' desc:需要删除的id记录(用逗号隔开)
  244. */
  245. public function cancelCollect()
  246. {
  247. $ids = input('post.ids','');
  248. if(!$ids) $this->error('请选择要取消收藏的记录');
  249. UserCollect::where('id','in',$ids)->where('user_id','=',$this->user_id)->delete();
  250. $this->success('取消成功');
  251. }
  252. /**
  253. * @title 取消收藏
  254. * @desc 根据类型和(商品id||资讯id||视频id)取消
  255. * @author qc
  256. * @method POST
  257. * @url /api/Collect/cancelCollectByType
  258. * @header name:Authorization require:1 desc:Token
  259. * @param name:coll_type type:int require:1 default:1s desc:类型(1商品2资讯3视频)
  260. * @param name:coll_id type:int default:1 desc:取消收藏的id(商品||资讯||视频)
  261. */
  262. public function cancelCollectByType()
  263. {
  264. $coll_type = input('post.coll_type',1);
  265. $coll_id = input('get.coll_id',0);
  266. UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->delete();
  267. $this->success('取消成功');
  268. }
  269. }