VideoController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Modules\Video\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Common\Errors\Errors;
  8. use Modules\Common\Exceptions\CommonBusinessException;
  9. use Modules\User\Models\User;
  10. use Modules\Video\Services\VideoService;
  11. class VideoController extends CatchController
  12. {
  13. use UserTrait;
  14. use ValidatesRequests;
  15. /**
  16. * 短剧列表
  17. * @param Request $request
  18. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  19. */
  20. public function list(Request $request) {
  21. $videoName = $request->input('videoName');
  22. $updateType = $request->input('updateType');
  23. $categoryId = $request->input('categoryId');
  24. $videoId = $request->input('videoId');
  25. $videos = DB::table('videos')
  26. ->when($videoId, function ($query, $videoId){
  27. return $query->where('id', $videoId);
  28. })
  29. ->when($videoName, function ($query, $videoName){
  30. return $query->where('name', 'like', '%'. $videoName . '%');
  31. })->when($updateType, function ($query, $updateType){
  32. return $query->where('update_type', $updateType);
  33. })->when($categoryId, function ($query, $categoryId){
  34. return $query->where('category_id', $categoryId);
  35. })->orderBy('id', 'desc')
  36. ->paginate($request->integer('per_page', 15));
  37. $userContext = $this->getUserContext($request->input('operateUserIdßßß'));
  38. $allVideoCategory = DB::table('video_category')
  39. ->get()->keyBy('id');
  40. foreach ($videos as $video) {
  41. $this->updateVideoInfo($video, $userContext);
  42. $video->category_str = $this->getCategoryStr($allVideoCategory, $video->category_id);
  43. }
  44. return $videos;
  45. }
  46. /**
  47. * 订阅设置
  48. * @param Request $request
  49. * @throws \Illuminate\Validation\ValidationException
  50. */
  51. public function setChargeConfig(Request $request) {
  52. $this->validate($request, [
  53. 'id' => 'required',
  54. 'chargeCoin' => 'required|integer|min:50|max:100',
  55. 'chargeType' => 'required|integer|in:1',
  56. 'chargeSequence' => 'required|integer|min:1|max:30'
  57. ]);
  58. $userContext = $this->getUserContext($request->input('operateUserId'));
  59. if(!(1 == $userContext['loginUser']->id ||
  60. $userContext['loginUserRoles']->diff(['administrator', 'optimizer'])->isNotEmpty())) {
  61. CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
  62. }
  63. $now = date('Y-m-d H:i:s');
  64. if($userContext['loginUserRoles']->contains('administrator') || 1 == $userContext['loginUser']->id) {
  65. DB::table('videos')
  66. ->where('id', $request->input('id'))
  67. ->update([
  68. 'd_charge_type' => $request->input('chargeType'),
  69. 'd_charge_sequence' => $request->input('chargeSequence'),
  70. 'd_charge_coin' => $request->input('chargeCoin'),
  71. 'updated_at' => $now
  72. ]);
  73. } else {
  74. DB::table('video_user_config')
  75. ->where([
  76. 'video_id' => $request->input('id'),
  77. 'uid' => $userContext['loginUser']->id
  78. ])->update(['is_enabled' => 0, 'updated_at' => $now]);
  79. DB::table('video_user_config')
  80. ->insert([
  81. 'uid' => $userContext['loginUser']->id,
  82. 'video_id' => $request->input('id'),
  83. 'charge_type' => $request->input('chargeType'),
  84. 'charge_sequence' => $request->input('chargeSequence'),
  85. 'charge_coin' => $request->input('chargeCoin'),
  86. 'updated_at' => $now,
  87. 'created_at' => $now
  88. ]);
  89. }
  90. }
  91. /**
  92. * 添加短剧
  93. * @param Request $request
  94. * @return int
  95. * @throws \Illuminate\Validation\ValidationException
  96. */
  97. public function add(Request $request) {
  98. $this->validate($request, [
  99. 'name' => 'required|string|max:128',
  100. 'total_episode_num' => 'required|integer',
  101. 'update_type' => 'required|integer|in:1,2',
  102. 'category_id' => 'required|integer',
  103. 'shelf_type' => 'required|integer|in:1,2',
  104. 'd_charge_sequence' => 'required|integer|min:1',
  105. 'd_charge_coin' => 'required|integer|min:1',
  106. 'cp_name' => 'required|string',
  107. 'cp_share_type' => 'required|integer|in:1,2,3',
  108. 'cover_image' => 'required|string'
  109. ]);
  110. $data = $request->all();
  111. $now = date('Y-m-d H:i:s');
  112. $data['created_at'] = $data['updated_at'] = $now;
  113. if(2 == $request->integer('shelf_type')) {
  114. $data['shelf_at'] = $now;
  115. } else {
  116. $data['shelf_at'] = null;
  117. }
  118. DB::table('videos')
  119. ->insert($data);
  120. return 1;
  121. }
  122. /**
  123. * 更新短剧信息
  124. * @param Request $request
  125. * @return int
  126. * @throws \Illuminate\Validation\ValidationException
  127. */
  128. public function update(Request $request) {
  129. $this->validate($request, [
  130. 'id' => 'required',
  131. 'name' => 'required|string|max:128',
  132. 'total_episode_num' => 'required|integer',
  133. 'update_type' => 'required|integer|in:1,2',
  134. 'category_id' => 'required|integer',
  135. 'shelf_type' => 'required|integer|in:1,2',
  136. 'd_charge_sequence' => 'required|integer|min:1',
  137. 'd_charge_coin' => 'required|integer|min:1',
  138. 'cp_name' => 'required|string',
  139. 'cp_share_type' => 'required|integer|in:1,2,3',
  140. 'cover_image' => 'required|string'
  141. ]);
  142. $id = $request->input('id');
  143. $data = $request->except('id', 'shelf_at');
  144. $data['updated_at'] = date('Y-m-d H:i:s');
  145. $video = VideoService::getVideoByIdOrException($request->input('id'));
  146. if(2 == $request->integer('shelf_type') && 1 == $video->shelf_type) {
  147. $data['shelf_at'] = $data['updated_at'];
  148. }
  149. DB::table('videos')
  150. ->where('id', $id)
  151. ->update($data);
  152. return 1;
  153. }
  154. private function updateVideoInfo($video, $userContext) {
  155. VideoService::updateVideoChargeInfo($video, $userContext);
  156. }
  157. private function getCategoryStr($allCategory,$categoryId) {
  158. $category = $allCategory->get($categoryId);
  159. if(!$category) {
  160. return '';
  161. } else {
  162. $firstLevelStr = $allCategory->get($category->pid)->category_name ?? '';
  163. $secondLevelStr = $category->category_name;
  164. return trim(join('/', compact('firstLevelStr','secondLevelStr')), '/');
  165. }
  166. }
  167. }