ref : 重构令牌管理逻辑,,统一状态码识别
This commit is contained in:
@@ -1,25 +1,45 @@
|
||||
import Cookies from 'js-cookie';
|
||||
import { TOKEN_COOKIE } from '@/constants/token-constants';
|
||||
import { localRemove, localSet } from '@/utils/cache-local-utils';
|
||||
import {
|
||||
CACHE_LOCAL_LOCK_PASSWD,
|
||||
CACHE_LOCAL_MASK,
|
||||
} from '@/constants/cache-keys-constants';
|
||||
TOKEN_ACCESS_COOKIE,
|
||||
TOKEN_REFRESH_COOKIE,
|
||||
} from '@/constants/token-constants';
|
||||
|
||||
/**获取cookis中Token字符串 */
|
||||
export function getToken(): string {
|
||||
return Cookies.get(TOKEN_COOKIE) || '';
|
||||
/**获取访问令牌 */
|
||||
export function getAccessToken(): string {
|
||||
return Cookies.get(TOKEN_ACCESS_COOKIE) || '';
|
||||
}
|
||||
|
||||
/**设置cookis中Token字符串 */
|
||||
export function setToken(token: string): void {
|
||||
Cookies.set(TOKEN_COOKIE, token || '');
|
||||
localSet(CACHE_LOCAL_MASK, 'none');
|
||||
/**
|
||||
* 设置访问令牌
|
||||
* @param token token字符串
|
||||
* @param exp 过期时间(秒)
|
||||
*/
|
||||
export function setAccessToken(token: string, exp: number): void {
|
||||
const expires = new Date(new Date().getTime() + exp * 1000);
|
||||
Cookies.set(TOKEN_ACCESS_COOKIE, token, { expires });
|
||||
}
|
||||
|
||||
/**移除cookis中Token字符串,localStorage中锁屏字符串 */
|
||||
export function removeToken(): void {
|
||||
Cookies.remove(TOKEN_COOKIE);
|
||||
localRemove(CACHE_LOCAL_MASK);
|
||||
localRemove(CACHE_LOCAL_LOCK_PASSWD);
|
||||
/**移除访问令牌 */
|
||||
export function delAccessToken(): void {
|
||||
Cookies.remove(TOKEN_ACCESS_COOKIE);
|
||||
}
|
||||
|
||||
/**获取刷新令牌 */
|
||||
export function getRefreshToken(): string {
|
||||
return Cookies.get(TOKEN_REFRESH_COOKIE) || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置刷新令牌
|
||||
* @param token token字符串
|
||||
* @param exp 过期时间(秒)
|
||||
*/
|
||||
export function setRefreshToken(token: string, exp: number): void {
|
||||
const expires = new Date(new Date().getTime() + exp * 1000);
|
||||
Cookies.set(TOKEN_REFRESH_COOKIE, token, { expires });
|
||||
}
|
||||
|
||||
/**移除刷新令牌 */
|
||||
export function delRefreshToken(): void {
|
||||
Cookies.remove(TOKEN_REFRESH_COOKIE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user