lanan-repair-app/utils/auth.js

109 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-10-09 13:34:36 +08:00
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)
}
2024-10-17 15:22:37 +08:00
export function removeTenantId() {
return uni.removeStorageSync(TenantIdKey);
}
2024-10-09 13:34:36 +08:00
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() {
2024-10-16 17:14:49 +08:00
if(uni.getStorageSync(UserInfo)){
return JSON.parse(uni.getStorageSync(UserInfo))
}else{
return undefined;
}
2024-10-09 13:34:36 +08:00
}
2024-10-16 17:14:49 +08:00
export function setUserInfo(dataObj) {
return uni.setStorageSync(UserInfo, JSON.stringify(dataObj))
2024-10-09 13:34:36 +08:00
}
2024-10-16 18:35:15 +08:00
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)
}
2024-10-17 15:22:37 +08:00
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)
}
2024-10-18 15:30:03 +08:00
// 设置本地存储,并设置一个过期时间(单位为秒)
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; // 数据未过期,返回数据
}