| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import axios from 'axios'
- import Cookie from 'js-cookie'
- // 跨域认证信息 header 名
- const xsrfHeaderName = 'Authorization'
- axios.defaults.timeout = 5000
- axios.defaults.withCredentials = true
- axios.defaults.xsrfHeaderName = xsrfHeaderName
- axios.defaults.xsrfCookieName = xsrfHeaderName
- //单独引用需修改的元素
- // 认证类型
- const AUTH_TYPE = {
- BEARER: 'Bearer',
- BASIC: 'basic',
- AUTH1: 'auth1',
- AUTH2: 'auth2',
- }
- // http method
- const METHOD = {
- GET: 'get',
- POST: 'post'
- }
- /**
- * axios请求
- * @param url 请求地址
- * @param method {METHOD} http method
- * @param params 请求参数
- * @returns {Promise<AxiosResponse<T>>}
- */
- async function request(url, method, params, config) {
- const Url = process.env.VUE_APP_API_BASE_URL + url;
- 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) {
- // console.log(authType, AUTH_TYPE.BEARER, auth)
- switch (authType) {
- case AUTH_TYPE.BEARER:
- Cookie.set(xsrfHeaderName, 'Bearer ' + auth)
- 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)
- 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)) {
- 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) {
- const { request, response } = 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 => {
- config.headers[xsrfHeaderName] = 'Bearer 2856c49f-7b34-4e83-b3c8-2c37dba7cb3b'
- return config
- },
- error => onRejected(error, options)
- )
- })
- // 加载响应拦截器
- response.forEach(item => {
- // let { onFulfilled, onRejected } = item
- console.log(item)
- // if (!onFulfilled || typeof onFulfilled !== 'function') {
- // onFulfilled = response => response
- // }
- // if (!onRejected || typeof onRejected !== 'function') {
- // onRejected = error => Promise.reject(error)
- // }
- axios.interceptors.response.use(
- response => {
- if (response.data.code !== 200) {
- return Promise.reject(response);
- }
- return response;
- },
- error => {
- console.log(error)
- if (error?.data) {
- window.$message.error(error?.data?.msg)
- return Promise.reject(error.data);
- }
- // onRejected(error, options)
- }
- )
- })
- }
- /**
- * 解析 url 中的参数
- * @param url
- * @returns {Object}
- */
- function parseUrlParams(url) {
- const params = {}
- if (!url || url === '' || typeof url !== 'string') {
- return params
- }
- const paramsStr = url.split('?')[1]
- 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
- }
|