From 35dbbe8711ae552b1063f84b37e8004323e44abb Mon Sep 17 00:00:00 2001 From: "chen.home" Date: Fri, 25 Aug 2023 23:43:48 +0800 Subject: [PATCH] feat: modify login logic --- mock/module/user.ts | 18 +++++++++--------- src/config/env.ts | 8 +------- src/service/api/login.ts | 6 +++--- src/service/http/index.ts | 7 +------ src/store/modules/auth.ts | 14 +++++++------- src/typings/api.d.ts | 12 +++++++++--- src/typings/env.d.ts | 4 ---- 7 files changed, 30 insertions(+), 39 deletions(-) diff --git a/mock/module/user.ts b/mock/module/user.ts index c90abad..74f7d20 100644 --- a/mock/module/user.ts +++ b/mock/module/user.ts @@ -8,7 +8,7 @@ const token = () => Random.string('upper', 32, 32) const userData = [ { userId: '1', - userName: 'super', + username: 'super', password: '123456', nickName: '超级管理员大人', avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg', @@ -16,7 +16,7 @@ const userData = [ }, { userId: '2', - userName: 'admin', + username: 'admin', password: '123456', nickName: '管理员大人', avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg', @@ -24,7 +24,7 @@ const userData = [ }, { userId: '3', - userName: 'user', + username: 'user', password: '123456', nickName: '用户大人', avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg', @@ -437,23 +437,23 @@ const userRoutes = [ export default [ { - url: '/mock/login', + url: '/mock/auth/login', method: 'post', response: (options: Service.MockOption) => { - const { userName = undefined, password = undefined } = options.body + const { username = undefined, password = undefined } = options.body - if (!userName || !password) + if (!username || !password) return resultFailed(null, '账号密码不全') - const userInfo = userData.find(item => item.userName === userName && item.password === password) + const userInfo = userData.find(item => item.username === username && item.password === password) if (userInfo) { return { code: 200, message: 'ok', data: { - userId: userInfo.userId, - token: token(), + id: userInfo.userId, + accessToken: token(), refreshToken: token(), }, } diff --git a/src/config/env.ts b/src/config/env.ts index ad6f7c1..eb8e359 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -1,21 +1,15 @@ /** 不同请求服务的环境配置 */ export const proxyConfig: Record = { dev: { - url: 'https://mock.mengxuegu.com/mock/61e4df7c17249f68847fc191/api', + url: 'http://localhost:3000', urlPattern: '/url-pattern', - secondUrl: 'http://localhost:3000', - secondUrlPattern: '/second-url-pattern', }, test: { url: 'http://localhost:8080', urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern', }, prod: { url: 'http://localhost:8080', urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern', }, } diff --git a/src/service/api/login.ts b/src/service/api/login.ts index ad2c95d..f1e9013 100644 --- a/src/service/api/login.ts +++ b/src/service/api/login.ts @@ -1,12 +1,12 @@ -import { devRequest, mockRequest } from '../http' +import { mockRequest } from '../http' interface Ilogin { - userName: string + username: string password: string } export function fetchLogin(params: Ilogin) { - return devRequest.post('/auth/login', params) + return mockRequest.post('/auth/login', params) } export function fetchUpdateToken(params: any) { return mockRequest.post('/updateToken', params) diff --git a/src/service/http/index.ts b/src/service/http/index.ts index ba6d8b5..54abdba 100644 --- a/src/service/http/index.ts +++ b/src/service/http/index.ts @@ -1,17 +1,12 @@ import { createRequest } from './request' import { proxyConfig } from '@/config' -const { url, urlPattern, secondUrl, secondUrlPattern } - = proxyConfig[import.meta.env.MODE] +const { url, urlPattern } = proxyConfig[import.meta.env.MODE] const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y' || false export const request = createRequest({ baseURL: isHttpProxy ? urlPattern : url, -}) - -export const devRequest = createRequest({ - baseURL: isHttpProxy ? secondUrlPattern : secondUrl, }, { msgKey: 'message', }) diff --git a/src/store/modules/auth.ts b/src/store/modules/auth.ts index bb34e59..674d715 100644 --- a/src/store/modules/auth.ts +++ b/src/store/modules/auth.ts @@ -47,9 +47,9 @@ export const useAuthStore = defineStore('auth-store', { }, /* 用户登录 */ - async login(userName: string, password: string) { + async login(username: string, password: string) { this.loginLoading = true - const { error, data } = await fetchLogin({ userName, password }) + const { error, data } = await fetchLogin({ username, password }) if (error) { this.loginLoading = false return @@ -88,17 +88,17 @@ export const useAuthStore = defineStore('auth-store', { }, /* 缓存用户信息 */ - async catchUserInfo(userToken: ApiAuth.loginToken) { + async catchUserInfo(userInfo: ApiAuth.loginToken) { let catchSuccess = false - const { token, refreshToken, userId } = userToken - const { error, data } = await fetchUserInfo({ userId }) + const { accessToken, refreshToken, id } = userInfo + const { error, data } = await fetchUserInfo({ userId: id }) if (error) return catchSuccess // 先存储token - local.set('token', token) + local.set('token', accessToken) local.set('refreshToken', refreshToken) - this.token = token + this.token = accessToken this.refreshToken = refreshToken // 请求/存储用户信息 local.set('userInfo', data) diff --git a/src/typings/api.d.ts b/src/typings/api.d.ts index 0438007..814bfc3 100644 --- a/src/typings/api.d.ts +++ b/src/typings/api.d.ts @@ -6,9 +6,15 @@ declare namespace ApiAuth { type UserInfo = Auth.UserInfo; /* 登录token字段 */ interface loginToken { - token: string; - refreshToken: string; - userId: number; + accessToken: string; + avatar?: string; + email?: string; + id: number; + nickname?: string; + notes?: string; + refreshToken: string; + tel?: string; + username: string; } } declare namespace CommonList { diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index a57b065..5ac69f1 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -12,10 +12,6 @@ interface ServiceEnvConfig { url: string /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */ urlPattern: '/url-pattern' - /** 另一个后端请求地址(有多个不同的后端服务时) */ - secondUrl: string - /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */ - secondUrlPattern: '/second-url-pattern' } interface ImportMetaEnv { /** 项目基本地址 */