Collect.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UserCollect;
  4. use app\common\model\VideoUrl;
  5. use think\Db;
  6. use library\tools\Data;
  7. /**
  8. * @title 收藏
  9. * @controller Collect
  10. * @group base
  11. */
  12. class Collect extends Base
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. parent::checkLogin();
  18. }
  19. /**
  20. * @title 用户视频收藏(单个收藏)
  21. * @desc 用户视频收藏(单个收藏)
  22. * @author qc
  23. * @method POST
  24. * @url /api/Collect/userCollect
  25. * @header name:Authorization require:1 desc:Token
  26. * @param name:coll_type type:int require:1 default:-- desc:类型(1平台视频2用户视频)
  27. * @param name:coll_id type:int require:1 default:-- desc:收藏id(平台视频点赞用video_arr的iD)
  28. * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
  29. */
  30. public function userCollect()
  31. {
  32. $coll_type = input('post.coll_type',1);
  33. $url_id = input('post.coll_id',0);
  34. $video_id = VideoUrl::where('id',$url_id)->value('video_id');
  35. $collect_check = UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$video_id,'children_id'=>$url_id])->find();
  36. if($collect_check) {
  37. UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$video_id,'children_id'=>$url_id])->delete();
  38. $this->success('取消收藏成功',['status'=>0]);
  39. }else{
  40. Data::save('UserCollect', [
  41. 'user_id'=>$this->user_id,
  42. 'coll_type'=>$coll_type,
  43. 'coll_id'=>$video_id,
  44. 'children_id'=>$url_id,
  45. 'create_int'=>time()],'user_id',['user_id'=>$this->user_id,'coll_type'=>$coll_type, 'coll_id'=>$video_id,'children_id'=>$url_id]);
  46. $this->success('收藏成功',['status'=>1]);
  47. }
  48. }
  49. /**
  50. * @title 获取用户收藏列表
  51. * @desc 获取用户收藏列表
  52. * @author qc
  53. * @method GET
  54. * @url /api/Collect/getUserCollectList
  55. * @header name:Authorization require:1 desc:Token
  56. * @param name:page type:int default:1 desc:页数
  57. * @param name:page_num type:int : default:20 desc:每页数
  58. * @param name:coll_type type:int require:1 default:-- desc:类型(1平台视频,2资料,3图文)
  59. * @return name:id type:int default:-- desc:记录id
  60. * @return name:create_at type:string default:-- desc:收藏时间
  61. * @return name:coll_type type:int default:-- desc:收藏类型(1平台视频)
  62. * @return name:video_item type:array default:-- desc:收藏详情(coll_type=1平台视频)
  63. * @return name:video_item.video_id type:int default:-- desc:视频记录id
  64. * @return name:video_item.id type:int default:-- desc:视频详情【url_arr】id
  65. * @return name:video_item.cover type:string default:-- desc:视频详情封面
  66. * @return name:video_item.url type:string default:-- desc:视频详情链接
  67. * @return name:video_item.title type:string default:-- desc:视频详标题
  68. * @return name:datum_item type:array default:-- desc:收藏详情(coll_type=2资料)
  69. * @return name:datum_item.datum_id type:int default:-- desc:资料记录id
  70. * @return name:datum_item.id type:int default:-- desc:资料详情id
  71. * @return name:datum_item.url type:string default:-- desc:资料地址
  72. * @return name:datum_item.title type:string default:-- desc:资料标题
  73. * @return name:article_item type:array default:-- desc:收藏详情(coll_type=3图文)
  74. * @return name:article_item.article_id type:int default:-- desc:图文记录id
  75. * @return name:article_item.id type:int default:-- desc:图文详情id
  76. * @return name:article_item.cover type:string default:-- desc:封面
  77. * @return name:article_item.images type:string default:-- desc:图片(多张用|隔开)
  78. * @return name:article_item.content type:string default:-- desc:图文内容
  79. */
  80. public function getUserCollectList()
  81. {
  82. $coll_type = input('get.coll_type',1);
  83. $sel_where = [];
  84. $sel_where[] = ['coll_type','=',$coll_type];
  85. $sel_where[] = ['user_id','=',$this->user_id];
  86. if(!in_array($coll_type,[1,2,3])) $this->error('收藏类型有误');
  87. $with = ['','videoItem','datumItem','articleItem'];
  88. $list = UserCollect::with($with[$coll_type])
  89. ->field('id,create_at,coll_type')
  90. ->where($sel_where)
  91. ->limit($this->off_set,$this->page_num)
  92. ->select()->toArray();
  93. $this->success('ok',['list'=>$list]);
  94. }
  95. /**
  96. * @title 取消收藏
  97. * @desc 取消收藏
  98. * @author qc
  99. * @method POST
  100. * @url /api/Collect/cancelCollect
  101. * @header name:Authorization require:1 desc:Token
  102. * @param name:ids type:string : default:'' desc:需要删除的id记录(用逗号隔开)
  103. */
  104. public function cancelCollect()
  105. {
  106. $ids = input('post.ids','');
  107. if(!$ids) $this->error('请选择要取消收藏的记录');
  108. UserCollect::where('id','in',$ids)->where('user_id','=',$this->user_id)->delete();
  109. $this->success('取消成功');
  110. }
  111. /**
  112. * @title 取消收藏
  113. * @desc 根据类型(平台视频id||用户视频id)取消
  114. * @author qc
  115. * @method POST
  116. * @url /api/Collect/cancelCollectByType
  117. * @header name:Authorization require:1 desc:Token
  118. * @param name:coll_type type:int require:1 default:1s desc:类型(1平台视频id,2用户视频id)
  119. * @param name:coll_id type:int default:1 desc:取消收藏的id(平台视频id||用户视频id)
  120. */
  121. public function cancelCollectByType()
  122. {
  123. $coll_type = input('post.coll_type',1);
  124. $coll_id = input('get.coll_id',0);
  125. UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>$coll_type,'coll_id'=>$coll_id])->delete();
  126. $this->success('取消成功');
  127. }
  128. }