123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import { get, isObject, isArray } from 'lodash-es'
- import Axios from 'axios'
- import { trimData, isNullOrUndefined } from './util-methods'
- import { ErrorCode } from './error-code'
- let currentRequests = 0
- export class AxiosWrapper {
- constructor({ baseUrl = import.meta.env.VITE_APP_BASE_API, unwrap = true } = {}) {
- this.baseUrl = baseUrl
- this.unwrap = unwrap
- }
- baseUrl
- unwrap
-
- static merge(uri, query) {
- if (!uri.includes(':')) {
- return uri
- }
- const keys = []
- const regexp = /\/:([a-z0-9_]+)/gi
- let res
-
- while ((res = regexp.exec(uri)) != null) {
- keys.push(res[1])
- }
- keys.forEach((key) => {
- const val = get(query, key)
- if (!isNullOrUndefined(val)) {
- uri = uri.replace(`:${key}`, `${val}`)
- }
- })
- return uri
- }
-
- static getUriWithQuery(uri, query) {
- if (!uri || !query) return uri
- uri = `${uri}?`
- const arr = []
- for (const i in query) {
- const val = query[i]
- if (isArray(val)) {
- for (const v of val) {
- const str = `${i}=${v}`
- arr.push(str)
- }
- } else {
- const str = `${i}=${val}`
- arr.push(str)
- }
- }
- const sign = '&'
- uri = `${uri}${arr.join(sign)}`
- return uri
- }
- $get(uri, query, options) {
- return this.$request(uri, undefined, query, options)
- }
- $post(uri, data = {}, query, options) {
- return this.$request(uri, data, query, options)
- }
- $delete(uri, data = {}, query, options = {}) {
- options = { ...options, method: 'delete' }
- return this.$request(uri, data, query, options)
- }
- async $request(uri, data, query, options = {}) {
-
-
-
-
-
-
-
-
-
-
-
- const url = AxiosWrapper.getUriWithQuery(uri, query)
-
-
-
-
-
-
-
-
-
-
-
- currentRequests += 1
-
-
-
- try {
- let returnData
- const axios = Axios.create({
- baseURL: this.baseUrl,
- withCredentials: true
- })
-
- const token = localStorage.getItem('webToken')
- const apiToken = localStorage.getItem('apiToken')
- if (token) axios.defaults.headers.common['token'] = token
- if (apiToken) axios.defaults.headers.common['api-token'] = apiToken
- const res = await axios.request({
- method: isNullOrUndefined(data) ? 'get' : 'post',
- url,
- data,
- responseType: 'json',
- ...options
- })
- const returnRes = res.data
- const { errcode, errmsg, details } = returnRes
- if (errcode) {
- console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`)
- return returnRes
- }
-
- if (this.unwrap) {
- returnData = returnRes
- }
-
- const { apiToken: at, ...others } = returnData
- if (at) localStorage.setItem('apiToken', at)
- return others
- } catch (err) {
- let errmsg = '接口请求失败,请稍后重试'
- if (err.response) {
- const { status } = err.response
- if (status === 401) errmsg = '用户认证失败,请重新登录'
- if (status === 403) errmsg = '当前用户不允许执行该操作'
- }
- console.error(
- `[AxiosWrapper] 接口请求失败: ${err.config && err.config.url} -
- ${err.message}`
- )
- return { errcode: ErrorCode.SERVICE_FAULT, errmsg, details: err.message }
- } finally {
-
- currentRequests -= 1
- if (currentRequests <= 0) {
- currentRequests = 0
-
- }
- }
- }
- }
|