UNPKG

tpa_web_component

Version:

TPA Web Components Library

112 lines (109 loc) 3.12 kB
import axios from 'axios'; import { ElNotification } from 'element-plus'; import { noticeAppTokenExpired } from './callingAppApi'; import { getDeviceInfoFromStorage, isUpThisVer } from './util'; let isnoticeAppTokenExpiredDoing = false; const axiosIntance = axios.create({ withCredentials: true, timeout: 20000 }); axiosIntance.interceptors.request.use( function (config) { config.params = config.params || {}; // 在发送请求之前做些什么 if(isUpThisVer('1.0.3') && config.params){ // APP 大于1.0.3 直接使用app给的参数 const deviceInfo = getDeviceInfoFromStorage() for(let key in deviceInfo){ config.params[key] = deviceInfo[key]; } return config; }else if(config.params){ const deviceInfo = getDeviceInfoFromStorage() for(let key in deviceInfo){ let newKey = key.replace(/Ver$/,"Version") config.params[newKey] = deviceInfo[key]; } return config; } return config }, function (error) { // 对请求错误做些什么 return Promise.reject(error); } ); // 添加响应拦截器 axiosIntance.interceptors.response.use( function (response) { // 对响应数据做点什么 const data = response.data; // console.log('response',response) // if (!data.code) return data;x\ if (data.code == 200 || data.code == 100000) { return response.data.data; } else if(data.code == 999999){ if(isnoticeAppTokenExpiredDoing)return; isnoticeAppTokenExpiredDoing = true; noticeAppTokenExpired(()=>{ isnoticeAppTokenExpiredDoing = false; window.location.reload() }) return Promise.reject(response.data); }else{ // ElNotification({ // title: '失败提示', // message: response.data.message, // type: 'error', // }) return Promise.reject(response.data.code); } }, function (error) { // console.log('response.data.error', response.data.error); // 对响应错误做点什么 // ElNotification({ // title: '失败提示', // message: typeof error == 'string' ? error : '系统繁忙', // type: 'error', // }) // ElMessageBox.alert(typeof error == 'string' ? error : '系统繁忙', { // // if you want to disable its autofocus // // autofocus: false, // confirmButtonText: '确认', // type: 'error', // callback: (action) => { // window.location.reload() // }, // }) return Promise.reject(error); } ); export function get(url, params, headers={}) { return axiosIntance.get(url, { params, headers }); } export function getWithConfig(url, params,config={}){ return axiosIntance({ method:'get', url, params, ...config }); } export function post(url, params, headers={}) { return axiosIntance.post(url, params,{headers}); } export function postWithConfig(url, params,config={}){ return axiosIntance({ method:'post', url, data:params, ...config }); } export function requestHead(url){ return axios({ method: 'head', url }) }