From a4fe3987c3160cc2e98fae30b9fb237f12efcf16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=89=E6=9D=A5=E6=8A=80=E6=9C=AF?= <1490493387@qq.com> Date: Thu, 18 Nov 2021 23:57:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(user.ts):=E7=94=A8=E6=88=B7=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/login/index.ts | 45 +++++++++++++++++++++++++++ src/store/interface.ts | 15 +++++++++ src/store/modules/user.ts | 64 +++++++++++++++++++++++++++++++++------ src/utils/request.ts | 8 ++--- 4 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 src/api/login/index.ts create mode 100644 src/store/interface.ts diff --git a/src/api/login/index.ts b/src/api/login/index.ts new file mode 100644 index 00000000..58e39036 --- /dev/null +++ b/src/api/login/index.ts @@ -0,0 +1,45 @@ +import request from "@utils/request"; + +/** + * 登录 + * @param data + */ +export function login(data: object) { + return request({ + url: '/youlai-auth/oauth/token', + params: data, + headers: { + 'Authorization': 'Basic bWFsbC1hZG1pbi13ZWI6MTIzNDU2' // 客户端信息加密摘要认证,明文:mall-admin-web:123456 + } + }) +} + +/** + * 登录成功后获取用户信息(包括用户头像、权限列表等) + */ +export function getUserInfo() { + return request({ + url: '/youlai-admin/api/v1/users/me', + method: 'get' + }) +} + +/** + * 注销 + */ +export function logout() { + return request({ + url: '/youlai-auth/oauth/logout', + method: 'delete' + }) +} + +/** + * 获取图片验证码 + */ +export function getCaptcha() { + return request({ + url: '/captcha', + method: 'get' + }) +} \ No newline at end of file diff --git a/src/store/interface.ts b/src/store/interface.ts new file mode 100644 index 00000000..b90ea070 --- /dev/null +++ b/src/store/interface.ts @@ -0,0 +1,15 @@ +// 接口类型声明 +export interface UserState { + token: string, + name: string, + avatar: string, + introduction: string, + roles: string[], + perms: string[] +} + + +// 顶级类型声明 +export interface RootStateTypes { + user: UserState +} \ No newline at end of file diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index ed7232f0..632d168c 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -1,11 +1,57 @@ import {Module} from "vuex"; +import {UserState, RootStateTypes} from "@store/interface"; +import {Local} from "@utils/storage"; +import {login} from "@api/login" +import {rejects} from "assert"; + +const userModule: Module = { + namespaced: true, + state: { + token: Local.get('token') || '', + name: '', + avatar: '', + introduction: '', + roles: [], + perms: [] + }, + mutations: { + SET_TOKEN(state: UserState, token: string) { + state.token = token + }, + SET_NAME(state: UserState, name: string) { + state.name = name + }, + SET_AVATAR(state: UserState, avatar: string) { + state.avatar = avatar + }, + SET_INTRODUCTION(state: UserState, introduction: string) { + state.introduction = introduction + }, + SET_ROLES(state: UserState, roles: string[]) { + state.roles = roles + }, + SET_PERMS(state: UserState, perms: string[]) { + state.perms = perms + } + }, + actions: { + // 登录 + login({commit}, userInfo: { username: string, password: string }) { + const {username, password} = userInfo + return new Promise((resolve, reject) => { + login({ username: username.trim(), password: password }).then(response => { + const {access_token, token_type} = response.data + const accessToken = token_type + " " + access_token + Local.set("token",accessToken) + commit('SET_TOKEN',accessToken) + }).catch(error => { + reject(error) + }) + }) + } + + } +} + +export default userModule; -// vuex的Module使用 https://blog.csdn.net/fanweilin0123/article/details/109903447 -export interface State { - token:string, - name:string, - avatar:string, - introduction:string, - roles:string[], - perms:string[] -} \ No newline at end of file diff --git a/src/utils/request.ts b/src/utils/request.ts index 37c70a51..747fc5e0 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,6 +1,6 @@ import axios from "axios"; import {ElMessage, ElMessageBox} from "element-plus"; -import {Session} from "@utils/storage"; +import {Local} from "@utils/storage"; // 创建 axios 实例 @@ -16,9 +16,7 @@ service.interceptors.request.use( if (!config?.headers) { throw new Error(`Expected 'config' and 'config.headers' not to be undefined`); } - if (Session.get('token')) { - config.headers.Authorization = `${Session.get('token')}`; - } + config.headers.Authorization = `${Local.get('token')}`; }, (error) => { return Promise.reject(error); @@ -43,7 +41,7 @@ service.interceptors.response.use( (error) => { const {code, msg} = error.response.data if (code === 'A0230') { // token 过期 - Session.clear(); // 清除浏览器全部临时缓存 + Local.clear(); // 清除浏览器全部缓存 window.location.href = '/'; // 跳转登录页 ElMessageBox.alert('当前页面已失效,请重新登录', '提示', {}) .then(() => {