perf(登录): 优化登录接口路径并完善退出登录功能
This commit is contained in:
@@ -30,6 +30,15 @@ function gotoLogin() {
|
||||
url: `${LOGIN_PAGE}?redirect=${encodeURIComponent('/pages/about/about?a=1&b=2')}`,
|
||||
})
|
||||
}
|
||||
function logout() {
|
||||
// 清空用户信息
|
||||
tokenStore.logout()
|
||||
// 执行退出登录逻辑
|
||||
uni.showToast({
|
||||
title: '退出登录成功',
|
||||
icon: 'success',
|
||||
})
|
||||
}
|
||||
|
||||
function gotoTabbar() {
|
||||
uni.switchTab({
|
||||
@@ -93,9 +102,17 @@ onShow(() => {
|
||||
<view class="my-2 text-center">
|
||||
<image src="/static/images/avatar.jpg" class="h-100px w-100px" />
|
||||
</view>
|
||||
<button class="mt-4 w-40 text-center" @click="gotoLogin">
|
||||
点击去登录页
|
||||
</button>
|
||||
<view class="my-2 text-center">
|
||||
当前是否登录:{{ tokenStore.hasLogin }}
|
||||
</view>
|
||||
<view class="m-auto max-w-600px flex items-center">
|
||||
<button class="mt-4 w-40 text-center" @click="gotoLogin">
|
||||
点击去登录页
|
||||
</button>
|
||||
<button class="mt-4 w-40 text-center" @click="logout">
|
||||
点击退出登录
|
||||
</button>
|
||||
</view>
|
||||
<button class="mt-4 w-60 text-center" @click="setTabbarBadge">
|
||||
设置tabbarBadge
|
||||
</button>
|
||||
|
||||
@@ -119,7 +119,7 @@ export async function userCreateWithListUsingPost({
|
||||
});
|
||||
}
|
||||
|
||||
/** Logs user into the system GET /user/login */
|
||||
/** Logs user into the system GET /auth/login */
|
||||
export async function userLoginUsingGet({
|
||||
params,
|
||||
options,
|
||||
@@ -128,7 +128,7 @@ export async function userLoginUsingGet({
|
||||
params: API.userLoginUsingGetParams;
|
||||
options?: CustomRequestOptions;
|
||||
}) {
|
||||
return request<string>('/user/login', {
|
||||
return request<string>('/auth/login', {
|
||||
method: 'GET',
|
||||
params: {
|
||||
...params,
|
||||
|
||||
@@ -122,7 +122,7 @@ export function useUserCreateWithListUsingPostMutation(options?: {
|
||||
return response;
|
||||
}
|
||||
|
||||
/** Logs user into the system GET /user/login */
|
||||
/** Logs user into the system GET /auth/login */
|
||||
export function userLoginUsingGetQueryOptions(options: {
|
||||
// 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
|
||||
params: API.userLoginUsingGetParams;
|
||||
|
||||
@@ -3,7 +3,6 @@ import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue' // 修复:导入 computed
|
||||
import {
|
||||
login as _login,
|
||||
logout as _logout,
|
||||
refreshToken as _refreshToken,
|
||||
wxLogin as _wxLogin,
|
||||
getWxCode,
|
||||
@@ -155,18 +154,21 @@ export const useTokenStore = defineStore(
|
||||
*/
|
||||
const logout = async () => {
|
||||
try {
|
||||
await _logout()
|
||||
// 清除存储的过期时间
|
||||
uni.removeStorageSync('accessTokenExpireTime')
|
||||
uni.removeStorageSync('refreshTokenExpireTime')
|
||||
// TODO 实现自己的退出登录逻辑
|
||||
// await _logout()
|
||||
}
|
||||
catch (error) {
|
||||
console.error('退出登录失败:', error)
|
||||
}
|
||||
finally {
|
||||
// 无论成功失败,都需要清除本地token信息
|
||||
const userStore = useUserStore()
|
||||
await userStore.removeUserInfo()
|
||||
// 清除存储的过期时间
|
||||
uni.removeStorageSync('accessTokenExpireTime')
|
||||
uni.removeStorageSync('refreshTokenExpireTime')
|
||||
console.log('退出登录-清除用户信息')
|
||||
tokenInfo.value = { ...tokenInfoState }
|
||||
uni.removeStorageSync('user')
|
||||
uni.removeStorageSync('token')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +223,9 @@ export const useTokenStore = defineStore(
|
||||
* 检查是否有登录信息(不考虑token是否过期)
|
||||
*/
|
||||
const hasLoginInfo = computed(() => {
|
||||
if (!tokenInfo.value) {
|
||||
return false
|
||||
}
|
||||
if (isDoubleTokenMode) {
|
||||
return isDoubleTokenRes(tokenInfo.value) && !!tokenInfo.value.accessToken
|
||||
}
|
||||
@@ -233,6 +238,7 @@ export const useTokenStore = defineStore(
|
||||
* 检查是否已登录且token有效
|
||||
*/
|
||||
const hasValidLogin = computed(() => {
|
||||
console.log('hasValidLogin', hasLoginInfo.value, !isTokenExpired.value)
|
||||
return hasLoginInfo.value && !isTokenExpired.value
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user