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

View File

@ -1,21 +1,16 @@
import service from './service' import service from './service'
import { CONTENT_TYPE, TRANSFORM_REQUEST_DATA } from '@/constants' import { CONTENT_TYPE } from '@/constants'
import { useUserStoreWithOut } from '@/store/modules/user' import { useUserStoreWithOut } from '@/store/modules/user'
import { objToFormData } from '@/utils'
const request = (option: AxiosConfig) => { const request = (option: AxiosConfig) => {
const { url, method, params, data, headers, responseType } = option 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() const userStore = useUserStoreWithOut()
return service.request({ return service.request({
url: url, url: url,
method, method,
params, params,
data: transformData ? objToFormData(data) : data, data: data,
responseType: responseType, responseType: responseType,
headers: { headers: {
'Content-Type': CONTENT_TYPE, 'Content-Type': CONTENT_TYPE,

View File

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

1
types/global.d.ts vendored
View File

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