VideoCollectService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberCollectModel;
  13. use App\Models\VideoCollectModel;
  14. use App\Models\VideoModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. use BN\Red;
  18. /**
  19. * 短视频收藏点赞管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Api
  23. */
  24. class VideoCollectService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. * MemberCollectService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new VideoCollectModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = (new static());
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 列表数据
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getDataList($userId, $params, $pageSize = 15, $field='')
  56. {
  57. $where = ['a.mark' => 1,'a.status'=>1,'b.status'=>1,'b.mark'=>1];
  58. $field = $field? $field : 'lev_a.*,lev_b.id as cid,lev_b.user_id as collect_user_id,lev_a.type as collect_type';
  59. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  60. $order = 'id desc';
  61. if($sortType == 1){
  62. $order = 'lev_b.create_time desc, lev_b.id desc';
  63. }
  64. $list = $this->model->with(['member'])
  65. ->from('video_collect as b')
  66. ->leftJoin('video as a', 'a.id', '=', 'b.collect_id')
  67. ->where($where)
  68. ->where(function ($query) use ($params) {
  69. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  70. if ($userId > 0) {
  71. $query->where('a.user_id', $userId);
  72. }
  73. $collectUserId = isset($params['collect_uid']) ? $params['collect_uid'] : 0;
  74. if ($collectUserId > 0) {
  75. $query->where('a.collect_uid', $collectUserId);
  76. }
  77. $isRead = isset($params['is_read'])? $params['is_read'] : 0;
  78. $isRead = $isRead>0? $isRead : 0;
  79. if ($isRead > 0) {
  80. $query->where('a.is_read', $isRead);
  81. }
  82. $type = isset($params['type'])? $params['type'] : 0;
  83. $type = $type>0? $type : 1;
  84. if ($type > 0) {
  85. $query->where('a.type', $type);
  86. }
  87. })
  88. ->where(function ($query) use ($params) {
  89. $keyword = isset($params['kw']) ? $params['kw'] : '';
  90. if ($keyword) {
  91. $query->where('a.title', 'like', "%{$keyword}%")
  92. ->orWhere('a.tags','like',"%{$keyword}%")
  93. ->orWhere('a.description','like',"%{$keyword}%");
  94. }
  95. })
  96. ->selectRaw($field)
  97. ->orderByRaw($order)
  98. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  99. $list = $list ? $list->toArray() : [];
  100. if ($list) {
  101. foreach ($list['data'] as &$item) {
  102. $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : '';
  103. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  104. $item['file_url'] = isset($item['file_url']) && $item['file_url'] ? get_image_url($item['file_url']) : '';
  105. if(isset($item['albums'])){
  106. $albums = $item['albums']? json_decode($item['albums'], true):[];
  107. $item['albums'] = $albums? get_images_preview($albums) : [];
  108. }
  109. if(isset($item['music_url'])){
  110. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  111. }
  112. $member = isset($item['member'])? $item['member'] : [];
  113. if($member){
  114. $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
  115. }
  116. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  117. $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
  118. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
  119. $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
  120. $item['member'] = $member;
  121. }
  122. }
  123. return [
  124. 'pageSize' => $pageSize,
  125. 'total' => isset($list['total']) ? $list['total'] : 0,
  126. 'list' => isset($list['data']) ? $list['data'] : []
  127. ];
  128. }
  129. /**
  130. * 是否已经收藏或点赞过
  131. * @param $userId
  132. * @param $collectUid
  133. * @param int $type 类型:
  134. * @return array|mixed
  135. */
  136. public function checkCollect($userId, $collectId, $type=1)
  137. {
  138. $cacheKey = "caches:videos:collect:u{$userId}_c{$collectId}_{$type}";
  139. $data = RedisService::get($cacheKey);
  140. if($data){
  141. return $data? 1: 2;
  142. }
  143. $data = $this->model->where(['user_id'=> $userId,'collect_id'=> $collectId,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
  144. if($data){
  145. RedisService::set($cacheKey, $data, rand(5, 10));
  146. }
  147. return $data? 1 : 2;
  148. }
  149. /**
  150. * 缓存信息
  151. * @param $userId
  152. * @param $collectId
  153. * @param $type
  154. * @return array|mixed
  155. */
  156. public function getCollectCacheInfo($userId, $collectId, $type)
  157. {
  158. $cacheKey = "caches:videos:collect:temp_{$userId}_{$collectId}_{$type}";
  159. $info = RedisService::get($cacheKey);
  160. if($info || RedisService::exists($cacheKey)){
  161. return $info;
  162. }
  163. $info = $this->model->where(['user_id'=> $userId,'collect_id'=> $collectId,'type'=> $type])->select(['id','user_id','status'])->first();
  164. $info = $info? $info->toArray() : [];
  165. if($info){
  166. RedisService::set($cacheKey, $info, rand(10,30));
  167. }
  168. return $info;
  169. }
  170. public function getRecommendData($userId)
  171. {
  172. $cacheKey = "caches:videos:recommend:{$userId}";
  173. $data = RedisService::get($cacheKey);
  174. if($data || RedisService::exists($cacheKey)){
  175. return $data? $data : [];
  176. }
  177. $arr = ['uids'=>[$userId],'tags'=>[]];
  178. $datas = $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  179. ->where('collect_uid','>', 0)
  180. ->select(['collect_uid','tags'])
  181. ->orderRaw('rand()')
  182. ->limit(rand(3,5))
  183. ->get();
  184. $datas = $datas? $datas->toArray() : [];
  185. if($datas){
  186. foreach ($datas as $item)
  187. {
  188. $uid = isset($item['collect_uid'])? $item['collect_uid'] : 0;
  189. $tags = isset($item['tags'])? explode(',', $item['tags']) : [];
  190. if($uid){
  191. $arr['uids'][] = $uid;
  192. }
  193. if($tags){
  194. $tags = array_filter($tags);
  195. $arr['tags'] = array_merge($arr['tags'], $tags);
  196. $arr['tags'] = array_unique($arr['tags']);
  197. }
  198. }
  199. RedisService::set($cacheKey, $arr, rand(3600, 7200));
  200. }
  201. return $arr;
  202. }
  203. /**
  204. * 收藏点赞
  205. * @param $userId
  206. * @param $dynamicId
  207. * @param $type
  208. * @param int $status
  209. * @return mixed
  210. */
  211. public function collect($userId, $params)
  212. {
  213. $collectId = isset($params['id'])? intval($params['id']) : 0;
  214. $type = isset($params['type'])? intval($params['type']) : 2;
  215. $status = isset($params['status'])? intval($params['status']) : 1;
  216. if($collectId<=0 || !in_array($type, [1,2,3]) || !in_array($status, [1,2,3])){
  217. $this->error = 2014;
  218. return false;
  219. }
  220. $collectInfo = $this->getCollectCacheInfo($userId, $collectId, $type);
  221. $id = isset($collectInfo['id'])? $collectInfo['id'] : 0;
  222. $status = isset($collectInfo['status'])? $collectInfo['status'] : 0;
  223. if($type==1 && $collectInfo && $status == 1){
  224. $this->error = 1017;
  225. return false;
  226. }
  227. // 信息
  228. $info = VideoModel::where(['id'=> $collectId,'mark'=>1])->select(['id','user_id','tags'])->first();
  229. $collectUid = isset($info['user_id'])? $info['user_id'] : 0;
  230. if(empty($info)){
  231. $this->error = 1039;
  232. return false;
  233. }
  234. $data = [
  235. 'user_id'=> $userId,
  236. 'type'=> $type,
  237. 'collect_id'=> $collectId,
  238. 'collect_uid'=> $collectUid,
  239. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  240. 'update_time'=> time(),
  241. 'status'=> $status,
  242. 'mark'=> 1,
  243. ];
  244. if(!$id){
  245. $data['create_time'] = time();
  246. $this->error = 1002;
  247. RedisService::clear("caches:videos:collect:u{$userId}_c{$collectId}_{$type}");
  248. RedisService::clear("caches:videos:collect:temp_{$userId}_{$collectId}_{$type}");
  249. RedisService::clear("caches:videos:recommend:{$userId}");
  250. return $this->model->insertGetId($data);
  251. }else{
  252. $this->error = 1002;
  253. RedisService::clear("caches:videos:collect:u{$userId}_c{$collectId}_{$type}");
  254. RedisService::clear("caches:videos:collect:temp_{$userId}_{$collectId}_{$type}");
  255. RedisService::clear("caches:videos:recommend:{$userId}");
  256. return $this->model->where('id', $id)->update($data);
  257. }
  258. }
  259. /**
  260. * 获取(被)收藏/关注/点赞数量
  261. * @param $userId 用户ID
  262. * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
  263. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  264. * @return array|mixed
  265. */
  266. public function getCount($userId, $type=1, $ctype=1)
  267. {
  268. $cacheKey = "caches:videos:collect:{$userId}_{$type}_{$ctype}";
  269. $data = RedisService::get($cacheKey);
  270. if($data){
  271. return $data;
  272. }
  273. $field = 'collect_uid';
  274. if($ctype == 2){
  275. $field = 'user_id';
  276. }
  277. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  278. if($data){
  279. RedisService::set($cacheKey, $data, 3600);
  280. }
  281. return $data;
  282. }
  283. /**
  284. * 取消
  285. * @param $userId
  286. * @return bool
  287. */
  288. public function cancel($userId)
  289. {
  290. // 参数
  291. $ids = request()->post('ids','');
  292. $ids = $ids? explode(',', $ids) : [];
  293. $type = request()->post('type',0);
  294. if (empty($ids) && $type == 0) {
  295. $this->error = 1033;
  296. return false;
  297. }
  298. if($type){
  299. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  300. }else {
  301. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  302. }
  303. $this->error = 1002;
  304. return true;
  305. }
  306. }