From 0becf0b4bc5e0d4b0b08b7427b73177561394f74 Mon Sep 17 00:00:00 2001 From: tomoat Date: Thu, 7 Apr 2022 22:47:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8mock=20api,=20=E5=8F=AF=E4=BB=A5=E4=BD=BF?= =?UTF-8?q?=E7=94=A8test=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=20(#233)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 用户登录使用mock api, 可以使用test用户登录 * Update permission.ts --- mock/user.ts | 23 +++++++++++++++++++++++ src/layout/hooks/nav.ts | 4 ++-- src/store/modules/permission.ts | 5 ++++- src/store/modules/user.ts | 10 +++++----- src/views/login.vue | 20 ++++++++++++++------ 5 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 mock/user.ts diff --git a/mock/user.ts b/mock/user.ts new file mode 100644 index 000000000..34389c0d2 --- /dev/null +++ b/mock/user.ts @@ -0,0 +1,23 @@ +import { MockMethod } from "vite-plugin-mock"; + +export default [ + { + url: "/login", + method: "post", + response: ({ body }) => { + if (body.username === "admin") { + return { + username: "admin", + expires: 24 * 60 * 60, + accessToken: "eyJhbGciOiJIUzUxMiJ9.admin" + }; + } else { + return { + username: "test", + expires: 24 * 60 * 60, + accessToken: "eyJhbGciOiJIUzUxMiJ9.test" + }; + } + } + } +] as MockMethod[]; diff --git a/src/layout/hooks/nav.ts b/src/layout/hooks/nav.ts index a162be933..d3e16ada7 100644 --- a/src/layout/hooks/nav.ts +++ b/src/layout/hooks/nav.ts @@ -8,6 +8,7 @@ import { transformI18n } from "/@/plugins/i18n"; import { storageSession } from "/@/utils/storage"; import { useAppStoreHook } from "/@/store/modules/app"; import { useEpThemeStoreHook } from "/@/store/modules/epTheme"; +import { useUserStoreHook } from "/@/store/modules/user"; export function useNav() { const pureApp = useAppStoreHook(); @@ -38,8 +39,7 @@ export function useNav() { // 退出登录 function logout() { - storageSession.removeItem("info"); - router.push("/login"); + useUserStoreHook().logOut(); } function backHome() { diff --git a/src/store/modules/permission.ts b/src/store/modules/permission.ts index f2aa691d0..a2d84cf90 100644 --- a/src/store/modules/permission.ts +++ b/src/store/modules/permission.ts @@ -61,8 +61,11 @@ export const usePermissionStore = defineStore({ break; } }, - // 清空缓存页面 + // 清空缓存 clearAllCachePage() { + this.wholeMenus = []; + this.menusTree = []; + this.buttonAuth = []; this.cachePageList = []; } } diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 6fb490c49..dcac6194c 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -33,12 +33,12 @@ export const useUserStore = defineStore({ }, // 登入 async loginByUsername(data) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { getLogin(data) - .then(data => { - if (data) { - setToken(data); - resolve(); + .then(res => { + if (res) { + setToken(res); + resolve(res); } }) .catch(error => { diff --git a/src/views/login.vue b/src/views/login.vue index 75dc2c956..dcf256472 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -14,6 +14,9 @@ import illustration4 from "/@/assets/login/illustration4.svg?component"; import illustration5 from "/@/assets/login/illustration5.svg?component"; import illustration6 from "/@/assets/login/illustration6.svg?component"; +import { useUserStoreHook } from "/@/store/modules/user"; +import { usePermissionStoreHook } from "/@/store/modules/permission"; + const router = useRouter(); // eslint-disable-next-line vue/return-in-computed-property @@ -42,12 +45,17 @@ let user = ref("admin"); let pwd = ref("123456"); const onLogin = (): void => { - storageSession.setItem("info", { - username: "admin", - accessToken: "eyJhbGciOiJIUzUxMiJ9.test" - }); - initRouter("admin").then(() => {}); - router.push("/"); + useUserStoreHook() + .loginByUsername({ username: user.value, password: pwd.value }) + .then(async (res: any) => { + storageSession.setItem("info", { + username: res.username, + accessToken: res.accessToken + }); + usePermissionStoreHook().clearAllCachePage(); + initRouter(res.username); + router.push("/"); + }); }; function onUserFocus() {