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()
export const getCodeApi = () => {
return request.get<IResponse<string>>({ url: 'user/captcha' })
export const getCodeApi = async () => {
return (await request.get<IResponse<string>>({ url: 'user/captcha' })).data
}
export const registerApi = (data: Omit<IUserModel, 'is_admin'>) => {
return request.post<IResponse<IUserModel>>({ url: 'user/register', data })
export const registerApi = async (data: Omit<IUserModel, 'is_admin'>) => {
return (await request.post<IResponse<IUserModel>>({ url: 'user/register', data })).data
}

View File

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

View File

@ -6,7 +6,7 @@ import { config } from '@/config/axios/config'
const { default_headers } = config
const request = <T>(option: AxiosConfig): AxiosPromise<T> => {
const request = (option: AxiosConfig) => {
const { url, method, params, data, headersType, responseType } = option
return service({
url: url,
@ -21,19 +21,19 @@ const request = <T>(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> {
return request<T>({ method: 'post', ...option })
return request({ method: 'post', ...option })
}
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> {
return request<T>({ method: 'put', ...option })
return request({ method: 'put', ...option })
}
export const useAxios = () => {

View File

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