From c43eec2354ce832b85b60efe711d20ee03af24b1 Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Mon, 19 Oct 2026 10:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9B=E9=9C=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Middleware/SupplyDemandAccess.php | 5 +++-- tests/Feature/SupplyDemandHandoverTest.php | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/Http/Middleware/SupplyDemandAccess.php b/app/Http/Middleware/SupplyDemandAccess.php index 03697e7..678e5dd 100644 --- a/app/Http/Middleware/SupplyDemandAccess.php +++ b/app/Http/Middleware/SupplyDemandAccess.php @@ -8,14 +8,15 @@ class SupplyDemandAccess { use ApiResponse; public static function registered($user): bool { - return $user && trim((string) $user->name) !== '' && preg_match('/^1\d{10}$/', (string) $user->getRawOriginal('mobile')); + return $user && preg_match('/^1\d{10}$/', (string) $user->getRawOriginal('mobile')); } private function tokenUser($bearer) { if (!is_string($bearer) || $bearer === '') return null; $token = PersonalAccessToken::findToken($bearer); if (!$token || !($token->tokenable instanceof User)) return null; if ($token->expires_at && !\Illuminate\Support\Carbon::parse($token->expires_at)->isFuture()) return null; - if (config('sanctum.expiration') && !$token->created_at->gt(now()->subMinutes(config('sanctum.expiration')))) return null; + // Match the existing mobile login lifecycle; token age alone does not log out a member. + // Revoked tokens and tokens with an explicit expiry remain invalid. return $token->tokenable; } public function handle($request, Closure $next, $mode = 'member') { diff --git a/tests/Feature/SupplyDemandHandoverTest.php b/tests/Feature/SupplyDemandHandoverTest.php index a3defc4..40ae3a2 100644 --- a/tests/Feature/SupplyDemandHandoverTest.php +++ b/tests/Feature/SupplyDemandHandoverTest.php @@ -230,6 +230,15 @@ class SupplyDemandHandoverTest extends TestCase $this->withToken('invalid')->getJson('/api/mobile/supply-demand/detail?id='.$d->id)->assertJsonPath('errcode',10002); } + public function test_existing_mobile_account_with_older_token_does_not_need_login_again() { + $this->enableGuestRequests();config(['sanctum.expiration'=>1440]); + $user=User::find(2);$user->name=null;$user->save(); + $token=$user->createToken('mobile-token')->plainTextToken; + DB::table('personal_access_tokens')->update(['created_at'=>now()->subDays(7)]); + $this->withToken($token)->getJson('/api/mobile/supply-demand/session')->assertJsonPath('registered',true)->assertJsonPath('user.id',2); + $this->withToken($token)->getJson('/api/mobile/supply-demand/unread-count')->assertJsonMissing(['errcode'=>40001])->assertJsonMissing(['errcode'=>10003]); + } + public function test_expired_real_token_can_browse_but_cannot_send() { $d=$this->demand();$this->enableGuestRequests();$token=User::find(2)->createToken('mobile-token')->plainTextToken; DB::table('personal_access_tokens')->update(['expires_at'=>now()->subMinute()]);