VideoController.php 6.9 KB

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