VideoService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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,'a.is_short'=>2,'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, $userId) {
  65. $type = isset($params['type']) ? $params['type'] : 0;
  66. if ($type > 0) {
  67. if($type == 1){
  68. // 发布的
  69. $query->where('a.user_id', $userId);
  70. }else if($type == 2){
  71. // 收藏
  72. $ids = VideoCollectModel::where(['user_id'=> $userId,'type'=>2,'source_type'=>1,'status'=>1,'mark'=>1])->pluck('collect_id');
  73. $ids = $ids? $ids : ['0'];
  74. if($ids){
  75. $query->whereIn('a.id', $ids);
  76. }
  77. }else if($type == 3){
  78. // 点赞喜欢
  79. $ids = VideoCollectModel::where(['user_id'=> $userId,'type'=>3,'source_type'=>1,'status'=>1,'mark'=>1])->pluck('collect_id');
  80. $ids = $ids? $ids : ['0'];
  81. if($ids){
  82. $query->whereIn('a.id', $ids);
  83. }
  84. }else if($type == 4){
  85. // 浏览历史
  86. $ids = VideoCollectModel::where(['user_id'=> $userId,'type'=>1,'source_type'=>1,'status'=>1,'mark'=>1])->pluck('collect_id');
  87. $ids = $ids? $ids : ['0'];
  88. if($ids){
  89. $query->whereIn('a.id', $ids);
  90. }
  91. }
  92. }
  93. $uid = isset($params['uid']) ? $params['uid'] : 0;
  94. if ($uid > 0) {
  95. $query->where(['a.user_id'=>$uid,'a.status'=>2]);
  96. }else if($type == 0){
  97. $query->where('a.status', 2);
  98. }
  99. $id = isset($params['id']) ? $params['id'] : 0;
  100. if ($id > 0) {
  101. $query->whereNotIn('a.id', [$id]);
  102. }
  103. })
  104. ->where(function ($query) use ($params) {
  105. $keyword = isset($params['kw']) ? $params['kw'] : '';
  106. if ($keyword) {
  107. $query->where('a.title', 'like', "%{$keyword}%")
  108. ->orWhere('a.tags', 'like', "%{$keyword}%")
  109. ->orWhere('a.description', 'like', "%{$keyword}%")
  110. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  111. }
  112. })
  113. ->where(function ($query) use ($params, $userId) {
  114. $query->where(function($query) use($userId){
  115. // 所有人可见
  116. $query->where('a.visible_type', 1);
  117. })->orWhere(function($query) use($userId){
  118. // 关注视频发布用户可见
  119. $uids = $uids = MemberCollectService::make()->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid');
  120. $uids = $uids? $uids : [0];
  121. $query->where('a.visible_type', 2)->where('a.user_id', $uids); // 仅自己可见
  122. })->orWhere(function($query) use($userId){
  123. // 仅自己可见
  124. $query->where('a.visible_type', 3)->where('a.user_id', $userId); // 仅自己可见
  125. });
  126. })
  127. ->selectRaw($field)
  128. ->orderByRaw($order)
  129. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  130. $list = $list ? $list->toArray() : [];
  131. if ($list && $list['data']) {
  132. foreach ($list['data'] as &$item) {
  133. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  134. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  135. if(isset($item['albums'])){
  136. $albums = $item['albums']? json_decode($item['albums'], true):[];
  137. $item['albums'] = $albums? get_images_preview($albums) : [];
  138. }
  139. if(isset($item['file_url'])){
  140. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  141. }
  142. if(isset($item['music_url'])){
  143. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  144. }
  145. $member = isset($item['member'])? $item['member'] : [];
  146. if($member){
  147. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  148. }
  149. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  150. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  151. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? intval($item['collect_num']) : 0;
  152. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  153. $item['comment_num'] = isset($item['comment_num']) && $item['comment_num']? intval($item['comment_num']) : 0;
  154. $item['is_like'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 3);
  155. $item['is_collect'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 2);
  156. $item['is_follow'] = MemberCollectService::make()->checkCollect($userId, $item['user_id'], 1);
  157. $item['is_fans'] = MemberCollectService::make()->checkCollect($item['user_id'], $userId, 1);
  158. $item['is_publisher'] = $userId == $item['user_id']? 1 : 0;
  159. $item['member'] = $member;
  160. }
  161. }
  162. return [
  163. 'pageSize' => $pageSize,
  164. 'total' => isset($list['total']) ? $list['total'] : 0,
  165. 'list' => isset($list['data']) ? $list['data'] : []
  166. ];
  167. }
  168. /**
  169. * 列表数据
  170. * @param $params
  171. * @param int $pageSize
  172. * @return array
  173. */
  174. public function getIndexList($params, $pageSize = 6, $field='', $userId=0)
  175. {
  176. $cacheKey = "caches:videos:index_{$userId}_{$pageSize}";
  177. $datas = RedisService::get($cacheKey);
  178. if($datas){
  179. return $datas;
  180. }
  181. $where = ['a.mark' => 1,'a.status'=>2,'a.is_short'=>2,'b.mark'=>1];
  182. $field = $field? $field : 'lev_a.*';
  183. $order = 'rand()';
  184. $model = $this->model->with(['member'])->from('videos as a')
  185. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  186. ->where($where)
  187. ->where(function ($query) use ($params) {
  188. $type = isset($params['type']) ? $params['type'] : 0;
  189. if ($type > 0) {
  190. $query->where('a.type', $type);
  191. }
  192. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  193. if ($uid > 0) {
  194. $query->where('a.user_id', $uid);
  195. }
  196. })
  197. ->where(function ($query) use ($params) {
  198. $keyword = isset($params['kw']) ? $params['kw'] : '';
  199. if ($keyword) {
  200. $query->where('a.title', 'like', "%{$keyword}%")
  201. ->orWhere('a.tags', 'like', "%{$keyword}%")
  202. ->orWhere('a.description', 'like', "%{$keyword}%")
  203. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  204. }
  205. })
  206. ->where(function ($query) use ($params, $userId) {
  207. $query->where(function($query) use($userId){
  208. // 所有人可见
  209. $query->where('a.visible_type', 1);
  210. })->orWhere(function($query) use($userId){
  211. // 关注视频发布用户可见
  212. $uids = $uids = MemberCollectService::make()->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid');
  213. $uids = $uids? $uids : [0];
  214. $query->where('a.visible_type', 2)->where('a.user_id', $uids); // 仅自己可见
  215. })->orWhere(function($query) use($userId){
  216. // 仅自己可见
  217. $query->where('a.visible_type', 3)->where('a.user_id', $userId); // 仅自己可见
  218. });
  219. });
  220. // 推荐的数据
  221. $uids = [];
  222. $countModel = clone $model;
  223. $countModel = $countModel->whereNotIn('user_id',[$userId])
  224. ->where(function($query) use($params, $userId, &$uids){
  225. // 推荐视频数据
  226. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  227. if ($isRecommend > 0) {
  228. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  229. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  230. $tags = isset($recommendData['tags'])? $recommendData['tags'] : []; // 按标签推荐
  231. $uids = array_unique($uids);
  232. if($uids){
  233. $query->orWhere(function($query) use($uids){
  234. $query->whereIn('a.user_id', $uids);
  235. });
  236. }
  237. if($tags){
  238. $query->orWhere(function($query) use($tags){
  239. foreach($tags as $tag){
  240. $query->where('a.tags', 'like',"%{$tag}%")
  241. ->orWhere('a.description','like',"%{$tag}%");
  242. }
  243. });
  244. }
  245. }
  246. });
  247. $total = $countModel->count('a.id');
  248. // 随机优先匹配推送
  249. $match = false;
  250. $list = [];
  251. $ids = [];
  252. if($total > 0 ){
  253. // 关联推荐数据
  254. $list = $countModel->selectRaw($field)
  255. ->orderByRaw($order)
  256. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  257. $list = $list ? $list->toArray() : [];
  258. $list = isset($list['data']) && $list['data']? $list['data'] :[];
  259. if($list){
  260. foreach ($list as $item){
  261. $ids[] = $item['id'];
  262. }
  263. }
  264. }
  265. // 数据不够补充默认匹配数据
  266. if($total<$pageSize){
  267. // 默认推荐数据
  268. $list1 = $model->where(function($query) use($ids){
  269. if($ids){
  270. $query->whereNotIn('a.id', $ids);
  271. }
  272. })->selectRaw($field)
  273. ->orderByRaw($order)
  274. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  275. $list1 = $list1 ? $list1->toArray() : [];
  276. $list1 = isset($list1['data']) && $list1['data']? $list1['data'] :[];
  277. $list = array_merge($list, $list1);
  278. }
  279. if ($list) {
  280. foreach ($list as &$item) {
  281. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  282. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  283. if(isset($item['albums'])){
  284. $albums = $item['albums']? json_decode($item['albums'], true):[];
  285. $item['albums'] = $albums? get_images_preview($albums) : [];
  286. }
  287. if(isset($item['file_url'])){
  288. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  289. }
  290. if(isset($item['music_url'])){
  291. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  292. }
  293. $member = isset($item['member'])? $item['member'] : [];
  294. if($member){
  295. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  296. }
  297. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  298. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  299. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? intval($item['collect_num']) : 0;
  300. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  301. $item['comment_num'] = isset($item['comment_num']) && $item['comment_num']? intval($item['comment_num']) : 0;
  302. $item['is_like'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 3);
  303. $item['is_collect'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 2);
  304. $item['is_follow'] = MemberCollectService::make()->checkCollect($userId, $item['user_id'], 1);
  305. $item['is_fans'] = MemberCollectService::make()->checkCollect($item['user_id'], $userId, 1);
  306. $item['is_publisher'] = $userId == $item['user_id']? 1 : 0;
  307. $item['member'] = $member;
  308. }
  309. }
  310. $datas = [
  311. 'pageSize' => $pageSize,
  312. "match"=> $match,
  313. 'total' => count($list),
  314. 'list' => $list ? $list : []
  315. ];
  316. RedisService::set($cacheKey, $datas, rand(2,3));
  317. return $datas;
  318. }
  319. /**
  320. * 详情
  321. * @param $id
  322. * @return array
  323. */
  324. public function getInfo($id, $userId=0, $field=[])
  325. {
  326. $field = $field? $field : ['a.*'];
  327. $info = $this->model->from('videos as a')->with(['member'])
  328. ->leftJoin('member as b','b.id','=','a.user_id')
  329. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  330. ->select($field)
  331. ->first();
  332. $info = $info? $info->toArray() : [];
  333. if($info){
  334. if(isset($info['thumb'])){
  335. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  336. }
  337. if(isset($info['file_url'])){
  338. $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
  339. }
  340. if(isset($item['music_url'])){
  341. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  342. }
  343. if(isset($info['albums'])){
  344. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  345. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  346. }
  347. $member = isset($info['member'])? $info['member'] : [];
  348. if($member){
  349. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  350. }
  351. $info['member'] = $member;
  352. $info['tags'] = isset($info['tags']) && $info['tags']? explode(',', $info['tags']) : [];
  353. $info['like_num'] = isset($info['like_num']) && $info['like_num']? intval($info['like_num']) : 0;
  354. $info['collect_num'] = isset($info['collect_num']) && $info['collect_num']? intval($info['collect_num']) : 0;
  355. $info['views'] = isset($info['views']) && $info['views']? intval($info['views']) : 0;
  356. $info['comment_num'] = isset($info['comment_num']) && $info['comment_num']? intval($info['comment_num']) : 0;
  357. $info['is_like'] = VideoCollectService::make()->checkCollect($userId, $info['id'], 3);
  358. $info['is_collect'] = VideoCollectService::make()->checkCollect($userId, $info['id'], 2);
  359. $info['is_follow'] = MemberCollectService::make()->checkCollect($userId, $info['user_id'], 1);
  360. $info['is_fans'] = MemberCollectService::make()->checkCollect($info['user_id'], $userId, 1);
  361. $info['is_publisher'] = $userId == $info['user_id']? 1 : 0;
  362. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  363. $info['is_my'] = $info['user_id'] == $userId ? 1:0;
  364. // 浏览历史
  365. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  366. $data = [
  367. 'user_id'=> $userId,
  368. 'type'=> 1,
  369. 'collect_id'=> $id,
  370. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  371. 'tags'=> isset($info['tags'])&&$info['tags']? implode(',', $info['tags']) : '',
  372. 'create_time'=> time(),
  373. 'status'=> 1,
  374. ];
  375. VideoCollectModel::insert($data);
  376. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
  377. RedisService::clear("caches:videos:recommend:{$userId}_1");
  378. RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
  379. }
  380. // 浏览量
  381. $this->updateView($userId, $id);
  382. }
  383. return $info;
  384. }
  385. /**
  386. * 更新播放浏览历史
  387. * @param $userId 用户ID
  388. * @param $id 视频ID
  389. * @return false
  390. */
  391. public function updatePlay($userId, $id)
  392. {
  393. // 浏览历史
  394. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  395. $info = $this->model->from('videos as a')
  396. ->where(['a.id'=> $id,'a.mark'=>1])
  397. ->select(['a.id','a.tags','a.user_id'])
  398. ->first();
  399. if(empty($info)){
  400. return false;
  401. }
  402. $data = [
  403. 'user_id'=> $userId,
  404. 'type'=> 1,
  405. 'collect_id'=> $id,
  406. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  407. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  408. 'create_time'=> time(),
  409. 'status'=> 1,
  410. ];
  411. VideoCollectModel::insert($data);
  412. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
  413. RedisService::clear("caches:videos:recommend:{$userId}_1");
  414. RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
  415. // 清除数据只保留7天历史数据
  416. VideoCollectModel::where(['user_id'=> $userId,'type'=>1])->where('create_time', '<=', time() - 7 * 86400)->delete();
  417. }
  418. // 浏览量
  419. $this->updateView($userId, $id);
  420. $this->error = 1010;
  421. return true;
  422. }
  423. /**
  424. * 更新浏览量
  425. * @param $userId
  426. * @param $dynamicId
  427. * @return array|mixed
  428. */
  429. public function updateView($userId, $id)
  430. {
  431. $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
  432. $data = RedisService::get($cacheKey);
  433. if($data){
  434. return false;
  435. }
  436. // 浏览视频任务处理
  437. TaskService::make()->updateTask($userId,2, $id);
  438. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  439. RedisService::set($cacheKey, $id, rand(1,3)*3600);
  440. return $data;
  441. }
  442. /**
  443. * 短剧列表
  444. * @param $params
  445. * @param int $pageSize
  446. * @param string $field
  447. * @param $userId
  448. * @return array
  449. */
  450. public function getShortlist($params, $pageSize=18,$field='', $userId)
  451. {
  452. $where = ['a.mark' => 1,'a.is_short'=>1,'a.pid'=>0,'b.mark'=>1];
  453. $field = $field? $field : 'lev_a.*';
  454. $order = 'lev_a.id desc';
  455. $list = $this->model->with(['member'])->from('videos as a')
  456. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  457. ->where($where)
  458. ->where(function ($query) use ($params, $userId) {
  459. $uid = isset($params['uid']) ? $params['uid'] : 0;
  460. if ($uid > 0) {
  461. $query->where('a.user_id', $uid);
  462. }else{
  463. $query->where('a.status', 2);
  464. }
  465. $id = isset($params['id']) ? $params['id'] : 0;
  466. if ($id > 0) {
  467. $query->whereNotIn('a.id', [$id]);
  468. }
  469. })
  470. ->where(function ($query) use ($params) {
  471. $keyword = isset($params['kw']) ? $params['kw'] : '';
  472. if ($keyword) {
  473. $query->where('a.title', 'like', "%{$keyword}%")
  474. ->orWhere('a.tags', 'like', "%{$keyword}%")
  475. ->orWhere('a.description', 'like', "%{$keyword}%");
  476. }
  477. })
  478. ->selectRaw($field)
  479. ->orderByRaw($order)
  480. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  481. $list = $list ? $list->toArray() : [];
  482. if ($list && $list['data']) {
  483. foreach ($list['data'] as &$item) {
  484. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  485. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  486. if(isset($item['file_url'])){
  487. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  488. }
  489. $member = isset($item['member'])? $item['member'] : [];
  490. if($member){
  491. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  492. }
  493. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  494. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  495. $item['member'] = $member;
  496. }
  497. }
  498. return [
  499. 'pageSize' => $pageSize,
  500. 'total' => isset($list['total']) ? $list['total'] : 0,
  501. 'list' => isset($list['data']) ? $list['data'] : []
  502. ];
  503. }
  504. /**
  505. * 详情
  506. * @param $id
  507. * @return array
  508. */
  509. public function getShortInfo($id, $userId=0, $field=[])
  510. {
  511. $field = $field? $field : ['a.*'];
  512. $info = $this->model->from('videos as a')->with(['member','playList'])
  513. ->leftJoin('member as b','b.id','=','a.user_id')
  514. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  515. ->select($field)
  516. ->first();
  517. $info = $info? $info->toArray() : [];
  518. if($info){
  519. if(isset($info['thumb'])){
  520. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  521. }
  522. if(isset($info['file_url'])){
  523. $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
  524. }
  525. $member = isset($info['member'])? $info['member'] : [];
  526. if($member){
  527. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  528. }
  529. $info['member'] = $member;
  530. $info['tags'] = isset($info['tags']) && $info['tags']? explode(',', $info['tags']) : [];
  531. $info['like_num'] = isset($info['like_num']) && $info['like_num']? intval($info['like_num']) : 0;
  532. $info['collect_num'] = isset($info['collect_num']) && $info['collect_num']? intval($info['collect_num']) : 0;
  533. $info['views'] = isset($info['views']) && $info['views']? intval($info['views']) : 0;
  534. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  535. }
  536. return $info;
  537. }
  538. /**
  539. * 发布
  540. * @param $goodsId
  541. * @return bool
  542. */
  543. public function publish($userId, $params, $request)
  544. {
  545. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  546. $type = isset($params['type'])? intval($params['type']) : 2;
  547. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  548. ->select(['id','nickname','status'])
  549. ->first();
  550. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  551. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  552. if(empty($userInfo) || $status != 1){
  553. $this->error = 2017;
  554. return false;
  555. }
  556. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  557. if($thumb){
  558. $thumb = get_image_path($thumb);
  559. if(empty($thumb)){
  560. $this->error = 2208;
  561. return false;
  562. }
  563. }
  564. // 音乐
  565. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  566. if($musicUrl){
  567. $result = upload_remote_image($musicUrl,'mp3','music');
  568. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  569. if(empty($musicUrl)){
  570. $this->error = 2209;
  571. return false;
  572. }
  573. }
  574. // 视频
  575. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  576. $albums = isset($params['album_urls'])?json_decode( $params['album_urls'],true) : [];
  577. if($type == 1) {
  578. if($fileUrl){
  579. $result = upload_video($request,'video');
  580. $data = isset($result['data'])? $result['data'] : [];
  581. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  582. if(empty($fileUrl)){
  583. $this->error = 2212;
  584. return false;
  585. }
  586. }else{
  587. $this->error = 2213;
  588. return false;
  589. }
  590. }
  591. // 相册
  592. else if($type == 2){
  593. if(empty($albums) || !is_array($albums)){
  594. $this->error = 2211;
  595. return false;
  596. }
  597. $albums = get_format_images($albums);
  598. }
  599. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  600. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  601. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  602. $publishCheck = $publishCheck>0? $publishCheck : 0;
  603. $tags = isset($params['tags']) && $params['tags']? $params['tags'] : '';
  604. $data = [
  605. 'user_id' => $userId,
  606. 'title' => $title,
  607. 'type' => $type,
  608. 'description' => $description,
  609. 'tags' => $tags && is_array($tags)? implode(',', $tags) : $tags,
  610. 'file_url' => $fileUrl,
  611. 'thumb' => $thumb,
  612. 'albums' => $albums? $albums : '',
  613. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  614. 'music_name' => isset($params['music_name'])? trim($params['music_name']) : '',
  615. 'music_url' => $musicUrl,
  616. 'goods_id' => $goodsId,
  617. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  618. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  619. 'status' => $publishCheck? 1 : 2,
  620. 'mark' => 1,
  621. 'publish_at' => date('Y-m-d H:i:s'),
  622. 'create_time' => time(),
  623. ];
  624. RedisService::set('caches:videos:publish', ['params'=> $params,'data'=> $data], 600);
  625. if($id = $this->model->insertGetId($data)){
  626. $this->error = 1023;
  627. // 发布视频任务处理
  628. TaskService::make()->updateTask($userId,5, $id);
  629. return ['id'=> $id];
  630. }
  631. $this->error = 1024;
  632. return false;
  633. }
  634. /**
  635. * 状态设置
  636. * @return bool
  637. */
  638. public function status()
  639. {
  640. $id = request()->post('id', 0);
  641. $status = request()->post('status', 1);
  642. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  643. $this->error = 2981;
  644. return false;
  645. }
  646. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  647. $this->error = 1002;
  648. return true;
  649. }
  650. $this->error = 1003;
  651. return true;
  652. }
  653. /**
  654. * 删除
  655. * @return bool
  656. */
  657. public function delete()
  658. {
  659. // 参数
  660. $param = request()->all();
  661. $id = getter($param, "id");
  662. if (empty($id)) {
  663. $this->error = 2014;
  664. return false;
  665. }
  666. if(!$this->model->where(['id'=> $id])->value('id')){
  667. $this->error = 1039;
  668. return false;
  669. }
  670. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  671. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  672. $this->error = 1025;
  673. return true;
  674. }else{
  675. $this->error = 1026;
  676. return false;
  677. }
  678. }
  679. }