VideoService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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\MemberModel;
  13. use App\Models\VideoCollectModel;
  14. use App\Models\VideoModel;
  15. use App\Services\BaseService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 短视频管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Api
  24. */
  25. class VideoService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * GoodsService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new VideoModel();
  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($params, $pageSize = 18, $field = '', $userId=0)
  57. {
  58. $where = ['a.mark' => 1,'b.mark'=>1];
  59. $field = $field? $field : 'lev_a.*';
  60. $order = 'lev_a.id desc';
  61. $list = $this->model->with(['member'])->from('videos as a')
  62. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  63. ->where($where)
  64. ->where(function ($query) use ($params) {
  65. $type = isset($params['type']) ? $params['type'] : 0;
  66. if ($type > 0) {
  67. $query->where('a.type', $type);
  68. }
  69. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  70. if ($uid > 0) {
  71. $query->where('a.user_id', $uid);
  72. }else{
  73. $query->where('a.status', 2);
  74. }
  75. })
  76. ->where(function ($query) use ($params) {
  77. $keyword = isset($params['kw']) ? $params['kw'] : '';
  78. if ($keyword) {
  79. $query->where('a.title', 'like', "%{$keyword}%")
  80. ->orWhere('a.tags', 'like', "%{$keyword}%")
  81. ->orWhere('a.description', 'like', "%{$keyword}%")
  82. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  83. }
  84. })
  85. ->selectRaw($field)
  86. ->orderByRaw($order)
  87. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  88. $list = $list ? $list->toArray() : [];
  89. if ($list && $list['data']) {
  90. foreach ($list['data'] as &$item) {
  91. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  92. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  93. if(isset($item['albums'])){
  94. $albums = $item['albums']? json_decode($item['albums'], true):[];
  95. $item['albums'] = $albums? get_images_preview($albums) : [];
  96. }
  97. if(isset($item['file_url'])){
  98. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  99. }
  100. if(isset($item['music_url'])){
  101. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  102. }
  103. $member = isset($item['member'])? $item['member'] : [];
  104. if($member){
  105. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  106. }
  107. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  108. $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
  109. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
  110. $item['comment_num'] = isset($item['comment_num']) && $item['comment_num']? format_num($item['comment_num']) : 0;
  111. $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
  112. $item['member'] = $member;
  113. }
  114. }
  115. return [
  116. 'pageSize' => $pageSize,
  117. 'total' => isset($list['total']) ? $list['total'] : 0,
  118. 'list' => isset($list['data']) ? $list['data'] : []
  119. ];
  120. }
  121. /**
  122. * 列表数据
  123. * @param $params
  124. * @param int $pageSize
  125. * @return array
  126. */
  127. public function getIndexList($params, $pageSize = 6, $field='', $userId=0)
  128. {
  129. $where = ['a.mark' => 1,'a.status'=>2,'b.mark'=>1];
  130. $field = $field? $field : 'lev_a.*';
  131. $order = 'rand()';
  132. $model = $this->model->with(['member'])->from('videos as a')
  133. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  134. ->where($where)
  135. ->where(function ($query) use ($params) {
  136. $type = isset($params['type']) ? $params['type'] : 0;
  137. if ($type > 0) {
  138. $query->where('a.type', $type);
  139. }
  140. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  141. if ($uid > 0) {
  142. $query->where('a.user_id', $uid);
  143. }
  144. })
  145. ->where(function ($query) use ($params) {
  146. $keyword = isset($params['kw']) ? $params['kw'] : '';
  147. if ($keyword) {
  148. $query->where('a.title', 'like', "%{$keyword}%")
  149. ->orWhere('a.tags', 'like', "%{$keyword}%")
  150. ->orWhere('a.description', 'like', "%{$keyword}%")
  151. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  152. }
  153. })
  154. ->where(function ($query) use ($params, $userId) {
  155. $query->where(function($query) use($userId){
  156. // 所有人可见
  157. $query->where('a.visible_type', 1);
  158. })->orWhere(function($query) use($userId){
  159. // 关注视频发布用户可见
  160. $uids = $uids = MemberCollectService::make()->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid');
  161. $uids = $uids? $uids : [0];
  162. $query->where('a.visible_type', 2)->where('a.user_id', $uids); // 仅自己可见
  163. })->orWhere(function($query) use($userId){
  164. // 仅自己可见
  165. $query->where('a.visible_type', 3)->where('a.user_id', $userId); // 仅自己可见
  166. });
  167. });
  168. // 推荐的数据
  169. $uids = [];
  170. $countModel = clone $model;
  171. $total = $countModel->where(function($query) use($params, $userId, &$uids){
  172. // 推荐视频数据
  173. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  174. if ($isRecommend > 0) {
  175. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  176. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  177. $tags = isset($recommendData['tags'])? $recommendData['tags'] : []; // 按标签推荐
  178. if($uids){
  179. $query->orWhere(function($query) use($uids){
  180. $query->whereIn('a.user_id', $uids);
  181. });
  182. }
  183. if($tags){
  184. $query->orWhere(function($query) use($tags){
  185. foreach($tags as $tag){
  186. $query->where('a.tags', 'like',"%{$tag}%")
  187. ->orWhere('a.description','like',"%{$tag}%");
  188. }
  189. });
  190. }
  191. }
  192. })->count('a.id');
  193. // 随机优先匹配推送
  194. $match = false;
  195. if($total > 3 && rand(100,999) > 300){
  196. // 关联推荐数据
  197. $match = true;
  198. $list = $countModel->selectRaw($field)
  199. ->orderByRaw($uids?DB::raw("FIND_IN_SET(user_id,'".implode(',', $uids)."')"):'lev_a.create_time desc')
  200. ->orderByRaw($order)
  201. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  202. }else{
  203. // 默认推荐数据
  204. $list = $model->selectRaw($field)
  205. ->orderByRaw('lev_a.create_time desc')
  206. ->orderByRaw($order)
  207. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  208. }
  209. $list = $list ? $list->toArray() : [];
  210. if ($list && $list['data']) {
  211. foreach ($list['data'] as &$item) {
  212. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  213. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  214. if(isset($item['albums'])){
  215. $albums = $item['albums']? json_decode($item['albums'], true):[];
  216. $item['albums'] = $albums? get_images_preview($albums) : [];
  217. }
  218. if(isset($item['file_url'])){
  219. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  220. }
  221. if(isset($item['music_url'])){
  222. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  223. }
  224. $member = isset($item['member'])? $item['member'] : [];
  225. if($member){
  226. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  227. }
  228. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  229. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  230. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? intval($item['collect_num']) : 0;
  231. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  232. $item['comment_num'] = isset($item['comment_num']) && $item['comment_num']? intval($item['comment_num']) : 0;
  233. $item['is_like'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 3);
  234. $item['is_collect'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 2);
  235. $item['is_follow'] = MemberCollectService::make()->checkCollect($userId, $item['user_id'], 1);
  236. $item['is_fans'] = MemberCollectService::make()->checkCollect($item['user_id'], $userId, 1);
  237. $item['is_publisher'] = $userId == $item['user_id']? 1 : 0;
  238. $item['member'] = $member;
  239. }
  240. }
  241. return [
  242. 'pageSize' => $pageSize,
  243. "match"=> $match,
  244. 'total' => isset($list['total']) ? $list['total'] : 0,
  245. 'list' => isset($list['data']) ? $list['data'] : []
  246. ];
  247. }
  248. /**
  249. * 详情
  250. * @param $id
  251. * @return array
  252. */
  253. public function getInfo($id, $userId=0, $field=[])
  254. {
  255. $field = $field? $field : ['a.*'];
  256. $info = $this->model->from('videos as a')->with(['member'])
  257. ->leftJoin('member as b','b.id','=','a.user_id')
  258. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  259. ->select($field)
  260. ->first();
  261. $info = $info? $info->toArray() : [];
  262. if($info){
  263. if(isset($info['thumb'])){
  264. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  265. }
  266. if(isset($info['file_url'])){
  267. $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
  268. }
  269. if(isset($item['music_url'])){
  270. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  271. }
  272. if(isset($info['albums'])){
  273. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  274. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  275. }
  276. $member = isset($info['member'])? $info['member'] : [];
  277. if($member){
  278. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  279. }
  280. $info['member'] = $member;
  281. $info['tags'] = isset($info['tags']) && $info['tags']? explode(',', $info['tags']) : [];
  282. $info['like_num'] = isset($info['like_num']) && $info['like_num']? intval($info['like_num']) : 0;
  283. $info['collect_num'] = isset($info['collect_num']) && $info['collect_num']? intval($info['collect_num']) : 0;
  284. $info['views'] = isset($info['views']) && $info['views']? intval($info['views']) : 0;
  285. $info['comment_num'] = isset($info['comment_num']) && $info['comment_num']? intval($info['comment_num']) : 0;
  286. $info['is_like'] = VideoCollectService::make()->checkCollect($userId, $info['id'], 3);
  287. $info['is_collect'] = VideoCollectService::make()->checkCollect($userId, $info['id'], 2);
  288. $info['is_follow'] = MemberCollectService::make()->checkCollect($userId, $info['user_id'], 1);
  289. $info['is_fans'] = MemberCollectService::make()->checkCollect($info['user_id'], $userId, 1);
  290. $info['is_publisher'] = $userId == $info['user_id']? 1 : 0;
  291. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  292. // 浏览历史
  293. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  294. $data = [
  295. 'user_id'=> $userId,
  296. 'type'=> 1,
  297. 'collect_id'=> $id,
  298. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  299. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  300. 'create_time'=> time(),
  301. 'status'=> 1,
  302. ];
  303. VideoCollectModel::insert($data);
  304. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
  305. RedisService::clear("caches:videos:recommend:{$userId}_1");
  306. RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
  307. }
  308. // 浏览量
  309. $this->updateView($userId, $id);
  310. }
  311. return $info;
  312. }
  313. /**
  314. * 更新播放浏览历史
  315. * @param $userId 用户ID
  316. * @param $id 视频ID
  317. * @return false
  318. */
  319. public function updatePlay($userId, $id)
  320. {
  321. // 浏览历史
  322. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  323. $info = $this->model->from('videos as a')
  324. ->where(['a.id'=> $id,'a.mark'=>1])
  325. ->select(['a.id','a.tags','a.user_id'])
  326. ->first();
  327. if(empty($info)){
  328. return false;
  329. }
  330. $data = [
  331. 'user_id'=> $userId,
  332. 'type'=> 1,
  333. 'collect_id'=> $id,
  334. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  335. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  336. 'create_time'=> time(),
  337. 'status'=> 1,
  338. ];
  339. VideoCollectModel::insert($data);
  340. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
  341. RedisService::clear("caches:videos:recommend:{$userId}_1");
  342. RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
  343. }
  344. // 浏览量
  345. $this->updateView($userId, $id);
  346. $this->error = 1010;
  347. return true;
  348. }
  349. /**
  350. * 更新浏览量
  351. * @param $userId
  352. * @param $dynamicId
  353. * @return array|mixed
  354. */
  355. public function updateView($userId, $id)
  356. {
  357. $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
  358. $data = RedisService::get($cacheKey);
  359. if($data){
  360. return false;
  361. }
  362. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  363. RedisService::set($cacheKey, $id, rand(1,3)*3600);
  364. return $data;
  365. }
  366. /**
  367. * 发布
  368. * @param $goodsId
  369. * @return bool
  370. */
  371. public function publish($userId, $params, $request)
  372. {
  373. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  374. $type = isset($params['type'])? intval($params['type']) : 2;
  375. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  376. ->select(['id','nickname','status'])
  377. ->first();
  378. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  379. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  380. if(empty($userInfo) || $status != 1){
  381. $this->error = 2017;
  382. return false;
  383. }
  384. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  385. if($thumb){
  386. $result = upload_base64($thumb);
  387. $thumb = isset($result['file_path'])? $result['file_path'] : '';
  388. if(empty($thumb)){
  389. $this->error = 2208;
  390. return false;
  391. }
  392. }
  393. // 音乐
  394. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  395. if($musicUrl){
  396. $result = upload_remote_image($musicUrl,'mp3','music');
  397. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  398. if(empty($musicUrl)){
  399. $this->error = 2209;
  400. return false;
  401. }
  402. }
  403. // 视频
  404. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  405. $albums = isset($params['album_urls'])?json_decode( $params['album_urls'],true) : [];
  406. if($type == 1) {
  407. if($fileUrl){
  408. $result = upload_video($request,'video');
  409. $data = isset($result['data'])? $result['data'] : [];
  410. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  411. if(empty($fileUrl)){
  412. $this->error = 2212;
  413. return false;
  414. }
  415. }else{
  416. $this->error = 2213;
  417. return false;
  418. }
  419. }
  420. // 相册
  421. else if($type == 2){
  422. if(empty($albums) || !is_array($albums)){
  423. $this->error = 2211;
  424. return false;
  425. }
  426. $albums = get_format_images($albums);
  427. }
  428. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  429. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  430. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  431. $publishCheck = $publishCheck>0? $publishCheck : 0;
  432. $tags = isset($params['tags']) && $params['tags']? $params['tags'] : '';
  433. $data = [
  434. 'user_id' => $userId,
  435. 'title' => $title,
  436. 'type' => $type,
  437. 'description' => $description,
  438. 'tags' => $tags && is_array($tags)? implode(',', $tags) : $tags,
  439. 'file_url' => $fileUrl,
  440. 'thumb' => $thumb,
  441. 'albums' => $albums? $albums : '',
  442. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  443. 'music_name' => isset($params['music_name'])? trim($params['music_name']) : '',
  444. 'music_url' => $musicUrl,
  445. 'goods_id' => $goodsId,
  446. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  447. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  448. 'status' => $publishCheck? 1 : 2,
  449. 'mark' => 1,
  450. 'publish_at' => date('Y-m-d H:i:s'),
  451. 'create_time' => time(),
  452. ];
  453. RedisService::set('caches:videos:publish', ['params'=> $params,'data'=> $data], 600);
  454. if($id = $this->model->insertGetId($data)){
  455. $this->error = 1023;
  456. return ['id'=> $id];
  457. }
  458. $this->error = 1024;
  459. return false;
  460. }
  461. /**
  462. * 状态设置
  463. * @return bool
  464. */
  465. public function status()
  466. {
  467. $id = request()->post('id', 0);
  468. $status = request()->post('status', 1);
  469. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  470. $this->error = 2981;
  471. return false;
  472. }
  473. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  474. $this->error = 1002;
  475. return true;
  476. }
  477. $this->error = 1003;
  478. return true;
  479. }
  480. /**
  481. * 删除
  482. * @return bool
  483. */
  484. public function delete()
  485. {
  486. // 参数
  487. $param = request()->all();
  488. $id = getter($param, "id");
  489. if (empty($id)) {
  490. $this->error = 2014;
  491. return false;
  492. }
  493. if(!$this->model->where(['id'=> $id])->value('id')){
  494. $this->error = 1039;
  495. return false;
  496. }
  497. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  498. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  499. $this->error = 1025;
  500. return true;
  501. }else{
  502. $this->error = 1026;
  503. return false;
  504. }
  505. }
  506. }