Browse Source

4s用户信息缓存在点击4s按钮时生成

kely 6 months ago
parent
commit
6ab44931a2

+ 1 - 15
app/Http/Controllers/API/AuthController.php

@@ -41,15 +41,6 @@ class AuthController extends Controller
 
         // 创建token
         $token = $user->createToken('user')->plainTextToken;
-        //用户信息放入缓存
-        $cacheKey = 'auth_token'.$token;
-        Cache::put($cacheKey, [
-            'user_id' => $user->id,
-            'username' => $user->username,
-            'email' => $user->email,
-            'phone' => $user->phone,
-     //       'token' => $token,
-        ], now()->addHours(2));
 
         return $this->success([
             'data' => [
@@ -61,13 +52,8 @@ class AuthController extends Controller
 
     public function logout(Request $request)
     {
-        //删除缓存token
-        Auth::user()->currentAccessToken()->delete();
-        $token = $request->headers->get('Authorization');
-        // 去除Bearer前缀(如果有的话)
-        $token = Str::startsWith($token, 'Bearer ') ? Str::substr($token, 7) : $token;
 
-        Cache::delete('auth_token' . $token);
+        Auth::user()->currentAccessToken()->delete();
 
         return $this->noContent();
     }

+ 13 - 5
app/Http/Controllers/API/ProjectController.php

@@ -49,6 +49,7 @@ use App\Services\Project\ProjectTaskGroupViewService;
 use Carbon\Carbon;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Crypt;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Str;
@@ -655,12 +656,19 @@ class ProjectController extends Controller
     }
 
     public function project4s(Request $request){
-        $token = $request->headers->get('Authorization');
-        // 去除Bearer前缀(如果有的话)
-        $token = Str::startsWith($token, 'Bearer ') ? Str::substr($token, 7) : $token;
-        $encryptedData = Crypt::encrypt('auth_token'.$token);
+        $key=uniqid();
+        $user=Auth::user();
+        //用户信息放入缓存
+        Cache::put($key, [
+            'user_id' => $user->id,
+            'username' => $user->username,
+            'email' => $user->email,
+            'phone' => $user->phone,
+            //       'token' => $token,
+        ], now()->addHours(2));
+
         return $this->success([
-            'data' => $encryptedData
+            'data' => $key
         ]);
     }
 }

+ 2 - 13
app/Http/Controllers/API/UserController.php

@@ -202,19 +202,8 @@ class UserController extends Controller
     }
 
     public function auth4sToken(Request $request){
-        $tokenKey=$request->get('key');
-        //解密加密token
-        $user=null;
-        try{
-            $decryptedData = Crypt::decrypt($tokenKey);
-            $user=Cache::get($decryptedData);
-            if(!empty($user)){
-                $user=$user;
-            }
-        }catch (DecryptException $e){
-            $decryptedData = null;
-        }
-
+        $key=$request->get('key');
+        $user=Cache::get($key);
         return $this->success([
            'data'=>$user
         ]);