175 lines
5.4 KiB
JavaScript
175 lines
5.4 KiB
JavaScript
import axios from 'axios'
|
|
import Cookie from 'js-cookie'
|
|
|
|
// 认证 token 名称
|
|
const xsrfHeaderName = 'Authorization'
|
|
|
|
|
|
axios.defaults.timeout = 5000 // 请求超时时间
|
|
axios.defaults.withCredentials= false // 允许携带 cookies
|
|
axios.defaults.xsrfHeaderName= xsrfHeaderName // 设置认证信息的名称
|
|
axios.defaults.xsrfCookieName= xsrfHeaderName // 设置认证信息的名称
|
|
|
|
// 认证类型
|
|
const AUTH_TYPE = {
|
|
BEARER: 'Bearer', // Bearer 认证
|
|
BASIC: 'basic', // basic 认证
|
|
AUTH1: 'auth1', // auth1 认证
|
|
AUTH2: 'auth2', // auth2 认证
|
|
}
|
|
|
|
// http method
|
|
const METHOD = {
|
|
GET: 'get', // get 请求
|
|
POST: 'post' // post 请求
|
|
}
|
|
|
|
|
|
/**
|
|
* axios请求
|
|
* @param url 请求地址
|
|
* @param method {METHOD} http method
|
|
* @param params 请求参数
|
|
* @returns {Promise<AxiosResponse<T>>}
|
|
*/
|
|
async function request(url, method, params, config) { // 请求方法
|
|
axios.defaults.headers.common[xsrfHeaderName] = Cookie.get(xsrfHeaderName)
|
|
switch (method) {
|
|
case METHOD.GET:
|
|
return axios.get(url, {params, ...config})
|
|
case METHOD.POST:
|
|
return axios.post(url, params, config)
|
|
default:
|
|
return axios.get(url, {params, ...config})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置认证信息
|
|
* @param auth {Object}
|
|
* @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
|
|
*/
|
|
function setAuthorization(auth, authType = AUTH_TYPE.BEARER) { // 设置认证信息
|
|
switch (authType) {
|
|
case AUTH_TYPE.BEARER:
|
|
Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, {expires: auth.expireAt}) // 设置cookie中的认证信息// 设置axios请求头
|
|
// 如果 xsrfHeaderName 存在于 cookie 中,则设置到 axios 的请求头中
|
|
axios.defaults.headers.common[xsrfHeaderName] = Cookie.get(xsrfHeaderName)
|
|
|
|
break
|
|
case AUTH_TYPE.BASIC:
|
|
case AUTH_TYPE.AUTH1:
|
|
case AUTH_TYPE.AUTH2:
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 移出认证信息
|
|
* @param authType {AUTH_TYPE} 认证类型
|
|
*/
|
|
function removeAuthorization(authType = AUTH_TYPE.BEARER) { // 移出认证信息
|
|
switch (authType) {
|
|
case AUTH_TYPE.BEARER:
|
|
Cookie.remove(xsrfHeaderName) // 移出cookie中的认证信息
|
|
break
|
|
case AUTH_TYPE.BASIC:
|
|
case AUTH_TYPE.AUTH1:
|
|
case AUTH_TYPE.AUTH2:
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查认证信息
|
|
* @param authType
|
|
* @returns {boolean}
|
|
*/
|
|
function checkAuthorization(authType = AUTH_TYPE.BEARER) {
|
|
switch (authType) {
|
|
case AUTH_TYPE.BEARER:
|
|
if (Cookie.get(xsrfHeaderName)) { // 检查cookie中是否存在认证信息
|
|
return true
|
|
}
|
|
break
|
|
case AUTH_TYPE.BASIC:
|
|
case AUTH_TYPE.AUTH1:
|
|
case AUTH_TYPE.AUTH2:
|
|
default:
|
|
break
|
|
}
|
|
return false
|
|
}
|
|
|
|
/**
|
|
* 加载 axios 拦截器
|
|
* @param interceptors
|
|
* @param options
|
|
*/
|
|
function loadInterceptors(interceptors, options) { // 加载 axios 拦截器
|
|
const {request, response} = interceptors // 获取interceptors中的请求拦截器和响应拦截器
|
|
// 加载请求拦截器
|
|
request.forEach(item => { // 遍历请求拦截器
|
|
let {onFulfilled, onRejected} = item // 获取请求拦截器中的成功回调和失败回调
|
|
if (!onFulfilled || typeof onFulfilled !== 'function') { // 如果没有成功回调或者成功回调不是函数
|
|
onFulfilled = config => config // 设置默认成功回调
|
|
}
|
|
if (!onRejected || typeof onRejected !== 'function') { // 如果没有失败回调或者失败回调不是函数
|
|
onRejected = error => Promise.reject(error) // 设置默认失败回调
|
|
}
|
|
axios.interceptors.request.use( // 加载请求拦截器
|
|
config => onFulfilled(config, options), // 成功回调
|
|
error => onRejected(error, options) // 失败回调
|
|
)
|
|
})
|
|
// 加载响应拦截器
|
|
response.forEach(item => { // 遍历响应拦截器
|
|
let {onFulfilled, onRejected} = item // 获取响应拦截器中的成功回调和失败回调
|
|
if (!onFulfilled || typeof onFulfilled !== 'function') { // 如果没有成功回调或者成功回调不是函数
|
|
onFulfilled = response => response // 设置默认成功回调
|
|
}
|
|
if (!onRejected || typeof onRejected !== 'function') { // 如果没有失败回调或者失败回调不是函数
|
|
onRejected = error => Promise.reject(error) // 设置默认失败回调
|
|
}
|
|
axios.interceptors.response.use( // 加载响应拦截器
|
|
response => onFulfilled(response, options), // 成功回调
|
|
error => onRejected(error, options) // 失败回调
|
|
)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 解析 url 中的参数
|
|
* @param url
|
|
* @returns {Object}
|
|
*/
|
|
function parseUrlParams(url) { // 解析 url 中的参数
|
|
const params = {} // 定义参数对象
|
|
if (!url || url === '' || typeof url !== 'string') { // 如果url不存在或者url不是字符串
|
|
return params // 返回空参数对象
|
|
}
|
|
const paramsStr = url.split('?')[1] // 获取url中的参数字符串
|
|
if (!paramsStr) { // 如果参数字符串不存在
|
|
return params // 返回空参数对象
|
|
}
|
|
const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ') // 将参数字符串转换为数组
|
|
for (let i = 0; i < paramsArr.length / 2; i++) { // 遍历参数数组
|
|
const value = paramsArr[i * 2 + 1] // 获取参数值
|
|
params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value) // 将参数添加到参数对象中
|
|
}
|
|
return params // 返回参数对象
|
|
}
|
|
|
|
export {
|
|
METHOD,
|
|
AUTH_TYPE,
|
|
request,
|
|
setAuthorization,
|
|
removeAuthorization,
|
|
checkAuthorization,
|
|
loadInterceptors,
|
|
parseUrlParams
|
|
}
|