|
|
@@ -61,8 +61,8 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断token是否过期
|
|
|
- */
|
|
|
+ * 判断token是否过期
|
|
|
+ */
|
|
|
const isTokenExpired = computed(() => {
|
|
|
if (!tokenInfo.value) {
|
|
|
return true
|
|
|
@@ -77,8 +77,8 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 判断refreshToken是否过期
|
|
|
- */
|
|
|
+ * 判断refreshToken是否过期
|
|
|
+ */
|
|
|
const isRefreshTokenExpired = computed(() => {
|
|
|
if (!isDoubleTokenMode)
|
|
|
return true
|
|
|
@@ -92,9 +92,9 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 登录成功后处理逻辑
|
|
|
- * @param tokenInfo 登录返回的token信息
|
|
|
- */
|
|
|
+ * 登录成功后处理逻辑
|
|
|
+ * @param tokenInfo 登录返回的token信息
|
|
|
+ */
|
|
|
async function _postLogin(tokenInfo: IAuthLoginRes) {
|
|
|
setTokenInfo(tokenInfo)
|
|
|
// const userStore = useUserStore()
|
|
|
@@ -102,12 +102,12 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 用户登录
|
|
|
- * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
- * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
- * @param loginForm 登录参数
|
|
|
- * @returns 登录结果
|
|
|
- */
|
|
|
+ * 用户登录
|
|
|
+ * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
+ * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
+ * @param loginForm 登录参数
|
|
|
+ * @returns 登录结果
|
|
|
+ */
|
|
|
const login = async (loginForm: ILoginForm) => {
|
|
|
try {
|
|
|
const res = await _login(loginForm)
|
|
|
@@ -130,11 +130,11 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 微信登录
|
|
|
- * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
- * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
- * @returns 登录结果
|
|
|
- */
|
|
|
+ * 微信登录
|
|
|
+ * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
+ * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
+ * @returns 登录结果
|
|
|
+ */
|
|
|
const wxLogin = async () => {
|
|
|
try {
|
|
|
// 获取用户信息
|
|
|
@@ -178,8 +178,8 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 退出登录 并 删除用户信息
|
|
|
- */
|
|
|
+ * 退出登录 并 删除用户信息
|
|
|
+ */
|
|
|
const logout = async () => {
|
|
|
try {
|
|
|
// TODO 实现自己的退出登录逻辑
|
|
|
@@ -210,9 +210,9 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 刷新token
|
|
|
- * @returns 刷新结果
|
|
|
- */
|
|
|
+ * 刷新token
|
|
|
+ * @returns 刷新结果
|
|
|
+ */
|
|
|
const refreshToken = async () => {
|
|
|
if (!isDoubleTokenMode) {
|
|
|
console.error('单token模式不支持刷新token')
|
|
|
@@ -238,10 +238,10 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取有效的token
|
|
|
- * 注意:在computed中不直接调用异步函数,只做状态判断
|
|
|
- * 实际的刷新操作应由调用方处理
|
|
|
- */
|
|
|
+ * 获取有效的token
|
|
|
+ * 注意:在computed中不直接调用异步函数,只做状态判断
|
|
|
+ * 实际的刷新操作应由调用方处理
|
|
|
+ */
|
|
|
const getValidToken = computed(() => {
|
|
|
// token已过期,返回空
|
|
|
if (isTokenExpired.value) {
|
|
|
@@ -257,8 +257,8 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 检查是否有登录信息(不考虑token是否过期)
|
|
|
- */
|
|
|
+ * 检查是否有登录信息(不考虑token是否过期)
|
|
|
+ */
|
|
|
const hasLoginInfo = computed(() => {
|
|
|
if (!tokenInfo.value) {
|
|
|
return false
|
|
|
@@ -272,17 +272,17 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 检查是否已登录且token有效
|
|
|
- */
|
|
|
+ * 检查是否已登录且token有效
|
|
|
+ */
|
|
|
const hasValidLogin = computed(() => {
|
|
|
console.log('hasValidLogin', hasLoginInfo.value && !isTokenExpired.value, hasLoginInfo.value, !isTokenExpired.value)
|
|
|
return hasLoginInfo.value && !isTokenExpired.value
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 尝试获取有效的token,如果过期且可刷新,则刷新token
|
|
|
- * @returns 有效的token或空字符串
|
|
|
- */
|
|
|
+ * 尝试获取有效的token,如果过期且可刷新,则刷新token
|
|
|
+ * @returns 有效的token或空字符串
|
|
|
+ */
|
|
|
const tryGetValidToken = async (): Promise<string> => {
|
|
|
if (!getValidToken.value && isDoubleTokenMode && !isRefreshTokenExpired.value) {
|
|
|
try {
|