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