|
@@ -0,0 +1,226 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Modules\Video\Http\Controllers;
|
|
|
+
|
|
|
+use Catch\Base\CatchController;
|
|
|
+use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Modules\Common\Errors\Errors;
|
|
|
+use Modules\Common\Exceptions\CommonBusinessException;
|
|
|
+use Modules\User\Models\User;
|
|
|
+
|
|
|
+class VideoController extends CatchController
|
|
|
+{
|
|
|
+ use UserTrait;
|
|
|
+ use ValidatesRequests;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 短剧列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
+ */
|
|
|
+ public function list(Request $request) {
|
|
|
+
|
|
|
+ $videoName = $request->input('videoName');
|
|
|
+ $updateType = $request->input('updateType');
|
|
|
+ $categoryId = $request->input('categoryId');
|
|
|
+
|
|
|
+ $videos = DB::table('videos')
|
|
|
+ ->when($videoName, function ($query, $videoName){
|
|
|
+ return $query->where('name', 'like', '%'. $videoName . '%');
|
|
|
+ })->when($updateType, function ($query, $updateType){
|
|
|
+ return $query->where('update_type', $updateType);
|
|
|
+ })->when($categoryId, function ($query, $categoryId){
|
|
|
+ return $query->where('category_id', $categoryId);
|
|
|
+ })->orderBy('id', 'desc')
|
|
|
+ ->paginate($request->integer('per_page', 15));
|
|
|
+ $userContext = $this->getUserContext($request);
|
|
|
+ $allVideoCategory = DB::table('video_category')
|
|
|
+ ->get()->keyBy('id');
|
|
|
+ foreach ($videos as $video) {
|
|
|
+ $this->updateVideoInfo($video, $userContext);
|
|
|
+ $video->category_str = $this->getCategoryStr($allVideoCategory, $video->category_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $videos;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订阅设置
|
|
|
+ * @param Request $request
|
|
|
+ * @throws \Illuminate\Validation\ValidationException
|
|
|
+ */
|
|
|
+ public function setChargeConfig(Request $request) {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'id' => 'required',
|
|
|
+ 'chargeCoin' => 'required|integer|min:50|max:100',
|
|
|
+ 'chargeType' => 'required|integer|in:1',
|
|
|
+ 'chargeSequence' => 'required|integer|min:1|max:30'
|
|
|
+ ]);
|
|
|
+ $userContext = $this->getUserContext($request);
|
|
|
+ if($userContext['loginUserRoles']->diff(['administrator', 'optimizer'])->isEmpty()) {
|
|
|
+ CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
|
|
|
+ }
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+ if($userContext['loginUserRoles']->contains('administrator')) {
|
|
|
+ DB::table('videos')
|
|
|
+ ->where('id', $request->input('id'))
|
|
|
+ ->update([
|
|
|
+ 'd_charge_type' => $request->input('chargeType'),
|
|
|
+ 'd_charge_sequence' => $request->input('chargeSequence'),
|
|
|
+ 'd_charge_coin' => $request->input('chargeCoin'),
|
|
|
+ 'updated_at' => $now
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ DB::table('video_user_config')
|
|
|
+ ->where([
|
|
|
+ 'video_id' => $request->input('id'),
|
|
|
+ 'uid' => $userContext['loginUser']->id
|
|
|
+ ])->update(['is_enabled' => 0, 'updated_at' => $now]);
|
|
|
+ DB::table('video_user_config')
|
|
|
+ ->insert([
|
|
|
+ 'uid' => $userContext['loginUser']->id,
|
|
|
+ 'video_id' => $request->input('id'),
|
|
|
+ 'charge_type' => $request->input('chargeType'),
|
|
|
+ 'charge_sequence' => $request->input('chargeSequence'),
|
|
|
+ 'charge_coin' => $request->input('chargeCoin'),
|
|
|
+ 'updated_at' => $now,
|
|
|
+ 'created_at' => $now
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加短剧
|
|
|
+ * @param Request $request
|
|
|
+ * @return int
|
|
|
+ * @throws \Illuminate\Validation\ValidationException
|
|
|
+ */
|
|
|
+ public function add(Request $request) {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'name' => 'required|string|max:128',
|
|
|
+ 'total_episode_num' => 'required|integer',
|
|
|
+ 'update_type' => 'required|integer|in:1,2',
|
|
|
+ 'category_id' => 'required|integer',
|
|
|
+ 'shelf_type' => 'required|integer|in:1,2',
|
|
|
+ 'd_charge_sequence' => 'required|integer|min:1',
|
|
|
+ 'd_charge_coin' => 'required|integer|min:1',
|
|
|
+ 'cp_name' => 'required|string',
|
|
|
+ 'cp_share_type' => 'required|integer|in:1,2,3',
|
|
|
+ 'cover_image' => 'required|string'
|
|
|
+ ]);
|
|
|
+ $data = $request->all();
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+ $data['created_at'] = $data['updated_at'] = $now;
|
|
|
+ if(2 == $request->integer('shelf_type')) {
|
|
|
+ $data['shelf_at'] = $now;
|
|
|
+ } else {
|
|
|
+ $data['shelf_at'] = null;
|
|
|
+ }
|
|
|
+ DB::table('videos')
|
|
|
+ ->insert($data);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新短剧信息
|
|
|
+ * @param Request $request
|
|
|
+ * @return int
|
|
|
+ * @throws \Illuminate\Validation\ValidationException
|
|
|
+ */
|
|
|
+ public function update(Request $request) {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'id' => 'required',
|
|
|
+ 'name' => 'required|string|max:128',
|
|
|
+ 'total_episode_num' => 'required|integer',
|
|
|
+ 'update_type' => 'required|integer|in:1,2',
|
|
|
+ 'category_id' => 'required|integer',
|
|
|
+ 'shelf_type' => 'required|integer|in:1,2',
|
|
|
+ 'd_charge_sequence' => 'required|integer|min:1',
|
|
|
+ 'd_charge_coin' => 'required|integer|min:1',
|
|
|
+ 'cp_name' => 'required|string',
|
|
|
+ 'cp_share_type' => 'required|integer|in:1,2,3',
|
|
|
+ 'cover_image' => 'required|string'
|
|
|
+ ]);
|
|
|
+ $id = $request->input('id');
|
|
|
+ $data = $request->except('id', 'shelf_at');
|
|
|
+ $data['updated_at'] = date('Y-m-d H:i:s');
|
|
|
+ $video = DB::table('videos')->where('id', $id)->first();
|
|
|
+ if(!$video) {
|
|
|
+ CommonBusinessException::throwError(Errors::VIDEO_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ if(2 == $request->integer('shelf_type') && 1 == $video->shelf_type) {
|
|
|
+ $data['shelf_at'] = $data['updated_at'];
|
|
|
+ }
|
|
|
+ DB::table('videos')
|
|
|
+ ->where('id', $id)
|
|
|
+ ->update($data);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private function getUserContext(Request $request) {
|
|
|
+ $loginUser = $this->getLoginUser();
|
|
|
+ $loginUserRoles = $this->listUserRoles();
|
|
|
+ $operateUserId = $request->input('operateUserId');
|
|
|
+ if($operateUserId) {
|
|
|
+ $operateUser = User::find($operateUserId);
|
|
|
+ $operateUserRoles = $operateUser->roles->pluck('identify');
|
|
|
+ if($loginUser->id != $operateUser->pid) {
|
|
|
+ CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
|
|
|
+ }
|
|
|
+ if(!$operateUserRoles->contains('optimizer')) {
|
|
|
+ CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $operateUser = $loginUser;
|
|
|
+ $operateUserRoles = $loginUserRoles;
|
|
|
+ }
|
|
|
+
|
|
|
+ return compact('loginUser', 'loginUserRoles', 'operateUserRoles', 'operateUser');
|
|
|
+ }
|
|
|
+ private function updateVideoInfo($video, $userContext) {
|
|
|
+ if($userContext['loginUserRoles']->contains('administrator')) {
|
|
|
+ $video->charge_sequence = $video->d_charge_sequence;
|
|
|
+ $video->charge_coin = $video->d_charge_coin;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if($userContext['loginUserRoles']->contains('company')) {
|
|
|
+ if($userContext['loginUser']->id == $userContext['operateUser']->id) {
|
|
|
+ $video->charge_sequence = $video->d_charge_sequence;
|
|
|
+ $video->charge_coin = $video->d_charge_coin;
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ $videoUserConfig = $this->getVideoUserConfig($userContext['operateUser']->id, $video->id);
|
|
|
+ $video->charge_sequence = $videoUserConfig->charge_sequence ?? $video->d_charge_sequence;
|
|
|
+ $video->charge_coin = $videoUserConfig->charge_coin ?? $video->d_charge_coin;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if($userContext['loginUserRoles']->contains('optimizer')) {
|
|
|
+ $videoUserConfig = $this->getVideoUserConfig($userContext['loginUser']->id, $video->id);
|
|
|
+ $video->charge_sequence = $videoUserConfig->charge_sequence ?? $video->d_charge_sequence;
|
|
|
+ $video->charge_coin = $videoUserConfig->charge_coin ?? $video->d_charge_coin;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getVideoUserConfig($uid, $videoId) {
|
|
|
+ return DB::table('video_user_config')
|
|
|
+ ->where(['is_enabled' => 1, 'uid' => $uid, 'video_id' => $videoId])->first();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getCategoryStr($allCategory,$categoryId) {
|
|
|
+ $category = $allCategory->get($categoryId);
|
|
|
+ if(!$category) {
|
|
|
+ return '';
|
|
|
+ } else {
|
|
|
+ $firstLevelStr = $allCategory->get($category->pid)->category_name ?? '';
|
|
|
+ $secondLevelStr = $category->category_name;
|
|
|
+ return trim(join('/', compact('firstLevelStr','secondLevelStr')), '/');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|