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.
fude/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'
1 year ago
const AGENT_INFO = 'AGENT_INFO'
1 year ago
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) {
1 year ago
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)))
1 year ago
}
/**
* 获取用户信息
*/
export function getUserInfo() {
1 year ago
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) || '')
1 year ago
return data ? JSON.parse(data) : {}
}
/**
* 删除用户相关缓存信息
*/
export function removeAll() {
uni.clearStorageSync()
uni.removeStorageSync(USER_TOKEN)
uni.removeStorageSync(USER_INFO)
1 year ago
uni.removeStorageSync(AGENT_INFO)
1 year ago
}
/**
* 获取用户本地缓存的设置信息
*/
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))
}