VideoController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $video->channel = $allVideoCategory->get($video->category_id)->pid;
  48. }
  49. return $videos;
  50. }
  51. /**
  52. * 订阅设置
  53. * @param Request $request
  54. * @throws \Illuminate\Validation\ValidationException
  55. */
  56. public function setChargeConfig(Request $request) {
  57. $this->validate($request, [
  58. 'id' => 'required',
  59. 'chargeCoin' => 'required|integer|min:50|max:300',
  60. 'chargeType' => 'required|integer|in:1',
  61. 'chargeSequence' => 'required|integer|min:1|max:30'
  62. ]);
  63. $userContext = $this->getUserContext($request->input('operateUserId'));
  64. if(!(1 == $userContext['loginUser']->id || $userContext['loginUserRoles']->contains('administrator') ||
  65. $userContext['loginUserRoles']->contains('optimizer'))) {
  66. CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
  67. }
  68. $now = date('Y-m-d H:i:s');
  69. if($userContext['loginUserRoles']->contains('administrator') || 1 == $userContext['loginUser']->id) {
  70. DB::table('videos')
  71. ->where('id', $request->input('id'))
  72. ->update([
  73. 'd_charge_type' => $request->input('chargeType'),
  74. 'd_charge_sequence' => $request->input('chargeSequence'),
  75. 'd_charge_coin' => $request->input('chargeCoin'),
  76. 'updated_at' => $now
  77. ]);
  78. } else {
  79. DB::table('video_user_config')
  80. ->where([
  81. 'video_id' => $request->input('id'),
  82. 'uid' => $userContext['loginUser']->id
  83. ])->update(['is_enabled' => 0, 'updated_at' => $now]);
  84. DB::table('video_user_config')
  85. ->insert([
  86. 'uid' => $userContext['loginUser']->id,
  87. 'video_id' => $request->input('id'),
  88. 'charge_type' => $request->input('chargeType'),
  89. 'charge_sequence' => $request->input('chargeSequence'),
  90. 'charge_coin' => $request->input('chargeCoin'),
  91. 'updated_at' => $now,
  92. 'created_at' => $now
  93. ]);
  94. }
  95. }
  96. /**
  97. * 添加短剧
  98. * @param Request $request
  99. * @return int
  100. * @throws \Illuminate\Validation\ValidationException
  101. */
  102. public function add(Request $request) {
  103. $this->validate($request, [
  104. 'name' => 'required|string|max:128',
  105. 'total_episode_num' => 'required|integer',
  106. 'update_type' => 'required|integer|in:1,2',
  107. 'category_id' => 'required|integer',
  108. 'shelf_type' => 'required|integer|in:1,2',
  109. 'd_charge_sequence' => 'required|integer|min:1',
  110. 'd_charge_coin' => 'required|integer|min:1',
  111. 'cp_name' => 'required|string',
  112. 'cp_share_type' => 'required|integer|in:1,2,3',
  113. 'cover_image' => 'required|string'
  114. ]);
  115. $data = $request->all();
  116. $now = date('Y-m-d H:i:s');
  117. $data['created_at'] = $data['updated_at'] = $now;
  118. if(2 == $request->integer('shelf_type')) {
  119. $data['shelf_at'] = $now;
  120. } else {
  121. $data['shelf_at'] = null;
  122. }
  123. DB::table('videos')
  124. ->insert($data);
  125. return 1;
  126. }
  127. /**
  128. * 更新短剧信息
  129. * @param Request $request
  130. * @return int
  131. * @throws \Illuminate\Validation\ValidationException
  132. */
  133. public function update(Request $request) {
  134. $this->validate($request, [
  135. 'id' => 'required',
  136. 'name' => 'required|string|max:128',
  137. 'total_episode_num' => 'required|integer',
  138. 'update_type' => 'required|integer|in:1,2',
  139. 'category_id' => 'required|integer',
  140. 'shelf_type' => 'required|integer|in:1,2',
  141. 'd_charge_sequence' => 'required|integer|min:1',
  142. 'd_charge_coin' => 'required|integer|min:1',
  143. 'cp_name' => 'required|string',
  144. 'cp_share_type' => 'required|integer|in:1,2,3',
  145. 'cover_image' => 'required|string'
  146. ]);
  147. $id = $request->input('id');
  148. $data = $request->except('id', 'shelf_at');
  149. $data['updated_at'] = date('Y-m-d H:i:s');
  150. $video = VideoService::getVideoByIdOrException($request->input('id'));
  151. if(2 == $request->integer('shelf_type') && 1 == $video->shelf_type) {
  152. $data['shelf_at'] = $data['updated_at'];
  153. }
  154. DB::table('videos')
  155. ->where('id', $id)
  156. ->update($data);
  157. return 1;
  158. }
  159. private function updateVideoInfo($video, $userContext) {
  160. VideoService::updateVideoChargeInfo($video, $userContext);
  161. }
  162. private function getCategoryStr($allCategory,$categoryId) {
  163. $category = $allCategory->get($categoryId);
  164. if(!$category) {
  165. return '';
  166. } else {
  167. $firstLevelStr = $allCategory->get($category->pid)->category_name ?? '';
  168. $secondLevelStr = $category->category_name;
  169. return trim(join('/', compact('firstLevelStr','secondLevelStr')), '/');
  170. }
  171. }
  172. }