fix: 修复无法登录问题

This commit is contained in:
kailong321200875 2024-01-10 09:43:33 +08:00
parent 9b2b4d42a6
commit 8ce00ab247
4 changed files with 13 additions and 14 deletions

View File

@ -1,8 +1,9 @@
import { AxiosResponse, InternalAxiosRequestConfig } from './types'
import { ElMessage } from 'element-plus'
import qs from 'qs'
import { SUCCESS_CODE } from '@/constants'
import { SUCCESS_CODE, TRANSFORM_REQUEST_DATA } from '@/constants'
import { useUserStoreWithOut } from '@/store/modules/user'
import { objToFormData } from '@/utils'
const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
if (
@ -10,6 +11,12 @@ const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
config.headers['Content-Type'] === 'application/x-www-form-urlencoded'
) {
config.data = qs.stringify(config.data)
} else if (
TRANSFORM_REQUEST_DATA &&
config.method === 'post' &&
config.headers['Content-Type'] === 'multipart/form-data'
) {
config.data = objToFormData(config.data)
}
if (config.method === 'get' && config.params) {
let url = config.url as string

View File

@ -1,21 +1,16 @@
import service from './service'
import { CONTENT_TYPE, TRANSFORM_REQUEST_DATA } from '@/constants'
import { CONTENT_TYPE } from '@/constants'
import { useUserStoreWithOut } from '@/store/modules/user'
import { objToFormData } from '@/utils'
const request = (option: AxiosConfig) => {
const { url, method, params, data, headers, responseType } = option
// 是否需要转换数据格式
const transformData =
TRANSFORM_REQUEST_DATA &&
(headers?.['Content-Type'] || CONTENT_TYPE) === 'multipart/form-data' &&
data
const userStore = useUserStoreWithOut()
return service.request({
url: url,
method,
params,
data: transformData ? objToFormData(data) : data,
data: data,
responseType: responseType,
headers: {
'Content-Type': CONTENT_TYPE,

View File

@ -6,11 +6,7 @@ export const SUCCESS_CODE = 0
/**
* contentType
*/
export const CONTENT_TYPE:
| 'application/json'
| 'multipart/form-data'
| 'application/x-www-form-urlencoded'
| 'text/plain' = 'multipart/form-data'
export const CONTENT_TYPE: AxiosContentType = 'application/json'
/**
*

1
types/global.d.ts vendored
View File

@ -30,6 +30,7 @@ declare global {
| 'application/json'
| 'application/x-www-form-urlencoded'
| 'multipart/form-data'
| 'text/plain'
declare type AxiosMethod = 'get' | 'post' | 'delete' | 'put'