Skip to content

Commit 6e1893a

Browse files
author
feige996
committed
perf(登录): 优化登录接口路径并完善退出登录功能
1 parent 30c8caa commit 6e1893a

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/pages/about/about.vue

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ function gotoLogin() {
3030
url: `${LOGIN_PAGE}?redirect=${encodeURIComponent('/pages/about/about?a=1&b=2')}`,
3131
})
3232
}
33+
function logout() {
34+
// 清空用户信息
35+
tokenStore.logout()
36+
// 执行退出登录逻辑
37+
uni.showToast({
38+
title: '退出登录成功',
39+
icon: 'success',
40+
})
41+
}
3342
3443
function gotoTabbar() {
3544
uni.switchTab({
@@ -93,9 +102,17 @@ onShow(() => {
93102
<view class="my-2 text-center">
94103
<image src="/static/images/avatar.jpg" class="h-100px w-100px" />
95104
</view>
96-
<button class="mt-4 w-40 text-center" @click="gotoLogin">
97-
点击去登录页
98-
</button>
105+
<view class="my-2 text-center">
106+
当前是否登录:{{ tokenStore.hasLogin }}
107+
</view>
108+
<view class="m-auto max-w-600px flex items-center">
109+
<button class="mt-4 w-40 text-center" @click="gotoLogin">
110+
点击去登录页
111+
</button>
112+
<button class="mt-4 w-40 text-center" @click="logout">
113+
点击退出登录
114+
</button>
115+
</view>
99116
<button class="mt-4 w-60 text-center" @click="setTabbarBadge">
100117
设置tabbarBadge
101118
</button>

src/service/user.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export async function userCreateWithListUsingPost({
119119
});
120120
}
121121

122-
/** Logs user into the system GET /user/login */
122+
/** Logs user into the system GET /auth/login */
123123
export async function userLoginUsingGet({
124124
params,
125125
options,
@@ -128,7 +128,7 @@ export async function userLoginUsingGet({
128128
params: API.userLoginUsingGetParams;
129129
options?: CustomRequestOptions;
130130
}) {
131-
return request<string>('/user/login', {
131+
return request<string>('/auth/login', {
132132
method: 'GET',
133133
params: {
134134
...params,

src/service/user.vuequery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function useUserCreateWithListUsingPostMutation(options?: {
122122
return response;
123123
}
124124

125-
/** Logs user into the system GET /user/login */
125+
/** Logs user into the system GET /auth/login */
126126
export function userLoginUsingGetQueryOptions(options: {
127127
// 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
128128
params: API.userLoginUsingGetParams;

src/store/token.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defineStore } from 'pinia'
33
import { computed, ref } from 'vue' // 修复:导入 computed
44
import {
55
login as _login,
6-
logout as _logout,
76
refreshToken as _refreshToken,
87
wxLogin as _wxLogin,
98
getWxCode,
@@ -155,18 +154,21 @@ export const useTokenStore = defineStore(
155154
*/
156155
const logout = async () => {
157156
try {
158-
await _logout()
159-
// 清除存储的过期时间
160-
uni.removeStorageSync('accessTokenExpireTime')
161-
uni.removeStorageSync('refreshTokenExpireTime')
157+
// TODO 实现自己的退出登录逻辑
158+
// await _logout()
162159
}
163160
catch (error) {
164161
console.error('退出登录失败:', error)
165162
}
166163
finally {
167164
// 无论成功失败,都需要清除本地token信息
168-
const userStore = useUserStore()
169-
await userStore.removeUserInfo()
165+
// 清除存储的过期时间
166+
uni.removeStorageSync('accessTokenExpireTime')
167+
uni.removeStorageSync('refreshTokenExpireTime')
168+
console.log('退出登录-清除用户信息')
169+
tokenInfo.value = { ...tokenInfoState }
170+
uni.removeStorageSync('user')
171+
uni.removeStorageSync('token')
170172
}
171173
}
172174

@@ -221,6 +223,9 @@ export const useTokenStore = defineStore(
221223
* 检查是否有登录信息(不考虑token是否过期)
222224
*/
223225
const hasLoginInfo = computed(() => {
226+
if (!tokenInfo.value) {
227+
return false
228+
}
224229
if (isDoubleTokenMode) {
225230
return isDoubleTokenRes(tokenInfo.value) && !!tokenInfo.value.accessToken
226231
}
@@ -233,6 +238,7 @@ export const useTokenStore = defineStore(
233238
* 检查是否已登录且token有效
234239
*/
235240
const hasValidLogin = computed(() => {
241+
console.log('hasValidLogin', hasLoginInfo.value, !isTokenExpired.value)
236242
return hasLoginInfo.value && !isTokenExpired.value
237243
})
238244

0 commit comments

Comments
 (0)