mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-06-11 12:49:30 +08:00
refactor: 对token配置进行重构、同时修复token获取失败的问题
This commit is contained in:
+1
-1
@@ -10,7 +10,7 @@
|
||||
* 修改时间:
|
||||
*/
|
||||
|
||||
import HaloTokenConfig from '@/config/token.config.js'
|
||||
import HaloTokenConfig from '@/config/uhalo.config.js'
|
||||
import {getAppConfigs} from '@/config/index.js'
|
||||
|
||||
const utils = {
|
||||
|
||||
+22
-22
@@ -1,39 +1,39 @@
|
||||
/**
|
||||
* 设置缓存
|
||||
* @param {缓存key} key
|
||||
* @param {需要存储的缓存值} value
|
||||
* @param {过期时间,默认0表示永久有效} expire
|
||||
* @param {string} key 缓存key
|
||||
* @param {Object} value 需要存储的缓存值
|
||||
* @param {number} expire 过期时间,默认0表示永久有效
|
||||
*/
|
||||
export const setCache = (key, value, expire = 0) => {
|
||||
let obj = {
|
||||
data: value, //存储的数据
|
||||
time: Date.now() / 1000, //记录存储的时间戳
|
||||
expire: expire //记录过期时间,单位秒
|
||||
}
|
||||
uni.setStorageSync(key, JSON.stringify(obj))
|
||||
let obj = {
|
||||
data: value, //存储的数据
|
||||
time: Date.now() / 1000, //记录存储的时间戳
|
||||
expire: expire //记录过期时间,单位秒
|
||||
}
|
||||
uni.setStorageSync(key, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param {缓存key} key
|
||||
* @param {string} key
|
||||
*/
|
||||
export const getCache = (key) => {
|
||||
let val = uni.getStorageSync(key)
|
||||
if (!val) {
|
||||
return null
|
||||
}
|
||||
val = JSON.parse(val)
|
||||
if (val.expire && Date.now() / 1000 - val.time > val.expire) {
|
||||
uni.removeStorageSync(key)
|
||||
return null
|
||||
}
|
||||
return val.data
|
||||
let val = uni.getStorageSync(key)
|
||||
if (!val) {
|
||||
return null
|
||||
}
|
||||
val = JSON.parse(val)
|
||||
if (val.expire && Date.now() / 1000 - val.time > val.expire) {
|
||||
uni.removeStorageSync(key)
|
||||
return null
|
||||
}
|
||||
return val.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @param {缓存key} key
|
||||
* @param { string } key 缓存key
|
||||
*/
|
||||
export const delCache = (key) => {
|
||||
uni.removeStorageSync(key)
|
||||
uni.removeStorageSync(key)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import {getCache, setCache} from "./storage";
|
||||
import utils from "./index"
|
||||
|
||||
export const APP_TOKENS_KEY = "APP_TOKENS";
|
||||
|
||||
|
||||
/**
|
||||
* 获取 tokens
|
||||
*/
|
||||
export function getTokens() {
|
||||
const tokens = getCache(APP_TOKENS_KEY);
|
||||
return tokens ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 tokens
|
||||
*/
|
||||
export function setTokens(value) {
|
||||
setCache(APP_TOKENS_KEY, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 tokens
|
||||
*/
|
||||
export function updateTokens(value) {
|
||||
if (!value) return false
|
||||
const tokens = getTokens(APP_TOKENS_KEY);
|
||||
if (!tokens) return false
|
||||
setTokens(utils.deepMerge(tokens, value))
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 系统token
|
||||
*/
|
||||
export function getPersonalToken() {
|
||||
const tokens = getTokens(APP_TOKENS_KEY);
|
||||
console.log("tokens", tokens)
|
||||
return tokens?.personalToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 系统token
|
||||
*/
|
||||
export function setPersonalToken(personalToken) {
|
||||
const tokens = getCache(APP_TOKENS_KEY);
|
||||
if (!tokens) return false
|
||||
tokens.personalToken = personalToken
|
||||
updateTokens(tokens)
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user