VideoCollectService.php 12 KB

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