VideoController.php 7.1 KB

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