84 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* eslint-disable */
 | |
| import axios from 'axios'
 | |
| import {Message} from "element-ui";
 | |
| import {workflow} from "./modules";
 | |
| 
 | |
| const instance = axios.create({
 | |
|   baseURL: process.env.NODE_ENV === "production" ? "/" : "/lan",
 | |
|   timeout: 600000,
 | |
|   withCredentials: true,
 | |
| })
 | |
| 
 | |
| const getStore = () => JSON.parse(localStorage.getItem("vuex") || null) || {}
 | |
| 
 | |
| const getToken = () => getStore().user?.token
 | |
| /**
 | |
|  * 节流工具
 | |
|  */
 | |
| let throttleMap = {}
 | |
| const source = axios.CancelToken.source();
 | |
| instance.interceptors.request.use(config => {
 | |
|   if (config.throttle) {// 节流处理
 | |
|     let timer = throttleMap[config.url]
 | |
|     if (!!timer) {
 | |
|       config.cancelToken = source.token
 | |
|       source.cancel("节流控制,取消请求:" + config.url)
 | |
|     }
 | |
|     throttleMap[config.url] = setTimeout(() => {
 | |
|       throttleMap[config.url] = null
 | |
|     }, config.throttle)
 | |
|   }
 | |
|   if (!config.withoutToken && getToken()) {
 | |
|     config.headers["Authorization"] = getToken()
 | |
|   }
 | |
|   //BUG 9456 去除传参空格
 | |
|   if (config.params) {
 | |
|     Object.keys(config.params).map(e => {
 | |
|       if (typeof config.params[e] == "string") config.params[e] = config.params[e].trim()
 | |
|     })
 | |
|   }
 | |
|   config = workflow.processAdapter(config)
 | |
|   return config
 | |
| }, err => {
 | |
|   console.error(err)
 | |
| })
 | |
| instance.interceptors.response.use(res => {
 | |
|   if (res && !!res.config.workflow) {
 | |
|     const {config: {workflow: app}, data: {data: bid}} = res
 | |
|     bid && workflow.startProcess({app, bid, flows: getStore().workflow})
 | |
|   }
 | |
|   if (res && res.data) {
 | |
|     if (!!res.data.code?.toString()) {
 | |
|       if (res.data.code == 0) {
 | |
|         return res.data
 | |
|       } else if (!!res.config.pureBack) {
 | |
|         return res.data
 | |
|       } else if (res.data.code == 401) {
 | |
|         console.error("安全令牌验证无法通过!")
 | |
|         return Promise.reject(res.data)
 | |
|       } else {
 | |
|         Message.error(res.data.msg || "请求失败!")
 | |
|         return !!res.config.returnError ? res.data : Promise.reject(res.data.msg)
 | |
|       }
 | |
|     } else return res.data
 | |
|   } else {
 | |
|     Message.error("服务器异常,请联系管理员!")
 | |
|   }
 | |
| }, err => {
 | |
|   if (err) {
 | |
|     if (err.response) {
 | |
|       if (err.response.status == 401) {
 | |
|         console.error("安全令牌验证无法通过!")
 | |
|       } else {
 | |
|         console.error(err.response.statusText)
 | |
|       }
 | |
|     } else {
 | |
|       console.error(err)
 | |
|     }
 | |
|   } else {
 | |
|     console.error("通信异常,请联系管理员!")
 | |
|   }
 | |
| })
 | |
| 
 | |
| export default instance
 |