VideoService.php 25 KB

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