fix: 修复axios已知问题

This commit is contained in:
kailong321200875 2022-06-24 19:58:18 +08:00
parent f4c940d95e
commit 537af57a0a
4 changed files with 15 additions and 22 deletions

View File

@ -3,10 +3,10 @@ import { IUserModel } from '@/api-types/user'
const request = useAxios() const request = useAxios()
export const getCodeApi = () => { export const getCodeApi = async () => {
return request.get<IResponse<string>>({ url: 'user/captcha' }) return (await request.get<IResponse<string>>({ url: 'user/captcha' })).data
} }
export const registerApi = (data: Omit<IUserModel, 'is_admin'>) => { export const registerApi = async (data: Omit<IUserModel, 'is_admin'>) => {
return request.post<IResponse<IUserModel>>({ url: 'user/register', data }) return (await request.post<IResponse<IUserModel>>({ url: 'user/register', data })).data
} }

View File

@ -1,10 +1,4 @@
import axios, { import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
AxiosInstance,
AxiosRequestConfig,
AxiosRequestHeaders,
AxiosResponse,
AxiosError
} from 'axios'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
@ -27,8 +21,7 @@ service.interceptors.request.use(
(config: AxiosRequestConfig) => { (config: AxiosRequestConfig) => {
if ( if (
config.method === 'post' && config.method === 'post' &&
(config.headers as AxiosRequestHeaders)['Content-Type'] === config!.headers!['Content-Type'] === 'application/x-www-form-urlencoded'
'application/x-www-form-urlencoded'
) { ) {
config.data = qs.stringify(config.data) config.data = qs.stringify(config.data)
} }

View File

@ -6,7 +6,7 @@ import { config } from '@/config/axios/config'
const { default_headers } = config const { default_headers } = config
const request = <T>(option: AxiosConfig): AxiosPromise<T> => { const request = (option: AxiosConfig) => {
const { url, method, params, data, headersType, responseType } = option const { url, method, params, data, headersType, responseType } = option
return service({ return service({
url: url, url: url,
@ -21,19 +21,19 @@ const request = <T>(option: AxiosConfig): AxiosPromise<T> => {
} }
function getFn<T = any>(option: AxiosConfig): AxiosPromise<T> { function getFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
return request<T>({ method: 'get', ...option }) return request({ method: 'get', ...option })
} }
function postFn<T = any>(option: AxiosConfig): AxiosPromise<T> { function postFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
return request<T>({ method: 'post', ...option }) return request({ method: 'post', ...option })
} }
function deleteFn<T = any>(option: AxiosConfig): AxiosPromise<T> { function deleteFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
return request<T>({ method: 'delete', ...option }) return request({ method: 'delete', ...option })
} }
function putFn<T = any>(option: AxiosConfig): AxiosPromise<T> { function putFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
return request<T>({ method: 'put', ...option }) return request({ method: 'put', ...option })
} }
export const useAxios = () => { export const useAxios = () => {

View File

@ -131,8 +131,8 @@ const toLogin = () => {
const codeUrl = ref('') const codeUrl = ref('')
const getCode = async () => { const getCode = async () => {
const { data } = await getCodeApi() const { result } = await getCodeApi()
codeUrl.value = data.result codeUrl.value = result
} }
getCode() getCode()
@ -145,8 +145,8 @@ const loginRegister = async () => {
try { try {
loading.value = true loading.value = true
const formData = await getFormData<Omit<IUserModel, 'is_admin'>>() const formData = await getFormData<Omit<IUserModel, 'is_admin'>>()
const { data } = await registerApi(formData) const { result } = await registerApi(formData)
if (data) { if (result) {
ElMessage.success('注册成功') ElMessage.success('注册成功')
toLogin() toLogin()
} }