diff --git a/app/Http/Controllers/ExamController.php b/app/Http/Controllers/ExamController.php index 1623a0a2..4cb2f032 100644 --- a/app/Http/Controllers/ExamController.php +++ b/app/Http/Controllers/ExamController.php @@ -95,4 +95,10 @@ class ExamController extends Controller // } + public function indexes() + { + $result = $this->repository->listIndexes(); + return $this->success($result); + } + } diff --git a/app/Models/Exam.php b/app/Models/Exam.php index 9c0f6989..b882ea51 100644 --- a/app/Models/Exam.php +++ b/app/Models/Exam.php @@ -3,11 +3,38 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; class Exam extends NexusModel { use HasFactory; protected $fillable = ['name', 'description', 'begin', 'end', 'status']; + + const STATUS_ENABLED = 0; + const STATUS_DISABLED = 1; + + public static $status = [ + self::STATUS_ENABLED => ['text' => '启用中'], + self::STATUS_DISABLED => ['text' => '已禁用'], + ]; + + const INDEX_UPLOADED = 1; + const INDEX_SEED_TIME = 2; + const INDEX_DOWNLOADED = 3; + const INDEX_LEECH_TIME = 4; + const INDEX_BONUS = 5; + + public static $indexes = [ + self::INDEX_UPLOADED => ['text' => 'Uploaded'], + self::INDEX_SEED_TIME => ['text' => 'Seed time'], + self::INDEX_DOWNLOADED => ['text' => 'Download'], + self::INDEX_LEECH_TIME => ['text' => 'Leech time'], + self::INDEX_BONUS => ['text' => 'Bonus'], + ]; + + public function getStatusTextAttribute() + { + return self::$status[$this->status]['text'] ?? ''; + } + } diff --git a/app/Models/User.php b/app/Models/User.php index c470e496..f57275ee 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -78,7 +78,7 @@ class User extends Authenticatable * * @var array */ - protected $fillable = ['username', 'email', 'password', 'secret', 'editsecret', 'added']; + protected $fillable = ['username', 'email', 'passhash', 'secret', 'editsecret', 'added']; /** * The attributes that should be hidden for arrays. @@ -96,6 +96,6 @@ class User extends Authenticatable * @var array */ protected $casts = [ - 'email_verified_at' => 'datetime', + ]; } diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index 0f835846..3aaac70d 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -4,6 +4,8 @@ namespace App\Repositories; use App\Models\Exam; use App\Models\Setting; use App\Models\User; +use Carbon\Carbon; +use Illuminate\Support\Facades\Log; class ExamRepository extends BaseRepository { @@ -28,4 +30,45 @@ class ExamRepository extends BaseRepository return $exam; } + public function listIndexes() + { + $out = []; + foreach(Exam::$indexes as $key => $value) { + $out[$key] = $value['text']; + } + return $out; + } + + public function listMatchExam($uid) + { + $now = Carbon::now(); + $user = User::query()->findOrFail($uid, ['id', 'username', 'added', 'class']); + $exams = Exam::query() + ->where('begin', '<=', $now) + ->where('end', '>=', $now) + ->where('status', Exam::STATUS_ENABLED) + ->get(); + $result = []; + $logPrefix = "uid: $uid"; + foreach ($exams as $exam) { + $filters = $exam->filters; + if (!in_array($user->class, $filters['classes'])) { + Log::info("$logPrefix, class: {$user->class} not in: " . json_encode($filters)); + continue; + } + $added = $user->added->toDateTimeString(); + if (!empty($filters['register_time_begin']) && $added < $filters['register_time_begin']) { + Log::info("$logPrefix, added: $added not after: " . $filters['register_time_begin']); + continue; + } + if (!empty($filters['register_time_end']) && $added > $filters['register_time_end']) { + Log::info("$logPrefix, added: $added not before: " . $filters['register_time_end']); + continue; + } + $result[] = $exam; + } + return $result; + } + + } diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 72bb852f..f81d3b53 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -440,7 +440,6 @@ function api(...$args) $msg = $args[1]; $data = $args[2]; } - if (defined('LARAVEL_START')) { $start = LARAVEL_START; if ($data instanceof \Illuminate\Http\Resources\Json\ResourceCollection || $data instanceof \Illuminate\Http\Resources\Json\JsonResource) { diff --git a/routes/api.php b/routes/api.php index 634d6e67..086777bc 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,5 +18,6 @@ Route::group([], function () { Route::resource('agent-allow', \App\Http\Controllers\AgentAllowController::class); Route::resource('user', \App\Http\Controllers\UserController::class); Route::resource('exam', \App\Http\Controllers\ExamController::class); + Route::get('exam-index', [\App\Http\Controllers\ExamController::class, 'indexes']); Route::get('class', [\App\Http\Controllers\UserController::class, 'classes']); });