VideoCollectService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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, $sourceType=1)
  158. {
  159. $cacheKey = "caches:videos:collect:temp_{$userId}_{$collectId}_{$type}_{$sourceType}";
  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,'source_type'=>$sourceType,'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. /**
  172. * 推荐数据
  173. * @param $userId 用户
  174. * @return array|mixed
  175. */
  176. public function getRecommendData($userId, $type=1)
  177. {
  178. $cacheKey = "caches:videos:recommend:{$userId}_{$type}";
  179. $data = RedisService::get($cacheKey);
  180. if($data || RedisService::exists($cacheKey)){
  181. return $data? $data : [];
  182. }
  183. $arr = ['uids'=>[$userId],'tags'=>[],'category'=>[]];
  184. $datas = $this->model->where(['user_id'=> $userId,'source_type'=> $type,'status'=>1,'mark'=>1])
  185. ->where('collect_uid','>', 0)
  186. ->select(['collect_uid','tags','category_id'])
  187. ->orderByRaw('rand()')
  188. ->limit(rand(3,5))
  189. ->get();
  190. $datas = $datas? $datas->toArray() : [];
  191. if($datas){
  192. foreach ($datas as $item)
  193. {
  194. $uid = isset($item['collect_uid'])? $item['collect_uid'] : 0;
  195. $categoryId = isset($item['category_id'])? $item['category_id'] : 0;
  196. $tags = isset($item['tags'])? explode(',', $item['tags']) : [];
  197. if($uid){
  198. $arr['uids'][] = $uid;
  199. }
  200. if($categoryId){
  201. $arr['category'][] = $categoryId;
  202. }
  203. if($tags){
  204. $tags = array_filter($tags);
  205. $arr['tags'] = array_merge($arr['tags'], $tags);
  206. $arr['tags'] = array_unique($arr['tags']);
  207. }
  208. }
  209. RedisService::set($cacheKey, $arr, rand(3600, 7200));
  210. }
  211. return $arr;
  212. }
  213. /**
  214. * 收藏点赞
  215. * @param $userId
  216. * @param $dynamicId
  217. * @param $type
  218. * @param int $status
  219. * @return mixed
  220. */
  221. public function collect($userId, $params)
  222. {
  223. try {
  224. $collectId = isset($params['id']) ? intval($params['id']) : 0;
  225. $type = isset($params['type']) ? intval($params['type']) : 2;
  226. $status = isset($params['status']) ? intval($params['status']) : 1;
  227. if ($collectId <= 0 || !in_array($type, [2, 3]) || !in_array($status, [1, 2])) {
  228. $this->error = 2014;
  229. return false;
  230. }
  231. $collectInfo = $this->getCollectCacheInfo($userId, $collectId, $type);
  232. $id = isset($collectInfo['id']) ? $collectInfo['id'] : 0;
  233. // 信息
  234. $info = VideoModel::where(['id' => $collectId, 'mark' => 1])->select(['id', 'user_id', 'tags'])->first();
  235. $collectUid = isset($info['user_id']) ? $info['user_id'] : 0;
  236. if (empty($info)) {
  237. $this->error = 1039;
  238. return false;
  239. }
  240. $data = [
  241. 'user_id' => $userId,
  242. 'type' => $type,
  243. 'source_type' => 1,
  244. 'collect_id' => $collectId,
  245. 'collect_uid' => $collectUid,
  246. 'tags' => isset($info['tags']) ? $info['tags'] : '',
  247. 'update_time' => time(),
  248. 'status' => $status,
  249. 'mark' => 1,
  250. ];
  251. DB::beginTransaction();
  252. if (!$id) {
  253. $data['create_time'] = time();
  254. if (!$this->model->insertGetId($data)) {
  255. DB::rollBack();
  256. return false;
  257. }
  258. } else {
  259. if (!$this->model->where('id', $id)->update($data)) {
  260. DB::rollBack();
  261. return false;
  262. }
  263. }
  264. $updateData = ['update_time' => time()];
  265. if ($type == 2) {
  266. $updateData['collect_num'] = DB::raw('collect_num ' . ($status == 1 ? '+ 1' : '-1'));
  267. } else if ($type == 3) {
  268. $updateData['like_num'] = DB::raw('like_num ' . ($status == 1 ? '+ 1' : '-1'));
  269. }
  270. if (!VideoModel::where(['id' => $collectId, 'mark' => 1])->update($updateData)) {
  271. DB::rollBack();
  272. return false;
  273. }
  274. $this->error = 1002;
  275. DB::commit();
  276. RedisService::clear("caches:videos:collect:u{$userId}_c{$collectId}_{$type}");
  277. RedisService::clear("caches:videos:collect:temp_{$userId}_{$collectId}_{$type}_1");
  278. RedisService::clear("caches:videos:recommend:{$userId}_1");
  279. return true;
  280. }catch (\Exception $exception){
  281. $this->error = $exception-> getMessage();
  282. return false;
  283. }
  284. }
  285. /**
  286. * 获取(被)收藏/关注/点赞数量
  287. * @param $userId 用户ID
  288. * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
  289. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  290. * @return array|mixed
  291. */
  292. public function getCount($userId, $type=1, $ctype=1)
  293. {
  294. $cacheKey = "caches:videos:collect:{$userId}_{$type}_{$ctype}";
  295. $data = RedisService::get($cacheKey);
  296. if($data){
  297. return $data;
  298. }
  299. $field = 'collect_uid';
  300. if($ctype == 2){
  301. $field = 'user_id';
  302. }
  303. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  304. if($data){
  305. RedisService::set($cacheKey, $data, 3600);
  306. }
  307. return $data;
  308. }
  309. /**
  310. * 取消
  311. * @param $userId
  312. * @return bool
  313. */
  314. public function cancel($userId)
  315. {
  316. // 参数
  317. $ids = request()->post('ids','');
  318. $ids = $ids? explode(',', $ids) : [];
  319. $type = request()->post('type',0);
  320. if (empty($ids) && $type == 0) {
  321. $this->error = 1033;
  322. return false;
  323. }
  324. if($type){
  325. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  326. }else {
  327. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  328. }
  329. $this->error = 1002;
  330. return true;
  331. }
  332. }