VideoController.php 7.8 KB

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