封装方式一
import axios from 'axios' import qs from 'qs' import { Toast } from 'vant' import Lockr from 'lockr' axios.defaults.timeout = 30000 axios.defaults.baseURL = import.meta.env.VITE_HOST axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded' // http request axios.interceptors.request.use((config) => { config.headers.refererUrl = window.location.href config.headers.authKey = Lockr.get('authKey') config.headers.sessionId = Lockr.get('sessionId') // 请求头参数处理 config.data = qs.stringify(config.data) return config }, (error) => { return Promise.reject(error) }) // http response axios.interceptors.response.use((res) => { if (res.status && res.status === 200 && res.data.code !== 200) { if (res.data.code === 101) { window.location.href = `${window.location.origin}${import.meta.env.VITE_HREF}/login${res.data.url?`?return_url=${res.data.url}&type=${res.data.type}`:''}` } else if (res.data.msg && res.data.msg !== '') { Toast(res.data.msg) } else if (res.data.code === 203) { return false } else { Toast('系统错误') } } return res }, (error) => { if (!error.response && error.message) { Toast('请求超时,请检查网络,刷新后重试') } else { Toast('系统错误') } return Promise.reject(error) }) export default axios /** * 封装get方法 * @param url * @param params * @returns {Promise} */ export function get (url: any, params: Object = {}) { params.t = new Date().getTime() return new Promise((resolve, reject) => { axios.get(url, { params: params }) .then(response => { resolve(response.data) }) .catch(err => { reject(err) }) }) } /** * 封装post请求 * @param url * @param data * @returns {Promise} */ export function post (url: any, data: Object = {}) { return new Promise((resolve, reject) => { axios.post(url, data) .then(response => { resolve(response.data) }, err => { reject(err) }) .catch(err => { reject(err) }) }) } /** * 封装delete方法 * @param url * @param params * @returns {Promise} */ export function doDelete (url: any, params: Object = {}) { return new Promise((resolve, reject) => { axios.delete(url, { params: params }) .then(response => { resolve(response.data) }) .catch(err => { reject(err) }) }) } /** * 封装patch请求 * @param url * @param data * @returns {Promise} */ export function patch (url: any, data: Object = {}) { return new Promise((resolve, reject) => { axios.patch(url, data) .then(response => { resolve(response.data) }, err => { reject(err) }) .catch(err => { reject(err) }) }) } /** * 封装put请求 * @param url * @param data * @returns {Promise} */ export function put (url: any, data: Object = {}) { return new Promise((resolve, reject) => { axios.put(url, data) .then(response => { resolve(response.data) }, err => { reject(err) }) .catch(err => { reject(err) }) }) }
使用方式
import { get, post } from '@/utils/request.ts'
get(url).then(...)
post(url).then(...)
...
封装方式二
// http.ts import axios, { AxiosRequestConfig, AxiosResponse } from 'axios' import qs from 'qs' /* const showStatus = (status: number) => { let message = '' switch (status) { case 400: message = '请求错误(400)' break case 401: message = '未授权,请重新登录(401)' break case 403: message = '拒绝访问(403)' break case 404: message = '请求出错(404)' break case 408: message = '请求超时(408)' break case 500: message = '服务器错误(500)' break case 501: message = '服务未实现(501)' break case 502: message = '网络错误(502)' break case 503: message = '服务不可用(503)' break case 504: message = '网络超时(504)' break case 505: message = 'HTTP版本不受支持(505)' break default: message = `连接出错(${status})!` } return `${message},请检查网络或联系管理员!` } */ const service = axios.create({ baseURL: import.meta.env.VITE_HOST, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, // 是否跨站点访问控制请求 withCredentials: false, timeout: 30000, transformRequest: [(data) => { data = JSON.stringify(data) return data }], validateStatus() { // 使用async-await,处理reject情况较为繁琐,所以全部返回resolve,在业务代码中处理异常 return true }, transformResponse: [(data) => { if (typeof data === 'string' && data.startsWith('{')) { data = JSON.parse(data) } return data }] }) // 声明一个 Map 用于存储每个请求的标识 和 取消函数 const pending = new Map() /** * 添加请求 * @param {Object} config */ const addPending = (config: AxiosRequestConfig) => { const url = [ config.method, config.url, qs.stringify(config.params), qs.stringify(config.data) ].join('&') config.cancelToken = config.cancelToken || new axios.CancelToken(cancel => { if (!pending.has(url)) { // 如果 pending 中不存在当前请求,则添加进去 pending.set(url, cancel) } }) } /** * 移除请求 * @param {Object} config */ const removePending = (config: AxiosRequestConfig) => { const url = [ config.method, config.url, qs.stringify(config.params), qs.stringify(config.data) ].join('&') if (pending.has(url)) { // 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除 const cancel = pending.get(url) cancel(url) pending.delete(url) } } /** * 清空 pending 中的请求(在路由跳转时调用) */ export const clearPending = () => { for (const [url, cancel] of pending) { cancel(url) } pending.clear() } // 请求拦截器 service.interceptors.request.use((config: AxiosRequestConfig) => { removePending(config) // 在请求开始前,对之前的请求做检查取消操作 addPending(config) // 将当前请求添加到 pending 中 // let token = localStorage.getItem('token') // if(token){ // config.headers.Authorization = `${token}`; // } return config }, (error) => { // 错误抛到业务代码 error.data = {} error.data.msg = '服务器异常,请联系管理员!' return Promise.resolve(error) }) // 响应拦截器 service.interceptors.response.use((response: AxiosResponse) => { removePending(response) // 在请求结束后,移除本次请求 const status = response.status let msg = '' if (status < 200 || status >= 300) { // 处理http错误,抛到业务代码 // msg = showStatus(status) if (typeof response.data === 'string') { response.data = { msg } } else { response.data.msg = msg } } return response }, (error) => { if (axios.isCancel(error)) { console.log('repeated request: ' + error.message) } else { // handle error code // 错误抛到业务代码 error.data = {} error.data.msg = '请求超时或服务器异常,请检查网络或联系管理员!' // ElMessage.error(error.data.msg) } return Promise.reject(error) }) export default service
使用方式
import axios from '@/utils/http.ts' axios(url, { method: 'get', responseType: 'json', params: {}, }).then(res => { console.warn('tag', res) })
方式二参考:Vue3+TypeScript封装axios并进行请求调用
注意事项: 谨慎添加withCredentials为true