feat: modify login logic

This commit is contained in:
chen.home 2023-08-25 23:43:48 +08:00
parent 14a1c9c950
commit 35dbbe8711
7 changed files with 30 additions and 39 deletions

View File

@ -8,7 +8,7 @@ const token = () => Random.string('upper', 32, 32)
const userData = [ const userData = [
{ {
userId: '1', userId: '1',
userName: 'super', username: 'super',
password: '123456', password: '123456',
nickName: '超级管理员大人', nickName: '超级管理员大人',
avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg', avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg',
@ -16,7 +16,7 @@ const userData = [
}, },
{ {
userId: '2', userId: '2',
userName: 'admin', username: 'admin',
password: '123456', password: '123456',
nickName: '管理员大人', nickName: '管理员大人',
avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg', avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg',
@ -24,7 +24,7 @@ const userData = [
}, },
{ {
userId: '3', userId: '3',
userName: 'user', username: 'user',
password: '123456', password: '123456',
nickName: '用户大人', nickName: '用户大人',
avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg', avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg',
@ -437,23 +437,23 @@ const userRoutes = [
export default [ export default [
{ {
url: '/mock/login', url: '/mock/auth/login',
method: 'post', method: 'post',
response: (options: Service.MockOption) => { 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, '账号密码不全') 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) { if (userInfo) {
return { return {
code: 200, code: 200,
message: 'ok', message: 'ok',
data: { data: {
userId: userInfo.userId, id: userInfo.userId,
token: token(), accessToken: token(),
refreshToken: token(), refreshToken: token(),
}, },
} }

View File

@ -1,21 +1,15 @@
/** 不同请求服务的环境配置 */ /** 不同请求服务的环境配置 */
export const proxyConfig: Record<ServiceEnvType, ServiceEnvConfig> = { export const proxyConfig: Record<ServiceEnvType, ServiceEnvConfig> = {
dev: { dev: {
url: 'https://mock.mengxuegu.com/mock/61e4df7c17249f68847fc191/api', url: 'http://localhost:3000',
urlPattern: '/url-pattern', urlPattern: '/url-pattern',
secondUrl: 'http://localhost:3000',
secondUrlPattern: '/second-url-pattern',
}, },
test: { test: {
url: 'http://localhost:8080', url: 'http://localhost:8080',
urlPattern: '/url-pattern', urlPattern: '/url-pattern',
secondUrl: 'http://localhost:8081',
secondUrlPattern: '/second-url-pattern',
}, },
prod: { prod: {
url: 'http://localhost:8080', url: 'http://localhost:8080',
urlPattern: '/url-pattern', urlPattern: '/url-pattern',
secondUrl: 'http://localhost:8081',
secondUrlPattern: '/second-url-pattern',
}, },
} }

View File

@ -1,12 +1,12 @@
import { devRequest, mockRequest } from '../http' import { mockRequest } from '../http'
interface Ilogin { interface Ilogin {
userName: string username: string
password: string password: string
} }
export function fetchLogin(params: Ilogin) { export function fetchLogin(params: Ilogin) {
return devRequest.post<any>('/auth/login', params) return mockRequest.post<any>('/auth/login', params)
} }
export function fetchUpdateToken(params: any) { export function fetchUpdateToken(params: any) {
return mockRequest.post<ApiAuth.loginToken>('/updateToken', params) return mockRequest.post<ApiAuth.loginToken>('/updateToken', params)

View File

@ -1,17 +1,12 @@
import { createRequest } from './request' import { createRequest } from './request'
import { proxyConfig } from '@/config' import { proxyConfig } from '@/config'
const { url, urlPattern, secondUrl, secondUrlPattern } const { url, urlPattern } = proxyConfig[import.meta.env.MODE]
= proxyConfig[import.meta.env.MODE]
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y' || false const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y' || false
export const request = createRequest({ export const request = createRequest({
baseURL: isHttpProxy ? urlPattern : url, baseURL: isHttpProxy ? urlPattern : url,
})
export const devRequest = createRequest({
baseURL: isHttpProxy ? secondUrlPattern : secondUrl,
}, { }, {
msgKey: 'message', msgKey: 'message',
}) })

View File

@ -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 this.loginLoading = true
const { error, data } = await fetchLogin({ userName, password }) const { error, data } = await fetchLogin({ username, password })
if (error) { if (error) {
this.loginLoading = false this.loginLoading = false
return return
@ -88,17 +88,17 @@ export const useAuthStore = defineStore('auth-store', {
}, },
/* 缓存用户信息 */ /* 缓存用户信息 */
async catchUserInfo(userToken: ApiAuth.loginToken) { async catchUserInfo(userInfo: ApiAuth.loginToken) {
let catchSuccess = false let catchSuccess = false
const { token, refreshToken, userId } = userToken const { accessToken, refreshToken, id } = userInfo
const { error, data } = await fetchUserInfo({ userId }) const { error, data } = await fetchUserInfo({ userId: id })
if (error) if (error)
return catchSuccess return catchSuccess
// 先存储token // 先存储token
local.set('token', token) local.set('token', accessToken)
local.set('refreshToken', refreshToken) local.set('refreshToken', refreshToken)
this.token = token this.token = accessToken
this.refreshToken = refreshToken this.refreshToken = refreshToken
// 请求/存储用户信息 // 请求/存储用户信息
local.set('userInfo', data) local.set('userInfo', data)

12
src/typings/api.d.ts vendored
View File

@ -6,9 +6,15 @@ declare namespace ApiAuth {
type UserInfo = Auth.UserInfo; type UserInfo = Auth.UserInfo;
/* 登录token字段 */ /* 登录token字段 */
interface loginToken { interface loginToken {
token: string; accessToken: string;
refreshToken: string; avatar?: string;
userId: number; email?: string;
id: number;
nickname?: string;
notes?: string;
refreshToken: string;
tel?: string;
username: string;
} }
} }
declare namespace CommonList { declare namespace CommonList {

View File

@ -12,10 +12,6 @@ interface ServiceEnvConfig {
url: string url: string
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */ /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
urlPattern: '/url-pattern' urlPattern: '/url-pattern'
/** 另一个后端请求地址(有多个不同的后端服务时) */
secondUrl: string
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
secondUrlPattern: '/second-url-pattern'
} }
interface ImportMetaEnv { interface ImportMetaEnv {
/** 项目基本地址 */ /** 项目基本地址 */