VideoController.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Jobs\Video\AgentUpdateChildUidsByAllParentUidsJob;
  5. use App\Models\Mysql\VideoCategoryModel;
  6. use App\Models\Mysql\VideoModel;
  7. use App\Repositories\AppRepository;
  8. use App\Repositories\Criterias\OrderLimitCriteria;
  9. use App\Repositories\Criterias\VideoCategoryPagesCriteria;
  10. use App\Repositories\Criterias\VideoPagesCriteria;
  11. use App\Repositories\Criterias\VideoTagPagesCriteria;
  12. use App\Repositories\VideoCategoryRepository;
  13. use App\Repositories\VideoRepository;
  14. use App\Repositories\VideoTagRepository;
  15. use Illuminate\Support\Facades\Validator;
  16. use Modules\ManageApi\Responses\CommonAllResponse;
  17. use Modules\ManageApi\Responses\CommonDetailResponse;
  18. use Modules\ManageApi\Responses\CommonPagesResponse;
  19. use Modules\ManageApi\Responses\CommonSuccessPkResponse;
  20. use Modules\ManageApi\Responses\CommonSuccessUidResponse;
  21. use Modules\ManageApi\Responses\VideoCategoryDetailResponse;
  22. use Modules\ManageApi\Responses\VideoCategoryPagesResponse;
  23. use Modules\ManageApi\Responses\VideoDetailResponse;
  24. use Modules\ManageApi\Responses\VideoPagesResponse;
  25. use Modules\ManageApi\Services\VideoTagService;
  26. class VideoController extends BaseController
  27. {
  28. /**
  29. * 视频列表
  30. * @return \Illuminate\Http\JsonResponse|VideoPagesResponse
  31. */
  32. public function lists()
  33. {
  34. /**
  35. * @var Validator $validatorFactory
  36. */
  37. $validatorFactory = app('validator');
  38. /**
  39. * @var VideoRepository $videoRepo
  40. */
  41. $videoRepo = app(VideoRepository::class);
  42. $inputData = $this->getJsonData();
  43. // 针对layui table自动分页请求参数,优先GET值.
  44. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  45. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  46. if (!is_null($queryPage)) {
  47. $inputData['page'] = $queryPage;
  48. }
  49. if (!is_null($queryPageSize)) {
  50. $inputData['page_size'] = $queryPageSize;
  51. }
  52. $rules = [
  53. 'page' => 'nullable|integer',
  54. 'page_size' => 'nullable|integer',
  55. 'video_uid' => 'nullable',
  56. 'type' => 'nullable|integer',
  57. 'category_uid' => 'nullable',
  58. 'tags' => 'nullable',
  59. 'status' => 'nullable|integer',
  60. 'title' => 'nullable',
  61. ];
  62. $validator = $validatorFactory->make($inputData, $rules);
  63. if ($validator->fails()) {
  64. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  65. return json_fail($validator->errors()->first());
  66. }
  67. $orders = [
  68. ['created_at', 'DESC'],
  69. ];
  70. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  71. if ($pageSize > 500) {
  72. $pageSize = CommonConstant::PAGE_SIZE;
  73. }
  74. $page = format_page($inputData['page'] ?? 0);
  75. // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
  76. $items = $videoRepo->pushCriteria(new VideoPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  77. return new VideoPagesResponse($items);
  78. }
  79. /**
  80. * 视频详情
  81. * @return \Illuminate\Http\JsonResponse|VideoDetailResponse
  82. */
  83. public function detail()
  84. {
  85. /**
  86. * @var Validator $validatorFactory
  87. */
  88. $validatorFactory = app('validator');
  89. /**
  90. * @var VideoRepository $videoRepo
  91. */
  92. $videoRepo = app(VideoRepository::class);
  93. $inputData = $this->getJsonData();
  94. $rules = [
  95. 'video_uid' => 'required|string',
  96. ];
  97. $validator = $validatorFactory->make($inputData, $rules);
  98. if ($validator->fails()) {
  99. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  100. return json_fail($validator->errors()->first());
  101. }
  102. $video = $videoRepo->findBy('video_uid', $inputData['video_uid']);
  103. return new VideoDetailResponse($video);
  104. }
  105. /**
  106. * 修改视频
  107. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  108. */
  109. public function modify()
  110. {
  111. /**
  112. * @var Validator $validatorFactory
  113. */
  114. $validatorFactory = app('validator');
  115. /**
  116. * @var VideoRepository $videoRepo
  117. */
  118. $videoRepo = app(VideoRepository::class);
  119. $inputData = $this->getJsonData();
  120. $rules = [
  121. 'video_uid' => 'required|string',
  122. 'type' => 'required|integer',
  123. 'category_uid' => 'required|string',
  124. 'title' => 'required|max:255',
  125. 'cover' => 'nullable',
  126. 'duration' => 'nullable',
  127. 'bitrate' => 'nullable',
  128. 'resolution' => 'nullable|max:16',
  129. 'tags' => 'nullable|max:700',
  130. 'screenshots' => 'nullable|array',
  131. 'status' => 'nullable|integer',
  132. ];
  133. $validator = $validatorFactory->make($inputData, $rules);
  134. if ($validator->fails()) {
  135. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  136. return json_fail($validator->errors()->first());
  137. }
  138. $video = $videoRepo->findBy('video_uid', $inputData['video_uid']);
  139. if (empty($video)) {
  140. return json_fail(trans('common.err_resource_not_found'));
  141. }
  142. $oldVideo = clone $video;
  143. if (isset($inputData['type'])) {
  144. $type = intval($inputData['type']);
  145. $video->type = $type;
  146. }
  147. if (isset($inputData['category_uid'])) {
  148. $categoryUid = trim($inputData['category_uid']);
  149. $video->category_uid = $categoryUid;
  150. }
  151. if (isset($inputData['title'])) {
  152. $title = trim($inputData['title']);
  153. $video->title = $title;
  154. }
  155. if (isset($inputData['cover'])) {
  156. $cover = trim($inputData['cover']);
  157. if (!empty($cover)) {
  158. if (!stripos($cover, '|')) {
  159. return json_fail('封面必须包含服务器组滚符号');
  160. }
  161. }
  162. $video->cover = $cover;
  163. }
  164. if (isset($inputData['duration'])) {
  165. $duration = intval($inputData['duration']);
  166. $video->duration = $duration;
  167. }
  168. if (isset($inputData['bitrate'])) {
  169. $bitrate = intval($inputData['bitrate']);
  170. $video->bitrate = $bitrate;
  171. }
  172. if (isset($inputData['resolution'])) {
  173. $resolution = strtolower(trim($inputData['resolution']));
  174. $video->resolution = $resolution;
  175. }
  176. if (isset($inputData['tags'])) {
  177. if (is_array($inputData['tags'])) {
  178. $strTags = implode(',', $inputData['tags']);
  179. } else {
  180. $strTags = (string)$inputData['tags'];
  181. }
  182. $video->tags = $strTags;
  183. }
  184. if (isset($inputData['screenshots'])) {
  185. if (is_array($inputData['screenshots'])) {
  186. $strScreenshots = implode(',', $inputData['screenshots']);
  187. } else {
  188. $strScreenshots = $inputData['screenshots'];
  189. }
  190. $video->screenshots = $strScreenshots;
  191. }
  192. if (isset($inputData['queue_status'])) {
  193. $queue_status = intval($inputData['queue_status']);
  194. $video->queue_status = $queue_status;
  195. }
  196. if (isset($inputData['status'])) {
  197. $status = intval($inputData['status']);
  198. $video->status = $status;
  199. }
  200. $result = $video->save();
  201. if (!$result) {
  202. return json_fail('编辑失败');
  203. } else {
  204. $logSubject = '编辑一个视频, 标题:' . $video->title;
  205. $logSvalue = $video->video_uid;
  206. $logDbData = [
  207. 'after_items' => [
  208. $video->toArray(),
  209. ],
  210. 'before_items' => [
  211. $oldVideo->toArray(),
  212. ],
  213. ];
  214. $this->log($logSubject, $logSvalue, $logDbData);
  215. return new CommonSuccessUidResponse($video->video_uid);
  216. }
  217. }
  218. /**
  219. * 删除视频
  220. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  221. */
  222. public function delete()
  223. {
  224. /**
  225. * @var Validator $validatorFactory
  226. */
  227. $validatorFactory = app('validator');
  228. /**
  229. * @var VideoRepository $videoRepo
  230. */
  231. $videoRepo = app(VideoRepository::class);
  232. $inputData = $this->getJsonData();
  233. $rules = [
  234. 'video_uid' => 'required|string',
  235. ];
  236. $validator = $validatorFactory->make($inputData, $rules);
  237. if ($validator->fails()) {
  238. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  239. return json_fail($validator->errors()->first());
  240. }
  241. $video = $videoRepo->findBy('video_uid', $inputData['video_uid']);
  242. if (empty($video)) {
  243. return json_fail(trans('common.err_resource_not_found'));
  244. }
  245. $oldVideo = clone $video;
  246. $result = $video->delete();
  247. if (!$result) {
  248. return json_fail('删除失败');
  249. } else {
  250. $logSubject = '删除一个视频,标题:' . $oldVideo->title;
  251. $logSvalue = $oldVideo->video_uid;
  252. $logDbData = [
  253. 'after_items' => [
  254. ],
  255. 'before_items' => [
  256. $oldVideo->toArray(),
  257. ],
  258. ];
  259. $this->log($logSubject, $logSvalue, $logDbData);
  260. return new CommonSuccessUidResponse($inputData['video_uid']);
  261. }
  262. }
  263. /**
  264. * 批量删除视频
  265. */
  266. public function batchDelete()
  267. {
  268. /**
  269. * @var Validator $validatorFactory
  270. */
  271. $validatorFactory = app('validator');
  272. /**
  273. * @var VideoRepository $videoRepo
  274. */
  275. $inputData = $this->getJsonData();
  276. $rules = [
  277. 'video_uids' => 'required|string',
  278. ];
  279. $validator = $validatorFactory->make($inputData, $rules);
  280. if ($validator->fails()) {
  281. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  282. return json_fail($validator->errors()->first());
  283. }
  284. // 要删除的UID
  285. $videoUids = explode(',', $inputData['video_uids']);
  286. $videoRepo = app(VideoRepository::class);
  287. $videoModel = clone $videoRepo;
  288. // 查询有UID的数据
  289. $videoKeys = $videoRepo->findArray('video_uid', $videoUids);
  290. if (empty($videoKeys)) {
  291. return json_fail(trans('common.err_resource_not_found'));
  292. }
  293. // 批量删除
  294. $result = $videoModel->delectUids('video_uid', $videoUids);
  295. $videoKeysStr = implode(',', $videoKeys);
  296. if (!$result) {
  297. return json_fail('删除失败');
  298. } else {
  299. $logSubject = '批量删除视频:' . $videoKeysStr;
  300. $logSvalue = $videoKeysStr;
  301. $logDbData = [
  302. 'after_items' => [
  303. ],
  304. 'before_items' => [
  305. $videoKeysStr,
  306. ],
  307. ];
  308. $this->log($logSubject, $logSvalue, $logDbData);
  309. return new CommonSuccessUidResponse($inputData['video_uids']);
  310. }
  311. }
  312. /**
  313. * 批量操作视频.
  314. * @return \Illuminate\Http\JsonResponse|mixed
  315. */
  316. public function batch()
  317. {
  318. /**
  319. * @var Validator $validatorFactory
  320. */
  321. $validatorFactory = app('validator');
  322. /**
  323. * @var VideoRepository $videoRepo
  324. */
  325. $videoRepo = app(VideoRepository::class);
  326. $inputData = $this->getJsonData();
  327. $operateName = $inputData['operate_name'] ?? '';
  328. switch ($operateName) {
  329. case 'delete':
  330. $itemPks = $inputData['item_pks'] ?? [];
  331. if (!empty($itemPks)) {
  332. $model = new VideoModel();
  333. $resultItems = $model->whereIn('video_uid', $itemPks)->delete();
  334. if ($resultItems) {
  335. $strPks = implode(',', $itemPks);
  336. $logSubject = '批量删除多个视频';
  337. $logSvalue = $strPks;
  338. $logDbData = [
  339. 'after_items' => [
  340. ],
  341. 'before_items' => [
  342. ],
  343. ];
  344. $this->log($logSubject, $logSvalue, $logDbData);
  345. }
  346. }
  347. $data = [
  348. 'result' => 'success',
  349. 'pks' => $itemPks,
  350. ];
  351. return json_success($data);
  352. break;
  353. default:
  354. break;
  355. }
  356. return json_fail('未知操作');
  357. }
  358. /**
  359. * 视频分类分页
  360. * @return \Illuminate\Http\JsonResponse|VideoCategoryPagesResponse
  361. */
  362. public function categoryLists()
  363. {
  364. /**
  365. * @var Validator $validatorFactory
  366. */
  367. $validatorFactory = app('validator');
  368. /**
  369. * @var VideoCategoryRepository $categoryRepo
  370. */
  371. $categoryRepo = app(VideoCategoryRepository::class);
  372. $inputData = $this->getJsonData();
  373. // 针对layui table自动分页请求参数,优先GET值.
  374. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  375. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  376. if (!is_null($queryPage)) {
  377. $inputData['page'] = $queryPage;
  378. }
  379. if (!is_null($queryPageSize)) {
  380. $inputData['page_size'] = $queryPageSize;
  381. }
  382. $rules = [
  383. 'page' => 'nullable|integer',
  384. 'page_size' => 'nullable|integer',
  385. 'category_uid' => 'nullable',
  386. 'name' => 'nullable',
  387. 'status' => 'nullable|integer',
  388. ];
  389. $validator = $validatorFactory->make($inputData, $rules);
  390. if ($validator->fails()) {
  391. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  392. return json_fail($validator->errors()->first());
  393. }
  394. $orders = [
  395. ['created_at', 'DESC'],
  396. ];
  397. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  398. if ($pageSize > 500) {
  399. $pageSize = CommonConstant::PAGE_SIZE;
  400. }
  401. $page = format_page($inputData['page'] ?? 0);
  402. // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
  403. $items = $categoryRepo->pushCriteria(new VideoCategoryPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  404. return new VideoCategoryPagesResponse($items);
  405. }
  406. /**
  407. * 分类详情
  408. * @return \Illuminate\Http\JsonResponse|VideoCategoryDetailResponse
  409. */
  410. public function categoryDetail()
  411. {
  412. /**
  413. * @var Validator $validatorFactory
  414. */
  415. $validatorFactory = app('validator');
  416. /**
  417. * @var VideoCategoryRepository $categoryRepo
  418. */
  419. $categoryRepo = app(VideoCategoryRepository::class);
  420. $inputData = $this->getJsonData();
  421. $rules = [
  422. 'category_uid' => 'required|string',
  423. ];
  424. $validator = $validatorFactory->make($inputData, $rules);
  425. if ($validator->fails()) {
  426. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  427. return json_fail($validator->errors()->first());
  428. }
  429. $category = $categoryRepo->findBy('category_uid', $inputData['category_uid']);
  430. return new VideoCategoryDetailResponse($category);
  431. }
  432. /**
  433. * 创建分类
  434. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  435. */
  436. public function categoryCreate()
  437. {
  438. /**
  439. * @var Validator $validatorFactory
  440. */
  441. $validatorFactory = app('validator');
  442. /**
  443. * @var VideoCategoryRepository $categoryRepo
  444. */
  445. $categoryRepo = app(VideoCategoryRepository::class);
  446. $inputData = $this->getJsonData();
  447. $rules = [
  448. 'name' => 'required|max:60',
  449. 'parent_uid' => 'nullable',
  450. 'squad' => 'nullable|integer',
  451. 'status' => 'nullable|integer',
  452. ];
  453. $validator = $validatorFactory->make($inputData, $rules);
  454. if ($validator->fails()) {
  455. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  456. return json_fail($validator->errors()->first());
  457. }
  458. if (!empty($inputData['parent_uid'])) {
  459. $parentUid = trim($inputData['parent_uid']);
  460. } else {
  461. $parentUid = '';
  462. }
  463. if (!isset($inputData['squad'])) {
  464. $squad = 1000;
  465. } else {
  466. $squad = intval($inputData['squad']);
  467. }
  468. if (!empty($inputData['status'])) {
  469. $status = intval($inputData['status']);
  470. } else {
  471. $status = 1;
  472. }
  473. $setarr = [
  474. 'name' => trim($inputData['name']),
  475. 'parent_uid' => $parentUid,
  476. 'squad' => $squad,
  477. 'status' => $status,
  478. ];
  479. $result = $categoryRepo->create($setarr);
  480. if (!$result) {
  481. return json_fail('创建失败');
  482. } else {
  483. $logSubject = '添加一个视频分类, 名称:' . $result->name;
  484. $logSvalue = $result->category_uid;
  485. $logDbData = [
  486. 'after_items' => [
  487. $result->toArray(),
  488. ],
  489. 'before_items' => [
  490. ],
  491. ];
  492. $this->log($logSubject, $logSvalue, $logDbData);
  493. return new CommonSuccessUidResponse($result->category_uid);
  494. }
  495. }
  496. /**
  497. * 修改分类
  498. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  499. */
  500. public function categoryModify()
  501. {
  502. /**
  503. * @var Validator $validatorFactory
  504. */
  505. $validatorFactory = app('validator');
  506. /**
  507. * @var VideoCategoryRepository $categoryRepo
  508. */
  509. $categoryRepo = app(VideoCategoryRepository::class);
  510. $inputData = $this->getJsonData();
  511. $rules = [
  512. 'category_uid' => 'required|string',
  513. 'name' => 'required|max:60',
  514. 'parent_uid' => 'nullable',
  515. 'squad' => 'nullable|integer',
  516. 'status' => 'nullable|integer',
  517. ];
  518. $validator = $validatorFactory->make($inputData, $rules);
  519. if ($validator->fails()) {
  520. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  521. return json_fail($validator->errors()->first());
  522. }
  523. $category = $categoryRepo->findBy('category_uid', $inputData['category_uid']);
  524. if (empty($category)) {
  525. return json_fail(trans('common.err_resource_not_found'));
  526. }
  527. $oldCategory = clone $category;
  528. if (isset($inputData['name'])) {
  529. $name = trim($inputData['name']);
  530. $category->name = $name;
  531. }
  532. if (isset($inputData['parent_uid'])) {
  533. $parentUid = trim($inputData['parent_uid']);
  534. $category->parent_uid = $parentUid;
  535. }
  536. if (isset($inputData['squad'])) {
  537. $squad = intval($inputData['squad']);
  538. $category->squad = $squad;
  539. }
  540. if (isset($inputData['status'])) {
  541. $status = intval($inputData['status']);
  542. $category->status = $status;
  543. }
  544. $result = $category->save();
  545. if (!$result) {
  546. return json_fail('编辑失败');
  547. } else {
  548. $logSubject = '编辑一个视频分类, 名称:' . $category->name;
  549. $logSvalue = $category->category_uid;
  550. $logDbData = [
  551. 'after_items' => [
  552. $category->toArray(),
  553. ],
  554. 'before_items' => [
  555. $oldCategory->toArray(),
  556. ],
  557. ];
  558. $this->log($logSubject, $logSvalue, $logDbData);
  559. return new CommonSuccessUidResponse($category->category_uid);
  560. }
  561. }
  562. /**
  563. * 修改视频分类状态
  564. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  565. */
  566. public function categoryEditStatus()
  567. {
  568. /**
  569. * @var Validator $validatorFactory
  570. */
  571. $validatorFactory = app('validator');
  572. $inputData = $this->getJsonData();
  573. $rules = [
  574. 'category_uid' => 'required|string',
  575. ];
  576. $validator = $validatorFactory->make($inputData, $rules);
  577. if ($validator->fails()) {
  578. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  579. return json_fail($validator->errors()->first());
  580. }
  581. /**
  582. * @var VideoCategoryRepository $categoryRepo
  583. */
  584. $categoryRepo = app(VideoCategoryRepository::class);
  585. $category = $categoryRepo->findBy('category_uid', $inputData['category_uid']);
  586. if (empty($category)) {
  587. return json_fail(trans('common.err_resource_not_found'));
  588. }
  589. $oldCategory = clone $category;
  590. $category->status = $category->status === 1 ? 2 : 1;
  591. if (!$category->save()) {
  592. return json_fail('删除失败');
  593. } else {
  594. $logSubject = '修改视屏分类状态:' . $category->name;
  595. $logSvalue = $category->category_uid;
  596. $logDbData = [
  597. 'after_items' => [
  598. ],
  599. 'before_items' => [
  600. $oldCategory->toArray(),
  601. ],
  602. ];
  603. $this->log($logSubject, $logSvalue, $logDbData);
  604. return new CommonSuccessUidResponse($inputData['category_uid']);
  605. }
  606. }
  607. /**
  608. * 删除分类
  609. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  610. */
  611. public function categoryDelete()
  612. {
  613. /**
  614. * @var Validator $validatorFactory
  615. */
  616. $validatorFactory = app('validator');
  617. /**
  618. * @var VideoCategoryRepository $categoryRepo
  619. */
  620. $categoryRepo = app(VideoCategoryRepository::class);
  621. $inputData = $this->getJsonData();
  622. $rules = [
  623. 'category_uid' => 'required|string',
  624. ];
  625. $validator = $validatorFactory->make($inputData, $rules);
  626. if ($validator->fails()) {
  627. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  628. return json_fail($validator->errors()->first());
  629. }
  630. $category = $categoryRepo->findBy('category_uid', $inputData['category_uid']);
  631. if (empty($category)) {
  632. return json_fail(trans('common.err_resource_not_found'));
  633. }
  634. $oldCategory = clone $category;
  635. $result = $category->delete();
  636. if (!$result) {
  637. return json_fail('删除失败');
  638. } else {
  639. $logSubject = '删除一个视频分类,名称:' . $oldCategory->username;
  640. $logSvalue = $oldCategory->category_uid;
  641. $logDbData = [
  642. 'after_items' => [
  643. ],
  644. 'before_items' => [
  645. $oldCategory->toArray(),
  646. ],
  647. ];
  648. $this->log($logSubject, $logSvalue, $logDbData);
  649. return new CommonSuccessUidResponse($inputData['category_uid']);
  650. }
  651. }
  652. /**
  653. * 批量删除视频分类
  654. */
  655. public function categoryBatchDelete()
  656. {
  657. /**
  658. * @var Validator $validatorFactory
  659. */
  660. $validatorFactory = app('validator');
  661. $inputData = $this->getJsonData();
  662. $rules = [
  663. 'category_uids' => 'required|string',
  664. ];
  665. $validator = $validatorFactory->make($inputData, $rules);
  666. if ($validator->fails()) {
  667. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  668. return json_fail($validator->errors()->first());
  669. }
  670. $categoryUidArray = explode(',', $inputData['category_uids']);
  671. /**
  672. * @var VideoCategoryRepository $categoryRepo
  673. */
  674. $categoryRepo = app(VideoCategoryRepository::class);
  675. $categoryModel = clone $categoryRepo;
  676. // 查询有UID的数据
  677. $categoryKeys = $categoryRepo->findArray('category_uid', $categoryUidArray);
  678. if (empty($categoryKeys)) {
  679. return json_fail(trans('common.err_resource_not_found'));
  680. }
  681. // 批量删除
  682. $result = $categoryModel->delectUids('category_uid', $categoryKeys);
  683. $categoryStr = implode(',', $categoryUidArray);
  684. if (!$result) {
  685. return json_fail('删除失败');
  686. } else {
  687. $logSubject = '批量删除视频分类:' . $categoryStr;
  688. $logSvalue = $categoryStr;
  689. $logDbData = [
  690. 'after_items' => [
  691. ],
  692. 'before_items' => [
  693. $categoryKeys,
  694. ],
  695. ];
  696. $this->log($logSubject, $logSvalue, $logDbData);
  697. return new CommonSuccessUidResponse($inputData['category_uids']);
  698. }
  699. }
  700. /**
  701. * 批量操作分类
  702. * @return \Illuminate\Http\JsonResponse|mixed
  703. */
  704. public function categoryBatch()
  705. {
  706. /**
  707. * @var Validator $validatorFactory
  708. */
  709. $validatorFactory = app('validator');
  710. /**
  711. * @var VideoCategoryRepository $categoryRepo
  712. */
  713. $categoryRepo = app(VideoCategoryRepository::class);
  714. $inputData = $this->getJsonData();
  715. $operateName = $inputData['operate_name'] ?? '';
  716. switch ($operateName) {
  717. case 'delete':
  718. $itemPks = $inputData['item_pks'] ?? [];
  719. if (!empty($itemPks)) {
  720. $model = new VideoCategoryModel();
  721. $resultItems = $model->whereIn('category_uid', $itemPks)->delete();
  722. if ($resultItems) {
  723. $strPks = implode(',', $itemPks);
  724. $logSubject = '批量删除多个视频分类';
  725. $logSvalue = $strPks;
  726. $logDbData = [
  727. 'after_items' => [
  728. ],
  729. 'before_items' => [
  730. ],
  731. ];
  732. $this->log($logSubject, $logSvalue, $logDbData);
  733. }
  734. }
  735. $data = [
  736. 'result' => 'success',
  737. 'pks' => $itemPks,
  738. ];
  739. return json_success($data);
  740. break;
  741. default:
  742. break;
  743. }
  744. return json_fail('未知操作');
  745. }
  746. /**
  747. * 所有视频分类
  748. * @return \Illuminate\Http\JsonResponse|VideoCategoryPagesResponse
  749. */
  750. public function categoryAll()
  751. {
  752. /**
  753. * @var VideoCategoryRepository $categoryRepo
  754. */
  755. $categoryRepo = app(VideoCategoryRepository::class);
  756. $orders = [
  757. ['created_at', 'DESC'],
  758. ];
  759. // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
  760. $items = $categoryRepo->pushCriteria(new OrderLimitCriteria($orders))->all();
  761. return new CommonAllResponse($items);
  762. }
  763. /**
  764. * 推送APP
  765. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  766. */
  767. public function pushApp()
  768. {
  769. /**
  770. * @var Validator $validatorFactory
  771. */
  772. $validatorFactory = app('validator');
  773. $inputData = $this->getJsonData();
  774. $rules = [
  775. 'app_uid' => 'required|string',
  776. 'video_uids' => 'required|array',
  777. 'addon_params' => 'nullable|max:700',
  778. ];
  779. $validator = $validatorFactory->make($inputData, $rules);
  780. if ($validator->fails()) {
  781. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  782. return json_fail($validator->errors()->first());
  783. }
  784. /**
  785. * @var $appRepo AppRepository
  786. */
  787. $appRepo = make(AppRepository::class);
  788. $item = $appRepo->findBy('app_uid', $inputData['app_uid']);
  789. if ($item->count() == 0) {
  790. return json_fail('无效应用');
  791. }
  792. $pushParams = [
  793. 'app_uid' => $inputData['app_uid'],
  794. 'video_uids' => $inputData['video_uids'],
  795. 'addon_params' => $inputData['addon_params'] ?? '',
  796. ];
  797. dispatch(new AgentUpdateChildUidsByAllParentUidsJob($pushParams));
  798. $strVideoUids = is_array($inputData['video_uids']) ? implode(',', $inputData['video_uids']) : $inputData['video_uids'];
  799. $logSubject = '推送一些视频至应用名称:' . $item->name;
  800. $logSvalue = $strVideoUids;
  801. $logDbData = [
  802. 'after_items' => [
  803. ],
  804. 'before_items' => [
  805. ],
  806. ];
  807. $this->log($logSubject, $logSvalue, $logDbData);
  808. return new CommonSuccessUidResponse($item->app_uid);
  809. }
  810. public function tagLists()
  811. {
  812. /**
  813. * @var Validator $validatorFactory
  814. */
  815. $validatorFactory = app('validator');
  816. /**
  817. * @var VideoTagRepository $tagRepo
  818. */
  819. $tagRepo = app(VideoTagRepository::class);
  820. /**
  821. * @var VideoTagService $tagService
  822. */
  823. $tagService = app(VideoTagService::class);
  824. $inputData = $this->getJsonData();
  825. // 针对layui table自动分页请求参数,优先GET值.
  826. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  827. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  828. if (!is_null($queryPage)) {
  829. $inputData['page'] = $queryPage;
  830. }
  831. if (!is_null($queryPageSize)) {
  832. $inputData['page_size'] = $queryPageSize;
  833. }
  834. $rules = [
  835. 'page' => 'nullable|integer',
  836. 'page_size' => 'nullable|integer',
  837. 'name' => 'nullable',
  838. 'origin' => 'nullable',
  839. ];
  840. $validator = $validatorFactory->make($inputData, $rules);
  841. if ($validator->fails()) {
  842. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  843. return json_fail($validator->errors()->first());
  844. }
  845. $orders = [
  846. ['created_at', 'DESC'],
  847. ];
  848. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  849. if ($pageSize > 500) {
  850. $pageSize = CommonConstant::PAGE_SIZE;
  851. }
  852. $page = format_page($inputData['page'] ?? 0);
  853. // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
  854. $items = $tagRepo->pushCriteria(new VideoTagPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  855. return new CommonPagesResponse($items);
  856. }
  857. public function tagCreate()
  858. {
  859. /**
  860. * @var Validator $validatorFactory
  861. */
  862. $validatorFactory = app('validator');
  863. /**
  864. * @var VideoTagRepository $tagRepo
  865. */
  866. $tagRepo = app(VideoTagRepository::class);
  867. /**
  868. * @var VideoTagService $tagService
  869. */
  870. $tagService = app(VideoTagService::class);
  871. $inputData = $this->getJsonData();
  872. $rules = [
  873. 'tags' => 'required',
  874. 'origin' => 'nullable',
  875. ];
  876. $validator = $validatorFactory->make($inputData, $rules);
  877. if ($validator->fails()) {
  878. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  879. return json_fail($validator->errors()->first());
  880. }
  881. if (!empty($inputData['origin'])) {
  882. $origin = trim($inputData['origin']);
  883. } else {
  884. $origin = '';
  885. }
  886. $result = $tagService->batchCreate($inputData['tags'], $origin);
  887. if (empty($result['names'])) {
  888. return json_fail('创建失败');
  889. } else {
  890. $strNames = implode(',', $result['names']);
  891. $strIds = implode(',', $result['ids']);
  892. $logSubject = '添加视频标签, 名称:' . mb_substr($strNames, 0, 255);
  893. $logSvalue = $strIds;
  894. $logDbData = [
  895. 'after_items' => [
  896. ],
  897. 'before_items' => [
  898. ],
  899. ];
  900. $this->log($logSubject, $logSvalue, $logDbData);
  901. $data = [
  902. 'result' => 'success',
  903. 'pks' => $result['ids'],
  904. ];
  905. return json_success($data);
  906. }
  907. }
  908. public function tagDetail()
  909. {
  910. /**
  911. * @var Validator $validatorFactory
  912. */
  913. $validatorFactory = app('validator');
  914. /**
  915. * @var VideoTagRepository $tagRepo
  916. */
  917. $tagRepo = app(VideoTagRepository::class);
  918. $inputData = $this->getJsonData();
  919. $rules = [
  920. 'id' => 'required',
  921. ];
  922. $validator = $validatorFactory->make($inputData, $rules);
  923. if ($validator->fails()) {
  924. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  925. return json_fail($validator->errors()->first());
  926. }
  927. $tag = $tagRepo->findBy('id', $inputData['id']);
  928. return new CommonDetailResponse($tag);
  929. }
  930. public function tagModify()
  931. {
  932. /**
  933. * @var Validator $validatorFactory
  934. */
  935. $validatorFactory = app('validator');
  936. /**
  937. * @var VideoTagRepository $tagRepo
  938. */
  939. $tagRepo = app(VideoTagRepository::class);
  940. /**
  941. * @var VideoTagService $tagService
  942. */
  943. $tagService = app(VideoTagService::class);
  944. $inputData = $this->getJsonData();
  945. $rules = [
  946. 'id' => 'required',
  947. 'name' => 'required',
  948. 'origin' => 'nullable',
  949. ];
  950. $validator = $validatorFactory->make($inputData, $rules);
  951. if ($validator->fails()) {
  952. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  953. return json_fail($validator->errors()->first());
  954. }
  955. $name = trim(urldecode($inputData['name']));
  956. $tag = $tagRepo->findBy('id', $inputData['id']);
  957. if (empty($tag)) {
  958. return json_fail(trans('common.err_resource_not_found'));
  959. }
  960. $oldTag = clone $tag;
  961. if (isset($inputData['origin'])) {
  962. if (!empty($inputData['origin'])) {
  963. $origin = trim($inputData['origin']);
  964. } else {
  965. $origin = '';
  966. }
  967. $tag->origin = $origin;
  968. }
  969. if (!$tagService->isExists($name, $tag->id)) {
  970. $tag->name = $name;
  971. $result = $tag->save();
  972. } else {
  973. $result = false;
  974. }
  975. if (!$result) {
  976. return json_fail('创建失败');
  977. } else {
  978. $logSubject = '编辑视频标签, 名称:' . $name;
  979. $logSvalue = $inputData['id'];
  980. $logDbData = [
  981. 'after_items' => [
  982. $tag->toArray(),
  983. ],
  984. 'before_items' => [
  985. $oldTag->toArray(),
  986. ],
  987. ];
  988. $this->log($logSubject, $logSvalue, $logDbData);
  989. return new CommonSuccessPkResponse($tag->id);
  990. }
  991. }
  992. public function tagDelete()
  993. {
  994. /**
  995. * @var Validator $validatorFactory
  996. */
  997. $validatorFactory = app('validator');
  998. /**
  999. * @var VideoTagRepository $tagRepo
  1000. */
  1001. $tagRepo = app(VideoTagRepository::class);
  1002. /**
  1003. * @var VideoTagService $tagService
  1004. */
  1005. $tagService = app(VideoTagService::class);
  1006. $inputData = $this->getJsonData();
  1007. $rules = [
  1008. 'id' => 'required',
  1009. ];
  1010. $validator = $validatorFactory->make($inputData, $rules);
  1011. if ($validator->fails()) {
  1012. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  1013. return json_fail($validator->errors()->first());
  1014. }
  1015. $tag = $tagRepo->findBy('id', $inputData['id']);
  1016. if (empty($tag)) {
  1017. return json_fail(trans('common.err_resource_not_found'));
  1018. }
  1019. $oldTag = clone $tag;
  1020. $result = $tag->delete();
  1021. if (!$result) {
  1022. return json_fail('删除失败');
  1023. } else {
  1024. $logSubject = '删除一个视频标签,名称:' . $oldTag->name;
  1025. $logSvalue = $oldTag->id;
  1026. $logDbData = [
  1027. 'after_items' => [
  1028. ],
  1029. 'before_items' => [
  1030. $oldTag->toArray(),
  1031. ],
  1032. ];
  1033. $this->log($logSubject, $logSvalue, $logDbData);
  1034. return new CommonSuccessPkResponse($inputData['id']);
  1035. }
  1036. }
  1037. /**
  1038. * 批量删除标签
  1039. * @return \Illuminate\Http\JsonResponse|CommonSuccessPkResponse
  1040. */
  1041. public function tagBatchDelete()
  1042. {
  1043. /**
  1044. * @var Validator $validatorFactory
  1045. */
  1046. $validatorFactory = app('validator');
  1047. /**
  1048. * @var VideoTagRepository $tagRepo
  1049. */
  1050. $tagRepo = app(VideoTagRepository::class);
  1051. /**
  1052. * @var VideoTagService $tagService
  1053. */
  1054. $tagService = app(VideoTagService::class);
  1055. $inputData = $this->getJsonData();
  1056. $rules = [
  1057. 'tagIds' => 'required',
  1058. ];
  1059. $validator = $validatorFactory->make($inputData, $rules);
  1060. if ($validator->fails()) {
  1061. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  1062. return json_fail($validator->errors()->first());
  1063. }
  1064. $tagModel = clone $tagRepo;
  1065. $tahIdsArray = explode(',', $inputData['tagIds']);
  1066. $tagIds = $tagRepo->findArray('id', $tahIdsArray);
  1067. if (empty($tagIds)) {
  1068. return json_fail(trans('common.err_resource_not_found'));
  1069. }
  1070. $tagIdsStr = implode(',', $tagIds);
  1071. if (!$tagModel->delectUids('id', $tagIds)) {
  1072. return json_fail('删除失败');
  1073. } else {
  1074. $logSubject = '批量删除视频标签 id:' . $tagIdsStr;
  1075. $logSvalue = $tagIdsStr;
  1076. $logDbData = [
  1077. 'after_items' => [
  1078. ],
  1079. 'before_items' => [
  1080. $tagIds,
  1081. ],
  1082. ];
  1083. $this->log($logSubject, $logSvalue, $logDbData);
  1084. return new CommonSuccessPkResponse($inputData['tagIds']);
  1085. }
  1086. }
  1087. public function tagBatch()
  1088. {
  1089. /**
  1090. * @var Validator $validatorFactory
  1091. */
  1092. $validatorFactory = app('validator');
  1093. /**
  1094. * @var VideoTagRepository $tagRepo
  1095. */
  1096. $tagRepo = app(VideoTagRepository::class);
  1097. /**
  1098. * @var VideoTagService $tagService
  1099. */
  1100. $tagService = app(VideoTagService::class);
  1101. $inputData = $this->getJsonData();
  1102. $operateName = $inputData['operate_name'] ?? '';
  1103. switch ($operateName) {
  1104. case 'delete':
  1105. $itemPks = $inputData['item_pks'] ?? [];
  1106. if (!empty($itemPks)) {
  1107. $model = new VideoModel();
  1108. $resultItems = $model->whereIn('id', $itemPks)->delete();
  1109. if ($resultItems) {
  1110. $strPks = implode(',', $itemPks);
  1111. $logSubject = '批量删除多个视频标签';
  1112. $logSvalue = $strPks;
  1113. $logDbData = [
  1114. 'after_items' => [
  1115. ],
  1116. 'before_items' => [
  1117. ],
  1118. ];
  1119. $this->log($logSubject, $logSvalue, $logDbData);
  1120. }
  1121. }
  1122. $data = [
  1123. 'result' => 'success',
  1124. 'pks' => $itemPks,
  1125. ];
  1126. return json_success($data);
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. return json_fail('未知操作');
  1132. }
  1133. }