mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-04-05 19:41:59 +08:00
feat: modify login logic
This commit is contained in:
parent
14a1c9c950
commit
35dbbe8711
@ -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(),
|
||||
},
|
||||
}
|
||||
|
@ -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',
|
||||
},
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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',
|
||||
})
|
||||
|
@ -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)
|
||||
|
10
src/typings/api.d.ts
vendored
10
src/typings/api.d.ts
vendored
@ -6,9 +6,15 @@ declare namespace ApiAuth {
|
||||
type UserInfo = Auth.UserInfo;
|
||||
/* 登录token字段 */
|
||||
interface loginToken {
|
||||
token: string;
|
||||
accessToken: string;
|
||||
avatar?: string;
|
||||
email?: string;
|
||||
id: number;
|
||||
nickname?: string;
|
||||
notes?: string;
|
||||
refreshToken: string;
|
||||
userId: number;
|
||||
tel?: string;
|
||||
username: string;
|
||||
}
|
||||
}
|
||||
declare namespace CommonList {
|
||||
|
4
src/typings/env.d.ts
vendored
4
src/typings/env.d.ts
vendored
@ -12,10 +12,6 @@ interface ServiceEnvConfig {
|
||||
url: string
|
||||
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
|
||||
urlPattern: '/url-pattern'
|
||||
/** 另一个后端请求地址(有多个不同的后端服务时) */
|
||||
secondUrl: string
|
||||
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
|
||||
secondUrlPattern: '/second-url-pattern'
|
||||
}
|
||||
interface ImportMetaEnv {
|
||||
/** 项目基本地址 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user