VideoService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
  62. $list = $this->model->with(['member'])->from('videos as a')
  63. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  64. ->where($where)
  65. ->where(function ($query) use ($params) {
  66. $type = isset($params['type']) ? $params['type'] : 0;
  67. if ($type > 0) {
  68. $query->where('a.type', $type);
  69. }
  70. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  71. if ($uid > 0) {
  72. $query->where('a.user_id', $uid);
  73. }else{
  74. $query->where('a.status', 2);
  75. }
  76. })
  77. ->where(function ($query) use ($params) {
  78. $keyword = isset($params['kw']) ? $params['kw'] : '';
  79. if ($keyword) {
  80. $query->where('a.title', 'like', "%{$keyword}%")
  81. ->orWhere('a.tags', 'like', "%{$keyword}%")
  82. ->orWhere('a.description', 'like', "%{$keyword}%")
  83. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  84. }
  85. })
  86. ->selectRaw($field)
  87. ->orderByRaw($order)
  88. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  89. $list = $list ? $list->toArray() : [];
  90. if ($list && $list['data']) {
  91. foreach ($list['data'] as &$item) {
  92. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  93. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  94. if(isset($item['albums'])){
  95. $albums = $item['albums']? json_decode($item['albums'], true):[];
  96. $item['albums'] = $albums? get_images_preview($albums) : [];
  97. }
  98. if(isset($item['file_url'])){
  99. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  100. }
  101. if(isset($item['music_url'])){
  102. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  103. }
  104. $member = isset($item['member'])? $item['member'] : [];
  105. if($member){
  106. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  107. }
  108. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  109. $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
  110. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_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. }else{
  144. $query->where('a.status', 2);
  145. }
  146. })
  147. ->where(function ($query) use ($params) {
  148. $keyword = isset($params['kw']) ? $params['kw'] : '';
  149. if ($keyword) {
  150. $query->where('a.title', 'like', "%{$keyword}%")
  151. ->orWhere('a.tags', 'like', "%{$keyword}%")
  152. ->orWhere('a.description', 'like', "%{$keyword}%")
  153. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  154. }
  155. });
  156. // 推荐的数据
  157. $countModel = clone $model;
  158. $total = $countModel->where(function($query) use($params, $userId){
  159. // 推荐视频数据
  160. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  161. if ($isRecommend > 0) {
  162. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  163. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  164. $tags = isset($recommendData['tags'])? $recommendData['tags'] : []; // 按标签推荐
  165. if($uids){
  166. $query->orWhere(function($query) use($uids){
  167. $query->whereIn('a.user_id', $uids);
  168. });
  169. }
  170. if($tags){
  171. $query->orWhere(function($query) use($tags){
  172. foreach($tags as $tag){
  173. $query->where('a.tags', 'like',"%{$tag}%")
  174. ->orWhere('a.description','like',"%{$tag}%");
  175. }
  176. });
  177. }
  178. }
  179. })->count('a.id');
  180. if($total > 0){
  181. // 关联推荐数据
  182. $list = $countModel->selectRaw($field)
  183. ->orderByRaw($order)
  184. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  185. }else{
  186. // 默认推荐数据
  187. $list = $model->selectRaw($field)
  188. ->orderByRaw($order)
  189. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  190. }
  191. $list = $list ? $list->toArray() : [];
  192. if ($list && $list['data']) {
  193. foreach ($list['data'] as &$item) {
  194. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  195. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  196. if(isset($item['albums'])){
  197. $albums = $item['albums']? json_decode($item['albums'], true):[];
  198. $item['albums'] = $albums? get_images_preview($albums) : [];
  199. }
  200. if(isset($item['file_url'])){
  201. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  202. }
  203. if(isset($item['music_url'])){
  204. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  205. }
  206. $member = isset($item['member'])? $item['member'] : [];
  207. if($member){
  208. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  209. }
  210. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  211. $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
  212. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
  213. $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
  214. $item['member'] = $member;
  215. }
  216. }
  217. return [
  218. 'pageSize' => $pageSize,
  219. 'total' => isset($list['total']) ? $list['total'] : 0,
  220. 'list' => isset($list['data']) ? $list['data'] : []
  221. ];
  222. }
  223. /**
  224. * 详情
  225. * @param $id
  226. * @return array
  227. */
  228. public function getInfo($id, $userId=0, $field=[])
  229. {
  230. $field = $field? $field : ['a.*'];
  231. $info = $this->model->from('videos as a')->with(['member'])
  232. ->leftJoin('member as b','b.id','=','a.user_id')
  233. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  234. ->select($field)
  235. ->first();
  236. $info = $info? $info->toArray() : [];
  237. if($info){
  238. if(isset($info['thumb'])){
  239. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  240. }
  241. if(isset($info['file_url'])){
  242. $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
  243. }
  244. if(isset($item['music_url'])){
  245. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  246. }
  247. if(isset($info['albums'])){
  248. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  249. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  250. }
  251. $member = isset($info['member'])? $info['member'] : [];
  252. if($member){
  253. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  254. }
  255. $info['member'] = $member;
  256. // 浏览历史
  257. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  258. $data = [
  259. 'user_id'=> $userId,
  260. 'type'=> 1,
  261. 'collect_id'=> $id,
  262. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  263. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  264. 'create_time'=> time(),
  265. 'status'=> 1,
  266. ];
  267. VideoCollectModel::insert($data);
  268. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1", $data, rand(10,30));
  269. RedisService::clear("caches:videos:recommend:{$userId}");
  270. }
  271. // 浏览量
  272. $this->updateView($userId, $id);
  273. }
  274. return $info;
  275. }
  276. /**
  277. * 更新播放浏览历史
  278. * @param $userId 用户ID
  279. * @param $id 视频ID
  280. * @return false
  281. */
  282. public function updatePlay($userId, $id)
  283. {
  284. // 浏览历史
  285. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  286. $info = $this->model->from('videos as a')
  287. ->where(['a.id'=> $id,'a.mark'=>1])
  288. ->select(['a.id','a.tags','a.user_id'])
  289. ->first();
  290. if(empty($info)){
  291. return false;
  292. }
  293. $data = [
  294. 'user_id'=> $userId,
  295. 'type'=> 1,
  296. 'collect_id'=> $id,
  297. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  298. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  299. 'create_time'=> time(),
  300. 'status'=> 1,
  301. ];
  302. VideoCollectModel::insert($data);
  303. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1", $data, rand(10,30));
  304. RedisService::clear("caches:videos:recommend:{$userId}");
  305. }
  306. // 浏览量
  307. $this->updateView($userId, $id);
  308. $this->error = 1010;
  309. return true;
  310. }
  311. /**
  312. * 更新浏览量
  313. * @param $userId
  314. * @param $dynamicId
  315. * @return array|mixed
  316. */
  317. public function updateView($userId, $id)
  318. {
  319. $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
  320. $data = RedisService::get($cacheKey);
  321. if($data){
  322. return false;
  323. }
  324. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  325. RedisService::set($cacheKey, $id, rand(1,3)*3600);
  326. return $data;
  327. }
  328. /**
  329. * 发布
  330. * @param $goodsId
  331. * @return bool
  332. */
  333. public function publish($userId, $params, $request)
  334. {
  335. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  336. $type = isset($params['type'])? intval($params['type']) : 2;
  337. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  338. ->select(['id','nickname','status'])
  339. ->first();
  340. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  341. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  342. if(empty($userInfo) || $status != 1){
  343. $this->error = 2017;
  344. return false;
  345. }
  346. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  347. if($thumb){
  348. $result = upload_base64($thumb);
  349. $thumb = isset($result['file_path'])? $result['file_path'] : '';
  350. if(empty($thumb)){
  351. $this->error = 2208;
  352. return false;
  353. }
  354. }
  355. // 音乐
  356. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  357. if($musicUrl){
  358. $result = upload_remote_image($musicUrl,'mp3','music');
  359. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  360. if(empty($musicUrl)){
  361. $this->error = 2209;
  362. return false;
  363. }
  364. }
  365. // 视频
  366. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  367. $albums = isset($params['album_urls'])?json_decode( $params['album_urls'],true) : [];
  368. if($type == 1) {
  369. if($fileUrl){
  370. $result = upload_video($request,'video');
  371. $data = isset($result['data'])? $result['data'] : [];
  372. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  373. if(empty($fileUrl)){
  374. $this->error = 2212;
  375. return false;
  376. }
  377. }else{
  378. $this->error = 2213;
  379. return false;
  380. }
  381. }
  382. // 相册
  383. else if($type == 2){
  384. if(empty($albums) || !is_array($albums)){
  385. $this->error = 2211;
  386. return false;
  387. }
  388. $albums = get_format_images($albums);
  389. }
  390. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  391. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  392. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  393. $publishCheck = $publishCheck>0? $publishCheck : 0;
  394. $tags = isset($params['tags']) && $params['tags']? $params['tags'] : '';
  395. $data = [
  396. 'user_id' => $userId,
  397. 'title' => $title,
  398. 'type' => $type,
  399. 'description' => $description,
  400. 'tags' => $tags && is_array($tags)? implode(',', $tags) : $tags,
  401. 'file_url' => $fileUrl,
  402. 'thumb' => $thumb,
  403. 'albums' => $albums? $albums : '',
  404. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  405. 'music_name' => isset($params['music_name'])? trim($params['music_name']) : '',
  406. 'music_url' => $musicUrl,
  407. 'goods_id' => $goodsId,
  408. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  409. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  410. 'status' => $publishCheck? 1 : 2,
  411. 'mark' => 1,
  412. 'publish_at' => date('Y-m-d H:i:s'),
  413. 'create_time' => time(),
  414. ];
  415. RedisService::set('caches:videos:publish', ['params'=> $params,'data'=> $data], 600);
  416. if($id = $this->model->insertGetId($data)){
  417. $this->error = 1023;
  418. return ['id'=> $id];
  419. }
  420. $this->error = 1024;
  421. return false;
  422. }
  423. /**
  424. * 状态设置
  425. * @return bool
  426. */
  427. public function status()
  428. {
  429. $id = request()->post('id', 0);
  430. $status = request()->post('status', 1);
  431. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  432. $this->error = 2981;
  433. return false;
  434. }
  435. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  436. $this->error = 1002;
  437. return true;
  438. }
  439. $this->error = 1003;
  440. return true;
  441. }
  442. /**
  443. * 删除
  444. * @return bool
  445. */
  446. public function delete()
  447. {
  448. // 参数
  449. $param = request()->all();
  450. $id = getter($param, "id");
  451. if (empty($id)) {
  452. $this->error = 2014;
  453. return false;
  454. }
  455. if(!$this->model->where(['id'=> $id])->value('id')){
  456. $this->error = 1039;
  457. return false;
  458. }
  459. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  460. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  461. $this->error = 1025;
  462. return true;
  463. }else{
  464. $this->error = 1026;
  465. return false;
  466. }
  467. }
  468. }