fix: 登录接口严格按swagger适配
This commit is contained in:
@@ -8,23 +8,14 @@ import request from '@/utils/http'
|
|||||||
export function fetchLogin(params: Api.Auth.LoginParams) {
|
export function fetchLogin(params: Api.Auth.LoginParams) {
|
||||||
return request.post<Api.Auth.LoginResponse>({
|
return request.post<Api.Auth.LoginResponse>({
|
||||||
url: '/api/admin/v1/auth/login',
|
url: '/api/admin/v1/auth/login',
|
||||||
params
|
params,
|
||||||
|
// 登录不应携带租户 Header(避免历史租户残留导致登录失败)
|
||||||
|
skipTenantHeader: true
|
||||||
// showSuccessMessage: true // 显示成功消息
|
// showSuccessMessage: true // 显示成功消息
|
||||||
// showErrorMessage: false // 不显示错误消息
|
// showErrorMessage: false // 不显示错误消息
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 免租户号登录(仅账号+密码)
|
|
||||||
*/
|
|
||||||
export function fetchLoginSimple(params: Api.Auth.LoginParams) {
|
|
||||||
return request.post<Api.Auth.LoginResponse>({
|
|
||||||
url: '/api/admin/v1/auth/login/simple',
|
|
||||||
params,
|
|
||||||
skipTenantHeader: true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户信息
|
* 获取用户信息
|
||||||
* @returns 用户信息
|
* @returns 用户信息
|
||||||
|
|||||||
3
src/types/api/auth.d.ts
vendored
3
src/types/api/auth.d.ts
vendored
@@ -3,7 +3,8 @@ declare namespace Api {
|
|||||||
namespace Auth {
|
namespace Auth {
|
||||||
/** 登录参数 */
|
/** 登录参数 */
|
||||||
interface LoginParams {
|
interface LoginParams {
|
||||||
account: string
|
accountName: string
|
||||||
|
phone: string
|
||||||
password: string
|
password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ const storageKeyManager = new StorageKeyManager()
|
|||||||
const REMEMBER_LOGIN_KEY = storageKeyManager.getStorageKey(REMEMBER_LOGIN_STORE_ID)
|
const REMEMBER_LOGIN_KEY = storageKeyManager.getStorageKey(REMEMBER_LOGIN_STORE_ID)
|
||||||
|
|
||||||
export interface RememberLoginPayload {
|
export interface RememberLoginPayload {
|
||||||
account: string
|
accountName: string
|
||||||
phone?: string
|
phone: string
|
||||||
password: string
|
password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,11 +25,11 @@ export interface RememberLoginPayload {
|
|||||||
* 保存加密后的登录信息
|
* 保存加密后的登录信息
|
||||||
*/
|
*/
|
||||||
export const saveRememberLogin = (payload: RememberLoginPayload) => {
|
export const saveRememberLogin = (payload: RememberLoginPayload) => {
|
||||||
if (!payload.account || !payload.password) return
|
if (!payload.accountName || !payload.phone || !payload.password) return
|
||||||
|
|
||||||
const encryptedPassword = CryptoJS.AES.encrypt(payload.password, ENCRYPT_KEY).toString()
|
const encryptedPassword = CryptoJS.AES.encrypt(payload.password, ENCRYPT_KEY).toString()
|
||||||
const data = {
|
const data = {
|
||||||
account: payload.account,
|
accountName: payload.accountName,
|
||||||
phone: payload.phone,
|
phone: payload.phone,
|
||||||
password: encryptedPassword,
|
password: encryptedPassword,
|
||||||
updatedAt: Date.now()
|
updatedAt: Date.now()
|
||||||
@@ -47,18 +47,21 @@ export const loadRememberLogin = (): RememberLoginPayload | null => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(storedValue) as {
|
const parsed = JSON.parse(storedValue) as {
|
||||||
account?: string
|
accountName?: string
|
||||||
phone?: string
|
phone?: string
|
||||||
password?: string
|
password?: string
|
||||||
}
|
}
|
||||||
if (!parsed.account || !parsed.password) return null
|
if (!parsed.accountName || !parsed.phone || !parsed.password) {
|
||||||
|
clearRememberLogin()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const decryptedPassword = CryptoJS.AES.decrypt(parsed.password, ENCRYPT_KEY).toString(
|
const decryptedPassword = CryptoJS.AES.decrypt(parsed.password, ENCRYPT_KEY).toString(
|
||||||
CryptoJS.enc.Utf8
|
CryptoJS.enc.Utf8
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
account: parsed.account,
|
accountName: parsed.accountName,
|
||||||
phone: parsed.phone,
|
phone: parsed.phone,
|
||||||
password: decryptedPassword
|
password: decryptedPassword
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@
|
|||||||
import { useUserStore } from '@/store/modules/user'
|
import { useUserStore } from '@/store/modules/user'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import { HttpError } from '@/utils/http/error'
|
import { HttpError } from '@/utils/http/error'
|
||||||
import { fetchLoginSimple } from '@/api/auth'
|
import { fetchLogin } from '@/api/auth'
|
||||||
import { ElNotification, type FormInstance, type FormRules } from 'element-plus'
|
import { ElNotification, type FormInstance, type FormRules } from 'element-plus'
|
||||||
import {
|
import {
|
||||||
clearRememberLogin,
|
clearRememberLogin,
|
||||||
@@ -153,15 +153,8 @@
|
|||||||
// 1. 读取记住的账号、手机号与密码
|
// 1. 读取记住的账号、手机号与密码
|
||||||
const rememberedLogin = loadRememberLogin()
|
const rememberedLogin = loadRememberLogin()
|
||||||
if (rememberedLogin) {
|
if (rememberedLogin) {
|
||||||
// 1.1 兼容旧数据:若 account 内包含 @ 且 phone 未存储,则拆分回填
|
formData.account = rememberedLogin.accountName
|
||||||
if (!rememberedLogin.phone && rememberedLogin.account.includes('@')) {
|
formData.phone = rememberedLogin.phone
|
||||||
const lastIndex = rememberedLogin.account.lastIndexOf('@')
|
|
||||||
formData.account = rememberedLogin.account.slice(0, lastIndex)
|
|
||||||
formData.phone = rememberedLogin.account.slice(lastIndex + 1)
|
|
||||||
} else {
|
|
||||||
formData.account = rememberedLogin.account
|
|
||||||
formData.phone = rememberedLogin.phone || ''
|
|
||||||
}
|
|
||||||
formData.password = rememberedLogin.password
|
formData.password = rememberedLogin.password
|
||||||
formData.rememberPassword = true
|
formData.rememberPassword = true
|
||||||
} else {
|
} else {
|
||||||
@@ -181,21 +174,21 @@
|
|||||||
// 2. 滑块验证(当前已禁用,直接通过)
|
// 2. 滑块验证(当前已禁用,直接通过)
|
||||||
isPassing.value = true
|
isPassing.value = true
|
||||||
|
|
||||||
// 3. 读取表单数据并拼接账号
|
// 3. 读取表单数据
|
||||||
const { account, phone, password, rememberPassword } = formData
|
const { account: accountName, phone, password, rememberPassword } = formData
|
||||||
const fullAccount = `${account}@${phone}`
|
|
||||||
loading.value = true
|
loading.value = true
|
||||||
|
|
||||||
// 4. 处理本地记住信息
|
// 4. 处理本地记住信息
|
||||||
if (rememberPassword && account && phone && password) {
|
if (rememberPassword && accountName && phone && password) {
|
||||||
saveRememberLogin({ account, phone, password })
|
saveRememberLogin({ accountName, phone, password })
|
||||||
} else {
|
} else {
|
||||||
clearRememberLogin()
|
clearRememberLogin()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 登录请求
|
// 5. 登录请求
|
||||||
const { accessToken, refreshToken, user } = await fetchLoginSimple({
|
const { accessToken, refreshToken, user } = await fetchLogin({
|
||||||
account: fullAccount,
|
accountName,
|
||||||
|
phone,
|
||||||
password
|
password
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user