Skip to content

Commit

Permalink
feat: modify login logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chansee97 committed Aug 25, 2023
1 parent 14a1c9c commit 35dbbe8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 39 deletions.
18 changes: 9 additions & 9 deletions mock/module/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ 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',
role: 'super',
},
{
userId: '2',
userName: 'admin',
username: 'admin',
password: '123456',
nickName: '管理员大人',
avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg',
role: 'admin',
},
{
userId: '3',
userName: 'user',
username: 'user',
password: '123456',
nickName: '用户大人',
avatar: 'https://z3.ax1x.com/2021/10/29/5jnWgf.jpg',
Expand Down Expand Up @@ -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(),
},
}
Expand Down
8 changes: 1 addition & 7 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
/** 不同请求服务的环境配置 */
export const proxyConfig: Record<ServiceEnvType, ServiceEnvConfig> = {
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',
},
}
6 changes: 3 additions & 3 deletions src/service/api/login.ts
Original file line number Diff line number Diff line change
@@ -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<any>('/auth/login', params)
return mockRequest.post<any>('/auth/login', params)
}
export function fetchUpdateToken(params: any) {
return mockRequest.post<ApiAuth.loginToken>('/updateToken', params)
Expand Down
7 changes: 1 addition & 6 deletions src/service/http/index.ts
Original file line number Diff line number Diff line change
@@ -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',
})
Expand Down
14 changes: 7 additions & 7 deletions src/store/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/typings/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions src/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ interface ServiceEnvConfig {
url: string
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
urlPattern: '/url-pattern'
/** 另一个后端请求地址(有多个不同的后端服务时) */
secondUrl: string
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
secondUrlPattern: '/second-url-pattern'
}
interface ImportMetaEnv {
/** 项目基本地址 */
Expand Down

0 comments on commit 35dbbe8

Please sign in to comment.