/** * @description: axios二次封装 * @author: chenxiaoxi * @date: 2018-09-04 11:29:34 */ import axios from 'axios' import router from '@/router' import store from '@/store' import {Message} from 'element-ui' import { baseUrl,webLoginType,ssoLoginUrlDev } from '@/sysConfig' import domMessage from '../messageOnce/messageOnce' const messageOnce = new domMessage() export const CancelToken = axios.CancelToken // import 'element-ui/lib/theme-chalk/index.css' const _axios = axios.create({ baseURL: process.env.NODE_ENV === 'production' ? baseUrl : '/' }) /** * @description: FormData * @author: xx * @date: 2018-08-14 16:57:48 */ let formData = (data) => { let _formData = new FormData() for (let i in data) { _formData.append(i, data[i]) } return _formData } _axios.defaults.timeout = 60000 _axios.defaults.headers['Content-type'] = 'application/json' window._axiosPromiseArr = [] /** * @description: axios请求拦截器 * liuyan */ _axios.interceptors.request.use( config => { // 记录接口请求 let token = store.state.token; if (!config.headers.hasOwnProperty('token') && token != null && token) { config.headers.token = token; } // config.cancelToken = new axios.CancelToken(cancel => { // // window._axiosPromiseArr.push({ cancel }) // }) return config }, error => { return Promise.reject(error.response) }) /** * @description: axios响应拦截器 * liuyan */ _axios.interceptors.response.use( res => { if(res.data && res.data.respCode && res.data.respCode === 'LE505'){ window.sessionStorage.clear() messageOnce.warning({ message: '登录超时,请重新登录', type: 'warning' }) if(webLoginType != null && webLoginType === 'idm' ) { // idm 统一认证登录页面 return window.location.href = ssoLoginUrlDev }else if(webLoginType != null && webLoginType === 'dev'){ return router.push('/login') } } return res }, error => { // 登录超时 if (error.response.data.path === '/a/login' && store.state.isTimeOut === false) { for (let key in store.state) { if (key !== 'isTimeOut' && key !== 'laws_urm' && key !== 'laws_prm') { store.state[key] = '' } } Message.warning('登录超时,请重新登录') router.push('/login') store.state.isTimeOut = true } }) function api () { return store.getters.getTypeFlag === 'CloudRepository' ? '/busApi/' : '/api/' } export default { _axios, /** * @description: post方法 * @config: { * _this : this (vue原型) * loading: data中定义的 loading('字符串格式 例如data定义的myloading config:{ _this: this, loading: 'myloading' }') * } * @author: xx * @date: 2018-08-14 16:59:56 */ post (url, param, config, thenFun, exeFun) { var _formData = formData(param) // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false // 是否显示操作提示 let showTips = config.showTips === undefined ? true : config.showTips if (_this) { _this[loading] = true } _axios.post(api() + url + '?' + new Date().getTime(), _formData).then(res => { if (_this && loading) { _this[loading] = false } if (res.data.ok !== undefined && showTips) { let type = res.data.ok ? 'success' : 'warning' if (_this) { if (res.data.message !== '') { _this.$message[type](res.data.message) } } } thenFun.call(this, res.data) }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, /** * @description: post方法(json格式提交) * @config: { * _this : this (vue原型) * loading: data中定义的 loading('字符串格式 例如data定义的myloading config:{ _this: this, loading: 'myloading' }') * } * @author: xx * @date: 2018-08-14 16:59:56 */ postData (url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false // 是否显示操作提示 let showTips = config.showTips === undefined ? true : config.showTips if (_this) { _this[loading] = true } _axios.post(api() + url + '?' + new Date().getTime(), param).then(res => { if (_this && loading) { _this[loading] = false } if (res.data.ok !== undefined && showTips) { let type = res.data.ok ? 'success' : 'warning' if (_this && res.data.message !== '') { _this.$message[type](res.data.message) } } thenFun.call(this, res.data) }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, /** * @description: $get方法(使用post方法获取数据) * @config: { * _this : this (vue原型) * loading: data中定义的 loading('字符串格式 例如data定义的myloading config:{ _this: this, loading: 'myloading' }') * } * @author: xx * @date: 2018-08-14 16:59:56 */ $get (url, param, config, thenFun, exeFun) { var _formData = formData(param) // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.post(api() + url + '?' + new Date().getTime(), _formData).then(res => { if (_this && loading) { _this[loading] = false } thenFun.call(this, res.data) }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, /** * @description: get方法 * @config: { * _this : this (vue原型) * loading: data中定义的 loading('字符串格式 例如data定义的myloading config:{ _this: this, loading: 'myloading' }') * } * @author: xx * @date: 2018-08-14 16:59:56 */ get (url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.get(api() + url + '?_t=' + new Date().getTime(), { params: param, cancelToken: config.cancelToken }).then(res => { if (_this && loading) { _this[loading] = false } // 返回data对象 if (res.ok !== undefined) { if (res.ok) { thenFun.call(this, res.data) } } else { // 返回data数组 thenFun.call(this, res.data) } }).catch(err => { if (_this && loading) { _this[loading] = false } if (exeFun) { exeFun.call(this, err) } }) }, /** * @description: put方法 * @author: chenxiaoxi * @date: 2018-09-06 13:28:37 */ put (url, param, config, thenFun, exeFun) { var _formData = formData(param) // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.put(api() + url, _formData).then(res => { if (_this && loading) { _this[loading] = false } thenFun.call(this, res.data) }).catch(err => { if (_this && loading) { _this[loading] = false } exeFun.call(this, err) }) }, /** * @description: put方法(json提交) * @author: chenxiaoxi * @date: 2018-09-06 13:28:37 */ putData (url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.put(api() + url + '?' + new Date().getTime(), param).then(res => { if (_this && loading) { _this[loading] = false } if (res.data.ok !== undefined) { let type = res.data.ok ? 'success' : 'warning' if (_this && res.data.message !== '') { _this.$message[type](res.data.message) } } thenFun.call(this, res.data) }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, downloadFile (url, param, fileName) { _axios({ method: 'post', url, data: param, responseType: 'blob' }).then( res => { const data = res.data const r = new FileReader() r.onload = function () { try { const resData = JSON.parse(this.result) } catch (err) { // 下载正常处理 // 兼容ie11 if (window.navigator.msSaveOrOpenBlob) { try { const blobObject = new Blob([data]) window.navigator.msSaveOrOpenBlob(blobObject, fileName) } catch (e) { console.log(e) } return } // a标签实现下载 let url = window.URL.createObjectURL(new Blob([data])) let link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', fileName) document.body.appendChild(link) link.click() resolve(fileName) } } r.readAsText(data) // FileReader的API }).catch(res => { console.log(res) }) }, /** * @description: delete方法 * @author: chenxiaoxi * @date: 2018-09-14 09:55:30 */ delete (url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.delete(api() + url + '?' + new Date().getTime(), { params: param }).then(res => { if (_this && loading) { _this[loading] = false } if (res.data.ok !== undefined) { let type = res.data.ok ? 'success' : 'warning' if (_this) { if (res.data.message !== '') { _this.$message[type](res.data.message) } } thenFun.call(this, res.data) } }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, /** * @description: delete方法(字符串) * @author: chenxiaoxi * @date: 2018-09-29 16:21:41 */ deleteStr (url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.delete(api() + url + '?' + new Date().getTime()).then(res => { if (_this && loading) { _this[loading] = false } if (res.data.ok !== undefined) { let type = res.data.ok ? 'success' : 'warning' if (_this) { if (res.data.message !== '') { _this.$message[type](res.data.message) } } thenFun.call(this, res.data) } }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, /** * @description: 自定义请求类型 * @params: type为请求类型 * @author: chenxiaoxi * @date: 2018-09-25 14:51:19 */ ajax (type, url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios[type](api() + url + '?' + new Date().getTime(), param).then(res => { if (_this && loading) { _this[loading] = false } if (res.data.ok !== undefined) { let type = res.data.ok ? 'success' : 'warning' if (_this) { if (res.data.message !== '') { _this.$message[type](res.data.message) } } if (res.data.ok) { thenFun.call(this, res.data) } } }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, /** * @description: 请求云端动态get方法 * @config: { * _this : this (vue原型) * loading: data中定义的 loading('字符串格式 例如data定义的myloading config:{ _this: this, loading: 'myloading' }') * } * @author: xx * @date: 2018-08-14 16:59:56 */ getMsgDynamicInfo (url, param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.get('/busApi/' + url + '?' + new Date().getTime(), { params: param }).then(res => { if (_this && loading) { _this[loading] = false } // 返回data对象 if (res.ok !== undefined) { if (res.ok) { thenFun.call(this, res.data) } } else { // 返回data数组 thenFun.call(this, res.data) } }).catch(err => { if (_this && loading) { _this[loading] = false // _this.$Notice.error({ // title: '错误', // desc: '网络连接错误' // }) } exeFun.call(this, err) }) }, start (param, config, thenFun, exeFun) { // 参数包含this let _this = config._this || false // 参数包含loading let loading = config.loading || false if (_this) { _this[loading] = true } _axios.get(`/bat-wkflow/activiti_define_start?id=11`, {}).then(res => { if (_this && loading) { _this[loading] = false } // 返回data对象 if (res.ok !== undefined) { if (res.ok) { thenFun.call(this, res.data) } } else { // 返回data数组 thenFun.call(this, res.data) } }).catch(err => { if (_this && loading) { _this[loading] = false } exeFun.call(this, err) }) }, /** * @description: delete方法 * @author: chenxiaoxi * @date: 2018-09-14 09:55:30 */ postDelete (url, param, thenFun, exeFun) { _axios.post(api() + url, {}).then(res => { thenFun.call(this, res.data) }).catch(err => { exeFun.call(this, err) }) } }