vue-admin-beautiful/src/utils/accessToken.js
2024-06-26 12:21:16 +08:00

60 lines
1.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { storage, tokenTableName } from '@/config'
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 获取accessToken
* @returns {string|ActiveX.IXMLDOMNode|Promise<any>|any|IDBRequest<any>|MediaKeyStatus|FormDataEntryValue|Function|Promise<Credential | null>}
*/
export function getAccessToken() {
if (storage) {
if ('localStorage' === storage) {
return localStorage.getItem(tokenTableName)
} else if ('sessionStorage' === storage) {
return sessionStorage.getItem(tokenTableName)
} else {
return localStorage.getItem(tokenTableName)
}
} else {
return localStorage.getItem(tokenTableName)
}
}
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 存储accessToken
* @param accessToken
* @returns {void|*}
*/
export function setAccessToken(accessToken) {
if (storage) {
if ('localStorage' === storage) {
return localStorage.setItem(tokenTableName, accessToken)
} else if ('sessionStorage' === storage) {
return sessionStorage.setItem(tokenTableName, accessToken)
} else {
return localStorage.setItem(tokenTableName, accessToken)
}
} else {
return localStorage.setItem(tokenTableName, accessToken)
}
}
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 移除accessToken
* @returns {void|Promise<void>}
*/
export function removeAccessToken() {
if (storage) {
if ('localStorage' === storage) {
return localStorage.removeItem(tokenTableName)
} else if ('sessionStorage' === storage) {
return sessionStorage.clear()
} else {
return localStorage.removeItem(tokenTableName)
}
} else {
return localStorage.removeItem(tokenTableName)
}
}