You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
huazhiyu/common/auth.js

82 lines
1.7 KiB

1 year ago
/**
* auth管理
* author zhangwei
*/
import {
Base64
} from "js-base64/base64"
const USER_TOKEN = 'USER_TOKEN'
const USER_INFO = 'USER_INFO'
const AGENT_INFO = 'AGENT_INFO'
const APP_SETTING = 'APP_SETTING'
/**
* 设置用户授权token
* @param {String} token
*/
export function setToken(token) {
return uni.setStorageSync(USER_TOKEN, token)
}
/**
* 获取授权token
*/
export function getToken() {
return uni.getStorageSync(USER_TOKEN) || ''
}
/**
* 设置用户信息
*/
export function setUserInfo(data) {
uni.setStorageSync(USER_INFO, JSON.stringify(data))
}
// 设置代理商信息
export function setAgentInfo(data) {
uni.setStorageSync(AGENT_INFO, JSON.stringify(data))
// uni.setStorageSync(AGENT_INFO, Base64.encode(JSON.stringify(data)))
}
/**
* 获取用户信息
*/
export function getUserInfo() {
const data = uni.getStorageSync(USER_INFO)|| ''
return data ? JSON.parse(data) : {}
}
/**
* 获取代理商信息
*/
export function getAgentInfo() {
const data = uni.getStorageSync(AGENT_INFO) || ''
// const data = Base64.decode(uni.getStorageSync(AGENT_INFO) || '')
return data ? JSON.parse(data) : {}
}
/**
* 删除用户相关缓存信息
*/
export function removeAll() {
1 year ago
// uni.clearStorageSync()
1 year ago
uni.removeStorageSync(USER_TOKEN)
uni.removeStorageSync(USER_INFO)
uni.removeStorageSync(AGENT_INFO)
}
/**
* 获取用户本地缓存的设置信息
*/
export function getAppSetting() {
const data = uni.getStorageSync(APP_SETTING)
return data ? JSON.parse(data) : {}
}
/**
* 用户设置保存到浏览器缓存中
* @param {Object} state 用户设置相关信息
*/
export function setAppSetting(state) {
uni.setStorageSync(APP_SETTING, JSON.stringify(state))
}