VideoController.php 7.9 KB

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