policeSecurity/collect_information/src/store/userStore.ts

41 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {defineStore} from "pinia";
import {ref} from "vue";
import Taro from "@tarojs/taro";
export const useUserStore = defineStore('userStore', () => {
/** 获取token信息 **/
const getTokenInfo = (): TokenInfo | null => {
const ti = Taro.getStorageSync('tokenInfo')
// 如果 ti 存在且不是空字符串,则返回 ti否则返回 null
return (ti !== undefined && ti !== null && ti !== '') ? ti : null;
}
/** 用户token信息 **/
const tokenInfo = ref<TokenInfo | null>(getTokenInfo())
/** 保存token信息 **/
const saveTokenInfo = (t: TokenInfo) => {
tokenInfo.value = t;
Taro.setStorageSync('tokenInfo', tokenInfo.value)
}
/** 清楚token信息 */
const clearTokenInfo = () => {
tokenInfo.value = null
Taro.removeStorageSync('tokenInfo')
}
/**
* 重置用户数据
* 一般用于用户退出登录 重置所有相关数据
*/
const resetUserInfo = () => {
clearTokenInfo()
}
return {
tokenInfo,
saveTokenInfo,
getTokenInfo,
resetUserInfo
}
})