132 lines
3.3 KiB
JavaScript
132 lines
3.3 KiB
JavaScript
![]() |
/**
|
|||
|
* 获取localStorage
|
|||
|
*/
|
|||
|
function getStorage(name) {
|
|||
|
if (!name) return;
|
|||
|
if (window.localStorage.getItem(name) === 'null') {
|
|||
|
return null;
|
|||
|
}
|
|||
|
return window.localStorage.getItem(name);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 存储localStorage
|
|||
|
*/
|
|||
|
function setStorage(name, content) {
|
|||
|
if (!name) return;
|
|||
|
if (typeof content !== 'string') {
|
|||
|
content = JSON.stringify(content);
|
|||
|
}
|
|||
|
|
|||
|
window.localStorage.setItem(name, content);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 删除localStorage
|
|||
|
*/
|
|||
|
function removeStorage(name) {
|
|||
|
if (!name) return;
|
|||
|
window.localStorage.removeItem(name);
|
|||
|
}
|
|||
|
|
|||
|
export function setUser(user) {
|
|||
|
if (!user) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
return setUserStorage({
|
|||
|
// avatar: user.avatar ? user.avatar : null,
|
|||
|
// org_id: user.org_id ? user.org_id : null,
|
|||
|
// org_logo: user.org_logo ? user.org_logo : null,
|
|||
|
// org_name: user.org_name ? user.org_name : null,
|
|||
|
// role: user.role ? user.role : null,
|
|||
|
token: user.token ? user.token : null, //当前系统正在使用的token,用来兼容模板的启动
|
|||
|
user_token: user.user_token ? user.user_token : user.token, //保存普通用户使用的token
|
|||
|
admin_token: user.admin_token ? user.admin_token : null, //保存管理员用的token
|
|||
|
// token_expire: user.token_expire ? user.token_expire : null,
|
|||
|
// username: user.username ? user.username : null,
|
|||
|
// realname: user.realname ? user.realname : null
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取存储到storage中的用户信息
|
|||
|
* @returns {any|null}
|
|||
|
*/
|
|||
|
export const getUserInfo = function () {
|
|||
|
let user = getStorage(import.meta.env.VITE_APP_PROJECTNAME);
|
|||
|
if (window.Android) {
|
|||
|
let androidToken = window.Android.getToken();
|
|||
|
return {
|
|||
|
token: androidToken,
|
|||
|
admin_token: androidToken,
|
|||
|
user_token: androidToken,
|
|||
|
};
|
|||
|
} else {
|
|||
|
return user ? JSON.parse(user) : null;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 获取token
|
|||
|
* @returns {*|null}
|
|||
|
*/
|
|||
|
export const getToken = function () {
|
|||
|
let token = null;
|
|||
|
if (window.CELLSYSTOKEN) {
|
|||
|
token = window.CELLSYSTOKEN;
|
|||
|
} else {
|
|||
|
//目前只会在登录之后获取用户信息时进入此逻辑,登录之后固定为user_token
|
|||
|
if (getUserInfo()) {
|
|||
|
let user = getUserInfo();
|
|||
|
window.CELLSYSTOKEN = user['user_token'];
|
|||
|
token = window.CELLSYSTOKEN;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return token ? token : null;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 指定获取管理员token
|
|||
|
* @returns {*|null}
|
|||
|
*/
|
|||
|
export const getAdminToken = function () {
|
|||
|
let token;
|
|||
|
let storage = getStorage(import.meta.env.VITE_APP_PROJECTNAME);
|
|||
|
if (storage) {
|
|||
|
token = JSON.parse(storage)['admin_token'];
|
|||
|
}
|
|||
|
return token ? token : null;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 设置用户缓存信息
|
|||
|
* @param userInfo
|
|||
|
*/
|
|||
|
export function setUserStorage(userInfo) {
|
|||
|
return setStorage(import.meta.env.VITE_APP_PROJECTNAME, userInfo);
|
|||
|
}
|
|||
|
|
|||
|
//切换内存里普通用户和管理员token
|
|||
|
export function switchToken(role) {
|
|||
|
let token = getUserInfo();
|
|||
|
if (role === 'admin') {
|
|||
|
if (token['admin_token']) {
|
|||
|
window.CELLSYSTOKEN = token['admin_token'];
|
|||
|
}
|
|||
|
} else {
|
|||
|
window.CELLSYSTOKEN = token['user_token'];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 移除token
|
|||
|
*/
|
|||
|
export function removeToken() {
|
|||
|
return removeStorage(import.meta.env.VITE_APP_PROJECTNAME);
|
|||
|
}
|
|||
|
|
|||
|
export function getAppName() {
|
|||
|
return getStorage('appName');
|
|||
|
}
|