VideoController.php 7.2 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\Video\Services\VideoService;
  11. class VideoController extends CatchController
  12. {
  13. use ValidatesRequests;
  14. use UserTrait;
  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('limit', 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. $video->shelf_type_str = $video->shelf_type == 1 ? '未上架':'上架';
  44. $video->update_type_str = $video->update_type == 1 ? '连载中' : '完结';
  45. $video->cp_share_type_str = ([1 => '分成', 2=>'保底', 3=>'买断'])[$video->cp_share_type] ?? '';
  46. $video->channel = $allVideoCategory->get($video->category_id)->pid;
  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:300',
  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 || $userContext['loginUserRoles']->contains('administrator') ||
  64. $userContext['loginUserRoles']->contains('optimizer'))) {
  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. }