VideoController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\Models\User;
  10. class VideoController extends CatchController
  11. {
  12. use UserTrait;
  13. use ValidatesRequests;
  14. /**
  15. * 短剧列表
  16. * @param Request $request
  17. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  18. */
  19. public function list(Request $request) {
  20. $videoName = $request->input('videoName');
  21. $updateType = $request->input('updateType');
  22. $categoryId = $request->input('categoryId');
  23. $videos = DB::table('videos')
  24. ->when($videoName, function ($query, $videoName){
  25. return $query->where('name', 'like', '%'. $videoName . '%');
  26. })->when($updateType, function ($query, $updateType){
  27. return $query->where('update_type', $updateType);
  28. })->when($categoryId, function ($query, $categoryId){
  29. return $query->where('category_id', $categoryId);
  30. })->orderBy('id', 'desc')
  31. ->paginate($request->integer('per_page', 15));
  32. $userContext = $this->getUserContext($request);
  33. $allVideoCategory = DB::table('video_category')
  34. ->get()->keyBy('id');
  35. foreach ($videos as $video) {
  36. $this->updateVideoInfo($video, $userContext);
  37. $video->category_str = $this->getCategoryStr($allVideoCategory, $video->category_id);
  38. }
  39. return $videos;
  40. }
  41. /**
  42. * 订阅设置
  43. * @param Request $request
  44. * @throws \Illuminate\Validation\ValidationException
  45. */
  46. public function setChargeConfig(Request $request) {
  47. $this->validate($request, [
  48. 'id' => 'required',
  49. 'chargeCoin' => 'required|integer|min:50|max:100',
  50. 'chargeType' => 'required|integer|in:1',
  51. 'chargeSequence' => 'required|integer|min:1|max:30'
  52. ]);
  53. $userContext = $this->getUserContext($request);
  54. if($userContext['loginUserRoles']->diff(['administrator', 'optimizer'])->isEmpty()) {
  55. CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
  56. }
  57. $now = date('Y-m-d H:i:s');
  58. if($userContext['loginUserRoles']->contains('administrator')) {
  59. DB::table('videos')
  60. ->where('id', $request->input('id'))
  61. ->update([
  62. 'd_charge_type' => $request->input('chargeType'),
  63. 'd_charge_sequence' => $request->input('chargeSequence'),
  64. 'd_charge_coin' => $request->input('chargeCoin'),
  65. 'updated_at' => $now
  66. ]);
  67. } else {
  68. DB::table('video_user_config')
  69. ->where([
  70. 'video_id' => $request->input('id'),
  71. 'uid' => $userContext['loginUser']->id
  72. ])->update(['is_enabled' => 0, 'updated_at' => $now]);
  73. DB::table('video_user_config')
  74. ->insert([
  75. 'uid' => $userContext['loginUser']->id,
  76. 'video_id' => $request->input('id'),
  77. 'charge_type' => $request->input('chargeType'),
  78. 'charge_sequence' => $request->input('chargeSequence'),
  79. 'charge_coin' => $request->input('chargeCoin'),
  80. 'updated_at' => $now,
  81. 'created_at' => $now
  82. ]);
  83. }
  84. }
  85. /**
  86. * 添加短剧
  87. * @param Request $request
  88. * @return int
  89. * @throws \Illuminate\Validation\ValidationException
  90. */
  91. public function add(Request $request) {
  92. $this->validate($request, [
  93. 'name' => 'required|string|max:128',
  94. 'total_episode_num' => 'required|integer',
  95. 'update_type' => 'required|integer|in:1,2',
  96. 'category_id' => 'required|integer',
  97. 'shelf_type' => 'required|integer|in:1,2',
  98. 'd_charge_sequence' => 'required|integer|min:1',
  99. 'd_charge_coin' => 'required|integer|min:1',
  100. 'cp_name' => 'required|string',
  101. 'cp_share_type' => 'required|integer|in:1,2,3',
  102. 'cover_image' => 'required|string'
  103. ]);
  104. $data = $request->all();
  105. $now = date('Y-m-d H:i:s');
  106. $data['created_at'] = $data['updated_at'] = $now;
  107. if(2 == $request->integer('shelf_type')) {
  108. $data['shelf_at'] = $now;
  109. } else {
  110. $data['shelf_at'] = null;
  111. }
  112. DB::table('videos')
  113. ->insert($data);
  114. return 1;
  115. }
  116. /**
  117. * 更新短剧信息
  118. * @param Request $request
  119. * @return int
  120. * @throws \Illuminate\Validation\ValidationException
  121. */
  122. public function update(Request $request) {
  123. $this->validate($request, [
  124. 'id' => 'required',
  125. 'name' => 'required|string|max:128',
  126. 'total_episode_num' => 'required|integer',
  127. 'update_type' => 'required|integer|in:1,2',
  128. 'category_id' => 'required|integer',
  129. 'shelf_type' => 'required|integer|in:1,2',
  130. 'd_charge_sequence' => 'required|integer|min:1',
  131. 'd_charge_coin' => 'required|integer|min:1',
  132. 'cp_name' => 'required|string',
  133. 'cp_share_type' => 'required|integer|in:1,2,3',
  134. 'cover_image' => 'required|string'
  135. ]);
  136. $id = $request->input('id');
  137. $data = $request->except('id', 'shelf_at');
  138. $data['updated_at'] = date('Y-m-d H:i:s');
  139. $video = DB::table('videos')->where('id', $id)->first();
  140. if(!$video) {
  141. CommonBusinessException::throwError(Errors::VIDEO_NOT_EXISTS);
  142. }
  143. if(2 == $request->integer('shelf_type') && 1 == $video->shelf_type) {
  144. $data['shelf_at'] = $data['updated_at'];
  145. }
  146. DB::table('videos')
  147. ->where('id', $id)
  148. ->update($data);
  149. return 1;
  150. }
  151. private function getUserContext(Request $request) {
  152. $loginUser = $this->getLoginUser();
  153. $loginUserRoles = $this->listUserRoles();
  154. $operateUserId = $request->input('operateUserId');
  155. if($operateUserId) {
  156. $operateUser = User::find($operateUserId);
  157. $operateUserRoles = $operateUser->roles->pluck('identify');
  158. if($loginUser->id != $operateUser->pid) {
  159. CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
  160. }
  161. if(!$operateUserRoles->contains('optimizer')) {
  162. CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
  163. }
  164. } else {
  165. $operateUser = $loginUser;
  166. $operateUserRoles = $loginUserRoles;
  167. }
  168. return compact('loginUser', 'loginUserRoles', 'operateUserRoles', 'operateUser');
  169. }
  170. private function updateVideoInfo($video, $userContext) {
  171. if($userContext['loginUserRoles']->contains('administrator')) {
  172. $video->charge_sequence = $video->d_charge_sequence;
  173. $video->charge_coin = $video->d_charge_coin;
  174. return;
  175. }
  176. if($userContext['loginUserRoles']->contains('company')) {
  177. if($userContext['loginUser']->id == $userContext['operateUser']->id) {
  178. $video->charge_sequence = $video->d_charge_sequence;
  179. $video->charge_coin = $video->d_charge_coin;
  180. return;
  181. } else {
  182. $videoUserConfig = $this->getVideoUserConfig($userContext['operateUser']->id, $video->id);
  183. $video->charge_sequence = $videoUserConfig->charge_sequence ?? $video->d_charge_sequence;
  184. $video->charge_coin = $videoUserConfig->charge_coin ?? $video->d_charge_coin;
  185. return;
  186. }
  187. }
  188. if($userContext['loginUserRoles']->contains('optimizer')) {
  189. $videoUserConfig = $this->getVideoUserConfig($userContext['loginUser']->id, $video->id);
  190. $video->charge_sequence = $videoUserConfig->charge_sequence ?? $video->d_charge_sequence;
  191. $video->charge_coin = $videoUserConfig->charge_coin ?? $video->d_charge_coin;
  192. return;
  193. }
  194. }
  195. private function getVideoUserConfig($uid, $videoId) {
  196. return DB::table('video_user_config')
  197. ->where(['is_enabled' => 1, 'uid' => $uid, 'video_id' => $videoId])->first();
  198. }
  199. private function getCategoryStr($allCategory,$categoryId) {
  200. $category = $allCategory->get($categoryId);
  201. if(!$category) {
  202. return '';
  203. } else {
  204. $firstLevelStr = $allCategory->get($category->pid)->category_name ?? '';
  205. $secondLevelStr = $category->category_name;
  206. return trim(join('/', compact('firstLevelStr','secondLevelStr')), '/');
  207. }
  208. }
  209. }