diff --git a/components/BsLogin.vue b/components/BsLogin.vue index 17e4000..df04795 100644 --- a/components/BsLogin.vue +++ b/components/BsLogin.vue @@ -147,11 +147,17 @@ export default { Message.error("请勾选同意《用户协议》和《隐私协议》"); return; } - const { result } = await ApiPostLogin({ + const { result, error } = await ApiPostLogin({ ...this.form, clientId: 1, systemId: 3, }); + if (error !== null) { + Message.error( + error.message || "登录失败,请检查手机号或验证码是否正确" + ); + return; + } if (result) { this.dialogTableVisible = false; this.$store.commit("setToken", result.token); diff --git a/layouts/module/header/HeaderInfoBar.vue b/layouts/module/header/HeaderInfoBar.vue index 0a7fc93..cd18b8d 100644 --- a/layouts/module/header/HeaderInfoBar.vue +++ b/layouts/module/header/HeaderInfoBar.vue @@ -122,7 +122,7 @@ export default { this.$router.push("/account/address"); break; case MENU_VALUE.LOGON_OUT: - this.$store.commit("setLoginOut"); + this.$store.dispatch("logout"); } }, diff --git a/plugins/api/account.js b/plugins/api/account.js index 6f78f56..835d0e7 100644 --- a/plugins/api/account.js +++ b/plugins/api/account.js @@ -31,6 +31,10 @@ export const ApiPutUser = (data) => * @param {*} data */ export const ApiPostLogin = (data) => ToAsyncAwait(axios.post(`${BASE_URL}/user/login`, data)); +/** + * 退出登录 + */ + export const ApiPostLogout = () => ToAsyncAwait(axiosTk.get(`${BASE_URL}/user/logout`)); /** * 获取手机验证码 * @param {*} params diff --git a/plugins/axiosTk.js b/plugins/axiosTk.js index bf532a4..b108c06 100644 --- a/plugins/axiosTk.js +++ b/plugins/axiosTk.js @@ -16,15 +16,15 @@ export default function ({$axios, store, route}, inject) { config.headers.Authorization = store.state.token; return config; }); - $axiosTk.onResponse(response => { + $axiosTk.onResponse(async response => { const result = response.data; if(response.status === 200){ if(result.code === 'SUCCESS'){ return result.data; } if(result.code === 'TOKEN_FAIL'){ - alert('这里要弹登录') - store.commit('setLoginOut'); + await store.dispatch('logout'); + store.commit('setLoginVisible'); return result; } return Promise.reject(result); diff --git a/store/index.js b/store/index.js index 18f385f..a1b5578 100644 --- a/store/index.js +++ b/store/index.js @@ -6,7 +6,7 @@ * @Description: file content */ import { TOKEN_KEY } from "@/constants"; -import { ApiGetCurrentUser } from "@/plugins/api/account"; +import { ApiGetCurrentUser, ApiPostLogout } from "@/plugins/api/account"; const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000; const state = () => ({ @@ -48,6 +48,12 @@ const actions = { commit("setUserInfo", result); } }, + + // 退出登录 + async logout({ commit }) { + await ApiPostLogout(); + commit("setLoginOut"); + }, }; export { state, mutations, actions };