@@ -35,10 +35,14 @@ class JwtToken
3535 */
3636 private const REFRESH_TOKEN = 2 ;
3737
38+ public const TOKEN_CLIENT_WEB = 'WEB ' ;
39+
40+ public const TOKEN_CLIENT_MOBILE = 'MOBILE ' ;
41+
3842 /**
3943 * @desc: 获取当前登录ID
40- * @throws JwtTokenException
4144 * @return mixed
45+ * @throws JwtTokenException
4246 * @author Tinywan(ShaoBo Wan)
4347 */
4448 public static function getCurrentId ()
@@ -116,12 +120,8 @@ public static function refreshToken(): array
116120 $ newToken ['refresh_token ' ] = self ::makeToken ($ payload ['refreshPayload ' ], $ refreshSecretKey , $ config ['algorithms ' ]);
117121 }
118122 if ($ config ['is_single_device ' ]) {
119- RedisHandler::generateToken ([
120- 'id ' => $ extend ['extend ' ]['id ' ],
121- 'access_token ' => $ newToken ['access_token ' ],
122- 'cache_token_ttl ' => $ extend ['exp ' ],
123- 'cache_token_pre ' => $ config ['cache_token_pre ' ]
124- ]);
123+ $ client = $ extend ['extend ' ]['client ' ] ?? self ::TOKEN_CLIENT_WEB ;
124+ RedisHandler::generateToken ($ config ['cache_token_pre ' ], $ client , $ extend ['extend ' ]['id ' ], $ extend ['exp ' ], $ newToken ['access_token ' ]);
125125 }
126126 return $ newToken ;
127127 }
@@ -152,12 +152,8 @@ public static function generateToken(array $extend): array
152152 $ token ['refresh_token ' ] = self ::makeToken ($ payload ['refreshPayload ' ], $ refreshSecretKey , $ config ['algorithms ' ]);
153153 }
154154 if ($ config ['is_single_device ' ]) {
155- RedisHandler::generateToken ([
156- 'id ' => $ extend ['id ' ],
157- 'access_token ' => $ token ['access_token ' ],
158- 'cache_token_ttl ' => $ config ['cache_token_ttl ' ],
159- 'cache_token_pre ' => $ config ['cache_token_pre ' ]
160- ]);
155+ $ client = $ extend ['extend ' ]['client ' ] ?? self ::TOKEN_CLIENT_WEB ;
156+ RedisHandler::generateToken ($ config ['cache_token_pre ' ], $ client , $ extend ['id ' ], $ config ['access_exp ' ], $ token ['access_token ' ]);
161157 }
162158 return $ token ;
163159 }
@@ -195,7 +191,7 @@ public static function verify(int $tokenType = self::ACCESS_TOKEN, string $token
195191 */
196192 private static function getTokenExtend (): array
197193 {
198- return (array ) self ::verify ()['extend ' ];
194+ return (array )self ::verify ()['extend ' ];
199195 }
200196
201197 /**
@@ -205,7 +201,7 @@ private static function getTokenExtend(): array
205201 */
206202 public static function getTokenExp (int $ tokenType = self ::ACCESS_TOKEN ): int
207203 {
208- return (int ) self ::verify ($ tokenType )['exp ' ] - time ();
204+ return (int )self ::verify ($ tokenType )['exp ' ] - time ();
209205 }
210206
211207 /**
@@ -255,16 +251,17 @@ private static function verifyToken(string $token, int $tokenType): array
255251 $ decoded = JWT ::decode ($ token , new Key ($ publicKey , $ config ['algorithms ' ]));
256252 $ decodeToken = json_decode (json_encode ($ decoded ), true );
257253 if ($ config ['is_single_device ' ] && self ::ACCESS_TOKEN == $ tokenType ) {
258- RedisHandler::verifyToken ($ config ['cache_token_pre ' ], (string ) $ decodeToken ['extend ' ]['id ' ], $ token );
254+ $ client = $ decodeToken ['extend ' ]['client ' ] ?? self ::TOKEN_CLIENT_WEB ;
255+ RedisHandler::verifyToken ($ config ['cache_token_pre ' ], $ client , (string )$ decodeToken ['extend ' ]['id ' ], $ token );
259256 }
260257 return $ decodeToken ;
261258 }
262259
263260 /**
264261 * @desc: 生成令牌.
265262 *
266- * @param array $payload 载荷信息
267- * @param string $secretKey 签名key
263+ * @param array $payload 载荷信息
264+ * @param string $secretKey 签名key
268265 * @param string $algorithms 算法
269266 * @return string
270267 */
@@ -361,13 +358,14 @@ private static function _getConfig(): array
361358
362359 /**
363360 * @desc: 注销令牌
361+ * @param string $client
364362 * @return bool
365363 */
366- public static function clear (): bool
364+ public static function clear (string $ client = self :: TOKEN_CLIENT_WEB ): bool
367365 {
368366 $ config = self ::_getConfig ();
369367 if ($ config ['is_single_device ' ]) {
370- return RedisHandler::clearToken ($ config ['cache_token_pre ' ], (string ) self ::getCurrentId ());
368+ return RedisHandler::clearToken ($ config ['cache_token_pre ' ], $ client , (string )self ::getCurrentId ());
371369 }
372370 return true ;
373371 }
0 commit comments