diff --git a/src/api/account.ts b/src/api/account.ts index d3b40ac..66fc8fa 100644 --- a/src/api/account.ts +++ b/src/api/account.ts @@ -120,3 +120,17 @@ export const AccountBindEmail = (data) => { loading: true }) } + +/** + * 退出登录 + * @param data - 参数 + * @param data.userId - 用户ID +*/ +export const AccountLogout = (data) => { + return request({ + url: '/buyer/account/logout', + method: 'post', + data, + loading: true + }) +} diff --git a/src/stores/userInfo.ts b/src/stores/userInfo.ts index 7165457..5c6bc54 100644 --- a/src/stores/userInfo.ts +++ b/src/stores/userInfo.ts @@ -1,8 +1,10 @@ // 每一个存储的模块,命名规则use开头,store结尾 +import { AccountLogout } from '@/api/account' import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { removeLocal, setLocal } from '@/utils/local' import MyEvent from '@/utils/myEvent' +import router from '@/router' export const useUserInfoStore = defineStore('userInfo', () => { const state = ref({ userInfo: { @@ -26,20 +28,21 @@ export const useUserInfoStore = defineStore('userInfo', () => { setLocal(data, 'token') } - const logout = () => { + const logout = async (reload: boolean = false) => { // 处理退出登录的一些逻辑 - return new Promise((resolve) => { - state.value.userInfo = { - userId: "", - email: "", - username: "", - accessToken: "", - expiresIn: "", - } - state.value.token = '' - removeLocal('token') - resolve('') - }) + const userId = state.value.userInfo.userId + if (userId) await AccountLogout({ userId }) + state.value.userInfo = { + userId: "", + email: "", + username: "", + accessToken: "", + expiresIn: "", + } + state.value.token = '' + removeLocal('token') + if (reload) router.go(0) + } return { diff --git a/src/views/main-header.vue b/src/views/main-header.vue index cc64bd5..da4f4fc 100644 --- a/src/views/main-header.vue +++ b/src/views/main-header.vue @@ -9,7 +9,10 @@ :key="v.path" class="nav-item" :class="{ - active: v.path === '/' ? activePath === v.path : new RegExp(`^${v.path}`).test(activePath) + active: + v.path === '/' + ? activePath === v.path + : new RegExp(`^${v.path}`).test(activePath) }" @click="onNavItemClick(v.path)" > @@ -141,8 +144,7 @@ hideProfilePopover() ElMessageBox.confirm('Are you sure to log off?') .then(() => { - userInfoStore.logout() - router.go(0) + userInfoStore.logout(true) }) .catch(() => {}) }