109 lines
2.6 KiB
JavaScript
109 lines
2.6 KiB
JavaScript
const TokenKey = 'App-Token'
|
|
const UserInfo = 'customerInfo'
|
|
const TenantIdKey = 'TENANT_ID'
|
|
export function getToken() {
|
|
return uni.getStorageSync(TokenKey)
|
|
}
|
|
|
|
export function setToken(token) {
|
|
return uni.setStorageSync(TokenKey, token)
|
|
}
|
|
|
|
export function removeToken() {
|
|
return uni.removeStorageSync(TokenKey)
|
|
}
|
|
|
|
export function setTenantId(TenantId) {
|
|
return uni.setStorageSync(TenantIdKey, TenantId);
|
|
}
|
|
export function getTenantId(){
|
|
return uni.getStorageSync(TenantIdKey)
|
|
}
|
|
export function removeTenantId() {
|
|
return uni.removeStorageSync(TenantIdKey);
|
|
}
|
|
export function hasRole(roleCode) {
|
|
const roleList = uni.getStorageSync('role')
|
|
if (roleList && roleList.length > 0) {
|
|
const roleInfo = roleList.find(f => f.code === roleCode)
|
|
if (roleInfo) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
export function getUserInfo() {
|
|
if(uni.getStorageSync(UserInfo)){
|
|
return JSON.parse(uni.getStorageSync(UserInfo))
|
|
}else{
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function setUserInfo(dataObj) {
|
|
return uni.setStorageSync(UserInfo, JSON.stringify(dataObj))
|
|
}
|
|
|
|
export function removeUserInfo() {
|
|
return uni.removeStorageSync(UserInfo)
|
|
}
|
|
|
|
export function getJSONData(keyStr) {
|
|
if(uni.getStorageSync(keyStr)){
|
|
return JSON.parse(uni.getStorageSync(keyStr))
|
|
}else{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export function setJSONData(keyStr,dataObj) {
|
|
return uni.setStorageSync(keyStr, JSON.stringify(dataObj))
|
|
}
|
|
|
|
export function removeJSONData(keyStr) {
|
|
return uni.removeStorageSync(keyStr)
|
|
}
|
|
|
|
export function getStrData(keyStr) {
|
|
return uni.getStorageSync(keyStr)
|
|
}
|
|
|
|
export function setStrData(keyStr,dataStr) {
|
|
return uni.setStorageSync(keyStr, dataStr)
|
|
}
|
|
|
|
export function removeStrData(keyStr) {
|
|
return uni.removeStorageSync(keyStr)
|
|
}
|
|
|
|
// 设置本地存储,并设置一个过期时间(单位为秒)
|
|
export function setStorageWithExpiry(key, value, ttl) {
|
|
const now = new Date();
|
|
// 计算过期时间
|
|
const item = {
|
|
value: value,
|
|
expiry: now.getTime() + ttl * 1000,
|
|
};
|
|
// 将数据对象转换为字符串存储
|
|
uni.setStorageSync(key, JSON.stringify(item));
|
|
}
|
|
|
|
// 获取本地存储的数据,检查是否过期
|
|
export function getStorageWithExpiry(key) {
|
|
// 从本地存储获取数据
|
|
const itemStr = uni.getStorageSync(key);
|
|
if (!itemStr) {
|
|
return null; // 未找到数据
|
|
}
|
|
const item = JSON.parse(itemStr);
|
|
const now = new Date();
|
|
// 检查项目是否过期
|
|
if (now.getTime() > item.expiry) {
|
|
// 如果过期,从存储中移除该项
|
|
uni.removeStorageSync(key);
|
|
return null;
|
|
}
|
|
return item.value; // 数据未过期,返回数据
|
|
}
|