refactor: refactor axios
parent
c589edd960
commit
0980640f65
@ -1,15 +1,11 @@
|
|||||||
import { useAxios } from '@/hooks/web/useAxios'
|
import request from '@/config/axios'
|
||||||
|
|
||||||
const request = useAxios()
|
|
||||||
|
|
||||||
// 获取所有字典
|
// 获取所有字典
|
||||||
export const getDictApi = async (): Promise<IResponse> => {
|
export const getDictApi = (): Promise<IResponse> => {
|
||||||
const res = await request.get({ url: '/dict/list' })
|
return request.get({ url: '/dict/list' })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 模拟获取某个字典
|
// 模拟获取某个字典
|
||||||
export const getDictOneApi = async (): Promise<IResponse> => {
|
export const getDictOneApi = async (): Promise<IResponse> => {
|
||||||
const res = await request.get({ url: '/dict/one' })
|
return request.get({ url: '/dict/one' })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,29 +1,22 @@
|
|||||||
import { useAxios } from '@/hooks/web/useAxios'
|
import request from '@/config/axios'
|
||||||
import type { WorkplaceTotal, Project, Dynamic, Team, RadarData } from './types'
|
import type { WorkplaceTotal, Project, Dynamic, Team, RadarData } from './types'
|
||||||
|
|
||||||
const request = useAxios()
|
export const getCountApi = (): Promise<IResponse<WorkplaceTotal>> => {
|
||||||
|
return request.get({ url: '/workplace/total' })
|
||||||
export const getCountApi = async (): Promise<IResponse<WorkplaceTotal>> => {
|
|
||||||
const res = await request.get({ url: '/workplace/total' })
|
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getProjectApi = async (): Promise<IResponse<Project>> => {
|
export const getProjectApi = (): Promise<IResponse<Project>> => {
|
||||||
const res = await request.get({ url: '/workplace/project' })
|
return request.get({ url: '/workplace/project' })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getDynamicApi = async (): Promise<IResponse<Dynamic[]>> => {
|
export const getDynamicApi = (): Promise<IResponse<Dynamic[]>> => {
|
||||||
const res = await request.get({ url: '/workplace/dynamic' })
|
return request.get({ url: '/workplace/dynamic' })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getTeamApi = async (): Promise<IResponse<Team[]>> => {
|
export const getTeamApi = (): Promise<IResponse<Team[]>> => {
|
||||||
const res = await request.get({ url: '/workplace/team' })
|
return request.get({ url: '/workplace/team' })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getRadarApi = async (): Promise<IResponse<RadarData[]>> => {
|
export const getRadarApi = (): Promise<IResponse<RadarData[]>> => {
|
||||||
const res = await request.get({ url: '/workplace/radar' })
|
return request.get({ url: '/workplace/radar' })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,18 @@
|
|||||||
import { useAxios } from '@/hooks/web/useAxios'
|
import request from '@/config/axios'
|
||||||
import type { TableData } from './types'
|
import type { TableData } from './types'
|
||||||
|
|
||||||
const request = useAxios()
|
export const getTableListApi = (params: any): Promise<IResponse> => {
|
||||||
|
return request.get({ url: '/example/list', params })
|
||||||
export const getTableListApi = async (params: any): Promise<IResponse> => {
|
|
||||||
const res = await request.get({ url: '/example/list', params })
|
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const saveTableApi = async (data: Partial<TableData>): Promise<IResponse> => {
|
export const saveTableApi = (data: Partial<TableData>): Promise<IResponse> => {
|
||||||
const res = await request.post({ url: '/example/save', data })
|
return request.post({ url: '/example/save', data })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getTableDetApi = async (id: string): Promise<IResponse<TableData>> => {
|
export const getTableDetApi = (id: string): Promise<IResponse<TableData>> => {
|
||||||
const res = await request.get({ url: '/example/detail', params: { id } })
|
return request.get({ url: '/example/detail', params: { id } })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const delTableListApi = async (ids: string[] | number[]): Promise<IResponse> => {
|
export const delTableListApi = (ids: string[] | number[]): Promise<IResponse> => {
|
||||||
const res = await request.post({ url: '/example/delete', data: { ids } })
|
return request.post({ url: '/example/delete', data: { ids } })
|
||||||
return res && res.data
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,70 @@
|
|||||||
|
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
|
||||||
|
|
||||||
|
import qs from 'qs'
|
||||||
|
|
||||||
|
import { config } from './config'
|
||||||
|
|
||||||
|
import { ElMessage } from 'element-plus'
|
||||||
|
|
||||||
|
const { result_code, base_url } = config
|
||||||
|
|
||||||
|
export const PATH_URL = base_url[import.meta.env.VITE_API_BASEPATH]
|
||||||
|
|
||||||
|
// 创建axios实例
|
||||||
|
const service: AxiosInstance = axios.create({
|
||||||
|
baseURL: PATH_URL, // api 的 base_url
|
||||||
|
timeout: config.request_timeout // 请求超时时间
|
||||||
|
})
|
||||||
|
|
||||||
|
// request拦截器
|
||||||
|
service.interceptors.request.use(
|
||||||
|
(config: AxiosRequestConfig) => {
|
||||||
|
if (
|
||||||
|
config.method === 'post' &&
|
||||||
|
(config.headers as any)['Content-Type'] === 'application/x-www-form-urlencoded'
|
||||||
|
) {
|
||||||
|
config.data = qs.stringify(config.data)
|
||||||
|
}
|
||||||
|
// get参数编码
|
||||||
|
if (config.method === 'get' && config.params) {
|
||||||
|
let url = config.url as string
|
||||||
|
url += '?'
|
||||||
|
const keys = Object.keys(config.params)
|
||||||
|
for (const key of keys) {
|
||||||
|
if (config.params[key] !== void 0 && config.params[key] !== null) {
|
||||||
|
url += `${key}=${encodeURIComponent(config.params[key])}&`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
url = url.substring(0, url.length - 1)
|
||||||
|
config.params = {}
|
||||||
|
config.url = url
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
},
|
||||||
|
(error: AxiosError) => {
|
||||||
|
// Do something with request error
|
||||||
|
console.log(error) // for debug
|
||||||
|
Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// response 拦截器
|
||||||
|
service.interceptors.response.use(
|
||||||
|
(response: AxiosResponse<any>) => {
|
||||||
|
if (response.config.responseType === 'blob') {
|
||||||
|
// 如果是文件流,直接过
|
||||||
|
return response
|
||||||
|
} else if (response.data.code === result_code) {
|
||||||
|
return response.data
|
||||||
|
} else {
|
||||||
|
ElMessage.error(response.data.message)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(error: AxiosError) => {
|
||||||
|
console.log('err' + error) // for debug
|
||||||
|
ElMessage.error(error.message)
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export { service }
|
||||||
@ -1,46 +0,0 @@
|
|||||||
import { service } from '@/config/axios'
|
|
||||||
|
|
||||||
import { AxiosPromise } from 'axios'
|
|
||||||
|
|
||||||
import { config } from '@/config/axios/config'
|
|
||||||
|
|
||||||
const { default_headers } = config
|
|
||||||
|
|
||||||
const request = (option: AxiosConfig) => {
|
|
||||||
const { url, method, params, data, headersType, responseType } = option
|
|
||||||
return service({
|
|
||||||
url: url,
|
|
||||||
method,
|
|
||||||
params,
|
|
||||||
data,
|
|
||||||
responseType: responseType,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': headersType || default_headers
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
|
|
||||||
return request({ method: 'get', ...option })
|
|
||||||
}
|
|
||||||
|
|
||||||
function postFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
|
|
||||||
return request({ method: 'post', ...option })
|
|
||||||
}
|
|
||||||
|
|
||||||
function deleteFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
|
|
||||||
return request({ method: 'delete', ...option })
|
|
||||||
}
|
|
||||||
|
|
||||||
function putFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
|
|
||||||
return request({ method: 'put', ...option })
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useAxios = () => {
|
|
||||||
return {
|
|
||||||
get: getFn,
|
|
||||||
post: postFn,
|
|
||||||
delete: deleteFn,
|
|
||||||
put: putFn
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue