VideoService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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('lev_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
  279. * @param $dynamicId
  280. * @return array|mixed
  281. */
  282. public function updateView($userId, $id)
  283. {
  284. $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
  285. $data = RedisService::get($cacheKey);
  286. if($data){
  287. return false;
  288. }
  289. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  290. RedisService::set($cacheKey, $id, rand(1,3)*3600);
  291. return $data;
  292. }
  293. /**
  294. * 发布
  295. * @param $goodsId
  296. * @return bool
  297. */
  298. public function publish($userId, $params, $request)
  299. {
  300. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  301. $type = isset($params['type'])? intval($params['type']) : 2;
  302. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  303. ->select(['id','nickname','status'])
  304. ->first();
  305. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  306. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  307. if(empty($userInfo) || $status != 1){
  308. $this->error = 2017;
  309. return false;
  310. }
  311. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  312. if($thumb){
  313. $result = upload_base64($thumb);
  314. $thumb = isset($result['file_path'])? $result['file_path'] : '';
  315. if(empty($thumb)){
  316. $this->error = 2208;
  317. return false;
  318. }
  319. }
  320. // 音乐
  321. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  322. if($musicUrl){
  323. $result = upload_remote_image($musicUrl,'mp3','music');
  324. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  325. if(empty($musicUrl)){
  326. $this->error = 2209;
  327. return false;
  328. }
  329. }
  330. // 视频
  331. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  332. $albums = isset($params['album_urls'])? $params['album_urls'] : [];
  333. if($type == 1) {
  334. if($fileUrl){
  335. $result = upload_video($request,'video');
  336. $data = isset($result['data'])? $result['data'] : [];
  337. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  338. if(empty($fileUrl)){
  339. $this->error = 2212;
  340. return false;
  341. }
  342. }else{
  343. $this->error = 2213;
  344. return false;
  345. }
  346. }
  347. // 相册
  348. else if($type == 2){
  349. var_dump($albums);
  350. if(empty($albums) || !is_array($albums)){
  351. $this->error = 2211;
  352. return false;
  353. }
  354. $albums = get_format_images($albums);
  355. }
  356. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  357. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  358. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  359. $publishCheck = $publishCheck>0? $publishCheck : 0;
  360. $tags = isset($params['tags']) && $params['tags']? $params['tags'] : '';
  361. $data = [
  362. 'user_id' => $userId,
  363. 'title' => $title,
  364. 'type' => $type,
  365. 'description' => $description,
  366. 'tags' => $tags && is_array($tags)? implode(',', $tags) : $tags,
  367. 'file_url' => $fileUrl,
  368. 'thumb' => $thumb,
  369. 'albums' => $albums? $albums : '',
  370. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  371. 'music_name' => isset($params['music_name'])? trim($params['music_name']) : '',
  372. 'music_url' => $musicUrl,
  373. 'goods_id' => $goodsId,
  374. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  375. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  376. 'status' => $publishCheck? 1 : 2,
  377. 'mark' => 1,
  378. 'publish_at' => date('Y-m-d H:i:s'),
  379. 'create_time' => time(),
  380. ];
  381. RedisService::set('caches:videos:publish', ['params'=> $params,'data'=> $data], 600);
  382. if($id = $this->model->insertGetId($data)){
  383. $this->error = 1023;
  384. return ['id'=> $id];
  385. }
  386. $this->error = 1024;
  387. return false;
  388. }
  389. /**
  390. * 状态设置
  391. * @return bool
  392. */
  393. public function status()
  394. {
  395. $id = request()->post('id', 0);
  396. $status = request()->post('status', 1);
  397. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  398. $this->error = 2981;
  399. return false;
  400. }
  401. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  402. $this->error = 1002;
  403. return true;
  404. }
  405. $this->error = 1003;
  406. return true;
  407. }
  408. /**
  409. * 删除
  410. * @return bool
  411. */
  412. public function delete()
  413. {
  414. // 参数
  415. $param = request()->all();
  416. $id = getter($param, "id");
  417. if (empty($id)) {
  418. $this->error = 2014;
  419. return false;
  420. }
  421. if(!$this->model->where(['id'=> $id])->value('id')){
  422. $this->error = 1039;
  423. return false;
  424. }
  425. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  426. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  427. $this->error = 1025;
  428. return true;
  429. }else{
  430. $this->error = 1026;
  431. return false;
  432. }
  433. }
  434. }