You've already forked lubo_comment_query
							
							新款导入节目单,自动匹配时间(需提前导入分P信息)
This commit is contained in:
		| @@ -5,6 +5,9 @@ namespace App\Http\Controllers; | ||||
| use App\Models\Programs; | ||||
| use App\Models\ProgramVideos; | ||||
| use App\Models\VideoComments; | ||||
| use App\Models\VideoParts; | ||||
| use App\Util\TimeUtil; | ||||
| use Carbon\Carbon; | ||||
| use Illuminate\Database\QueryException; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Routing\Controller as BaseController; | ||||
| @@ -12,6 +15,7 @@ use Illuminate\Support\Facades\DB; | ||||
| 
 | ||||
| class ProgramConstructController extends BaseController | ||||
| { | ||||
|     // region Views
 | ||||
|     public function index(Request $request) { | ||||
|         $status = $request->get("status", "0"); | ||||
|         $query = Programs::query()->with(["appends", "video_pivots.video"])->where("status", $status, 0)->orderByDesc("created_at"); | ||||
| @@ -27,6 +31,25 @@ class ProgramConstructController extends BaseController | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     public function from_comment(Request $request, VideoComments $comment) { | ||||
|         return redirect(route("program.construct.batch_add"))->withInput([ | ||||
|             "bvid"=>$comment->video->bvid, | ||||
|             "content"=>$comment->content | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     public function batch_add(Request $request) { | ||||
|         return view("program.construct.batch_add"); | ||||
|     } | ||||
| 
 | ||||
|     public function edit(Request $request, Programs $program) { | ||||
|         return view("program.construct.create", [ | ||||
|             "program"=>$program | ||||
|         ]); | ||||
|     } | ||||
|     // endregion
 | ||||
| 
 | ||||
|     // region Form Submit
 | ||||
|     public function create(Request $request) { | ||||
|         $request->validate([ | ||||
|             "name" => ["required"] | ||||
| @@ -39,17 +62,6 @@ class ProgramConstructController extends BaseController | ||||
|         return redirect(route("program.construct.edit", ["program"=>$program->id])); | ||||
|     } | ||||
| 
 | ||||
|     public function from_comment(Request $request, VideoComments $comment) { | ||||
|         return redirect(route("program.construct.batch_add"))->withInput([ | ||||
|             "bvid"=>$comment->video->bvid, | ||||
|             "content"=>$comment->content | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     public function batch_add(Request $request) { | ||||
|         return view("program.construct.batch_add"); | ||||
|     } | ||||
| 
 | ||||
|     public function batch_create(Request $request) { | ||||
|         $request->validate([ | ||||
|             'bvid' => ['required'], | ||||
| @@ -63,7 +75,8 @@ class ProgramConstructController extends BaseController | ||||
|                 "bvid" => "该BVID下已有{$count}个节目关联,请手动添加" | ||||
|             ]); | ||||
|         } | ||||
|         $regex = "/^p(?P<part>\d{1,2})[-# _:,)]+(?P<time>(0?1[::])?\d{1,3}[::]\d{1,2}) ?(?P<content>.+)$/ui"; | ||||
|         $regex = "/^p(?P<part>\d{1,2})[-# _:,)]+(?P<time>(0?1[::])?\d{1,3}[::]\d{1,2})\s*(?P<content>.+)$/ui"; | ||||
|         $video_parts = VideoParts::query()->where("bvid", "=", $bvid)->get(); | ||||
|         DB::beginTransaction(); | ||||
|         try { | ||||
|             foreach (explode("\n", $content) as $line) { | ||||
| @@ -84,6 +97,30 @@ class ProgramConstructController extends BaseController | ||||
|                 $video_pivot->start_part = $match["part"]; | ||||
|                 $video_pivot->start_time = $time; | ||||
|                 $video_pivot->stop_part = $match["part"]; | ||||
|                 if (sizeof($video_parts) > 0) { | ||||
|                     /** | ||||
|                      * 计算开始时间 | ||||
|                      * @var VideoParts $cur_part | ||||
|                      */ | ||||
|                     $cur_part = $video_parts->where("part_num", "=", $match["part"])->first(); | ||||
|                     if ($cur_part) { | ||||
|                         // 根据标题名称,判断开始时间
 | ||||
|                         $title = $cur_part->title; | ||||
|                         $date_str = substr($title, 0, 13); | ||||
|                         $base_time = Carbon::createFromFormat("Ymd_Hi", $date_str); | ||||
|                         $start_time = Carbon::createFromFormat("H:i:s", $time); | ||||
|                         $video_pivot->created_at = TimeUtil::calculate_program_time( | ||||
|                             $base_time, | ||||
|                             $start_time->secondsSinceMidnight() | ||||
|                         ); | ||||
|                         if ($cur_part->duration->diffInMinutes($start_time) < 20) { | ||||
|                             $video_pivot->stop_part += 1; | ||||
|                             if ($video_pivot->stop_part > $video_parts->pluck("part_num")->max()) { | ||||
|                                 $video_pivot->stop_part = $video_parts->pluck("part_num")->max(); | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|                 $program->save(); | ||||
|                 $program->video_pivots()->save($video_pivot); | ||||
|             } | ||||
| @@ -97,12 +134,6 @@ class ProgramConstructController extends BaseController | ||||
|         return redirect(route("program.construct.list")); | ||||
|     } | ||||
| 
 | ||||
|     public function edit(Request $request, Programs $program) { | ||||
|         return view("program.construct.create", [ | ||||
|             "program"=>$program | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     public function submit(Request $request, Programs $program) { | ||||
|         $request->validate([ | ||||
|             "name" => ["required"] | ||||
| @@ -114,4 +145,5 @@ class ProgramConstructController extends BaseController | ||||
|             "program"=>$program | ||||
|         ]); | ||||
|     } | ||||
|     // endregion
 | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,7 @@ namespace App\Http\Controllers; | ||||
| 
 | ||||
| use App\Models\Programs; | ||||
| use App\Models\ProgramVideos; | ||||
| use App\Util\TimeUtil; | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Routing\Controller as BaseController; | ||||
| @@ -134,21 +135,17 @@ class ProgramVideoConstructController extends BaseController | ||||
|                 $query->where("created_at", ">", $program_video->created_at)->orWhere("created_at", "=", null); | ||||
|             }) | ||||
|             ->get(); | ||||
|         /** | ||||
|          * @var $created_at Carbon | ||||
|          */ | ||||
|         $created_at = $program_video->created_at; | ||||
|         $base_time = $created_at->subSeconds($program_video->start_sec)->subSeconds(($program_video->start_part - 1) * $each_time_sec); | ||||
|         foreach ($program_videos as $video) { | ||||
|             if ($video->id === $program_video->id) { | ||||
|                 continue; | ||||
|             } | ||||
|             $time = $base_time->copy()->addSeconds(($video->start_part - 1) * $each_time_sec)->addSeconds($video->start_sec); | ||||
|             if ($time->second > 30) { | ||||
|                 $time->addMinute(); | ||||
|             } | ||||
|             $time->seconds(0); | ||||
|             $video->created_at = $time; | ||||
|             $video->created_at = TimeUtil::calculate_program_time( | ||||
|                 $base_time, | ||||
|                 $video->start_sec, | ||||
|                 ($video->start_part - 1) * $each_time_sec | ||||
|             ); | ||||
|             $video->update(); | ||||
|         } | ||||
|         return redirect(route("program.construct.list")); | ||||
|   | ||||
| @@ -10,11 +10,27 @@ use Illuminate\Support\Facades\Hash; | ||||
| 
 | ||||
| class UserController extends BaseController | ||||
| { | ||||
|     // region Views
 | ||||
|     public function login_page(Request $request) | ||||
|     { | ||||
|         return view("user.login"); | ||||
|     } | ||||
| 
 | ||||
|     public function logout(Request $request) | ||||
|     { | ||||
|         Auth::logout(); | ||||
|         $request->session()->invalidate(); | ||||
|         $request->session()->regenerateToken(); | ||||
|         return redirect("/"); | ||||
|     } | ||||
| 
 | ||||
|     public function register_page(Request $request) | ||||
|     { | ||||
|         return view("user.register"); | ||||
|     } | ||||
|     // endregion
 | ||||
| 
 | ||||
|     // region Form Submit
 | ||||
|     public function authenticate(Request $request): \Illuminate\Http\RedirectResponse | ||||
|     { | ||||
|         $request->validate([ | ||||
| @@ -38,19 +54,6 @@ class UserController extends BaseController | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     public function logout(Request $request) | ||||
|     { | ||||
|         Auth::logout(); | ||||
|         $request->session()->invalidate(); | ||||
|         $request->session()->regenerateToken(); | ||||
|         return redirect("/"); | ||||
|     } | ||||
| 
 | ||||
|     public function register_page(Request $request) | ||||
|     { | ||||
|         return view("user.register"); | ||||
|     } | ||||
| 
 | ||||
|     public function register(Request $request) | ||||
|     { | ||||
|         $request_payload = $request->validate([ | ||||
| @@ -64,4 +67,5 @@ class UserController extends BaseController | ||||
|         $user->save(); | ||||
|         return redirect(route("login")); | ||||
|     } | ||||
|     // endregion
 | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,13 @@ use Illuminate\Database\Eloquent\Casts\Attribute; | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| use Illuminate\Support\Carbon; | ||||
| 
 | ||||
| /** | ||||
|  * @property Carbon $created_at | ||||
|  * @property int $stop_part | ||||
|  * @property int $start_part | ||||
|  * @property int $startSec | ||||
|  * @property int $stopSec | ||||
|  */ | ||||
| class ProgramVideos extends Model | ||||
| { | ||||
|     protected $fillable = ["video_bvid", "start_part", "start_time", "stop_part", "stop_time"]; | ||||
|   | ||||
| @@ -4,9 +4,15 @@ namespace App\Models; | ||||
| 
 | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| 
 | ||||
| /** | ||||
|  * @property \Carbon\Carbon $duration | ||||
|  */ | ||||
| class VideoParts extends Model | ||||
| { | ||||
|     public $timestamps = false; | ||||
|     protected $casts = [ | ||||
|         "duration" => "datetime", | ||||
|     ]; | ||||
|     public function video(): \Illuminate\Database\Eloquent\Relations\BelongsTo | ||||
|     { | ||||
|         return $this->belongsTo(Videos::class, "bvid", "bvid"); | ||||
|   | ||||
							
								
								
									
										19
									
								
								app/Util/TimeUtil.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								app/Util/TimeUtil.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Util; | ||||
| 
 | ||||
| 
 | ||||
| use Carbon\Carbon; | ||||
| 
 | ||||
| class TimeUtil | ||||
| { | ||||
|     public static function calculate_program_time(Carbon $base_time, int $seconds, int $bias = 0): Carbon | ||||
|     { | ||||
|         $time = $base_time->copy()->addSeconds($seconds)->addSeconds($bias); | ||||
|         if ($time->second > 30) { | ||||
|             $time->addMinute(); | ||||
|         } | ||||
|         $time->seconds(0); | ||||
|         return $time; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user