VideoService.php 18 KB

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