VideoController.php 7.6 KB

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