VideoService.php 32 KB

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