123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- 'use strict';
- import axios from 'axios';
- import { Loading } from 'element-ui';
- import qs from 'qs';
- import szjcomo from 'szjcomo-utils';
- import { globalConfig } from '@/service/common/config';
- import store from '@/store';
- import router from '@/router';
- import cache from '@/service/common/cache';
- /**
- * [reqConf 全局通用配置]
- * @type {Object}
- */
- const reqConf = {
- /**
- * [maxContentLength `定义http内容中允许的最大内容长度]
- * @type {[type]}
- */
- maxContentLength:5 * 1024 * 1024,
- /**
- * [timeout指定请求超时前的毫秒数。如果请求所用时间超过timeout,则请求将被中止请求。]
- * @type {Number}
- */
- timeout:20 * 1000,
- /**
- * 除非url是绝对的,否则baseURL将加在url前面。可以方便地为axios实例设置baseURL以传递相对url到该实例的方法。
- * @type {String}
- */
- baseURL:process.env.NODE_ENV == 'development' ? '/apis' : globalConfig.app_domain,
- /**
- * `transformResponse` allows changes to the response data to be made before it is passed to then/catch
- * @author szjcomo
- * @createTime 2020-09-12
- * @param {[type]} data [description]
- * @return {[type]} [description]
- */
- transformResponse:function(data) {
- try {
- return JSON.parse(data);
- } catch(err) {
- return data;
- }
- }
- };
- /**
- * [openLoading 打开加载框]
- * @author szjcomo
- * @createTime 2020-09-12
- * @param {Object} options [description]
- * @return {[type]} [description]
- */
- const openLoading = function(options = {}) {
- let defaults = {text:'加载中',background:'#000000cc'};
- return Loading.service(Object.assign(defaults,options));
- }
- /**
- * [closeLoading 关闭加载框]
- * @author szjcomo
- * @createTime 2020-09-12
- * @param {Object} options [description]
- * @return {[type]} [description]
- */
- const closeLoading = function(loadingInstance) {
- if(loadingInstance) loadingInstance.close();
- }
- /**
- * [getAxiosInstance 创建一个请求实例]
- * @author szjcomo
- * @createTime 2020-09-12
- * @param {Object} options [description]
- * @return {[type]} [description]
- */
- const getAxiosInstance = function(options = {}) {
- let instance = axios.create(Object.assign(reqConf,options));
- instance.interceptors.request.use(async (config) => {
- return config;
- }, (error) => {return Promise.reject(error);});
- instance.interceptors.response.use((response)=> {
- if(response && response.data && response.data.result_code == 10001) {
- return Promise.reject(new Error(response.data.message));
- } else {
- return Promise.resolve(response.data);
- }
- }, (error) => {
- return Promise.reject(error);
- });
- return instance;
- }
- /**
- * [szjcomoAxios 自定义请求对象]
- * @type {Object}
- */
- const szjcomoAxios = {
- /**
- * [get 发送get请求]
- * @author szjcomo
- * @createTime 2020-09-12
- * @param {[type]} url [description]
- * @param {Object} params [description]
- * @param {Object} instanceOptions [description]
- * @param {Boolean} loading [description]
- * @return {[type]} [description]
- */
- get:async function(url,params = {},instanceOptions = {},loading = true) {
- let loadingInstance,result;
- if(loading) loadingInstance = openLoading({text:'加载中'});
- try {
- let tmpUrl = url;
- if(params && Object.keys(params).length > 0) tmpUrl += ((tmpUrl.indexOf('?') > -1?'&':'?') + qs.stringify(params));
- result = await getAxiosInstance(instanceOptions).get(tmpUrl);
- } catch(err) {
- result = szjcomo.appResult(err.message,err.data,true,err.status);
- } finally {
- if(loadingInstance) closeLoading(loadingInstance);
- return result;
- }
- },
- /**
- * [post 发送post请求]
- * @author szjcomo
- * @createTime 2020-09-23
- * @param {[type]} url [description]
- * @param {Object} params [description]
- * @param {Object} instanceOptions [description]
- * @param {Boolean} loading [description]
- * @return {[type]} [description]
- */
- post:async function(url,params = {},instanceOptions = {},loading = true) {
- let loadingInstance,result;
- if(loading) loadingInstance = openLoading({text:'提交中'});
- try {
- result = await getAxiosInstance(instanceOptions).post(url,params);
- } catch(err) {
- result = szjcomo.appResult(err.message,err.data,true,err.status);
- } finally {
- if(loadingInstance) closeLoading(loadingInstance);
- return result;
- }
- },
- /**
- * [put 发送put请求]
- * @author szjcomo
- * @date 2020-10-22
- * @param {[type]} url [description]
- * @param {Object} params [description]
- * @param {Object} instanceOptions [description]
- * @param {Boolean} loading [description]
- * @return {[type]} [description]
- */
- put:async function(url,params = {},instanceOptions = {},loading = true){
- let loadingInstance,result;
- if(loading) loadingInstance = openLoading({text:'提交中'});
- try {
- result = await getAxiosInstance(instanceOptions).put(url,params);
- } catch(err) {
- result = szjcomo.appResult(err.message,err.data,true,err.status);
- } finally {
- if(loadingInstance) closeLoading(loadingInstance);
- return result;
- }
- },
- /**
- * [delete 发送delete请求]
- * @author szjcomo
- * @date 2020-10-22
- * @param {[type]} url [description]
- * @param {Object} params [description]
- * @param {Object} instanceOptions [description]
- * @param {Boolean} loading [description]
- * @return {[type]} [description]
- */
- delete:async function(url,params = {},instanceOptions = {},loading = true) {
- let loadingInstance,result;
- if(loading) loadingInstance = openLoading({text:'提交中'});
- try {
- let tmpUrl = url;
- if(params && Object.keys(params).length > 0) tmpUrl += ((tmpUrl.indexOf('?') > -1?'&':'?') + qs.stringify(params));
- result = await getAxiosInstance(instanceOptions).delete(tmpUrl);
- } catch(err) {
- result = szjcomo.appResult(err.message,err.data,true,err.status);
- } finally {
- if(loadingInstance) closeLoading(loadingInstance);
- return result;
- }
- },
- /**
- * [upload 实现文件上传功能]
- * @author szjcomo
- * @createTime 2020-10-06
- * @param {[type]} url [description]
- * @param {Object} params [description]
- * @param {Object} instanceOptions [description]
- * @param {Boolean} loading [description]
- * @return {[type]} [description]
- */
- upload:async function(url,params = {},filename = 'filename',instanceOptions = {},loading = true) {
- let loadingInstance,result;
- if(loading) loadingInstance = openLoading({text:'上传中'});
- try {
- let formData = new FormData();
- Object.keys(params).forEach(key => {
- if(key == filename) {
- formData.append(key,params[key],params[key].name);
- } else if(key != 'headers') {
- formData.append(key,params[key]);
- }
- })
- let headers = {'Content-Type': 'multipart/form-data'};
- if(params.headers) {
- Object.keys(params.headers).forEach(key => {
- headers[key] = params.headers[key];
- })
- }
- result = await getAxiosInstance(instanceOptions).request({
- method:'post',url:`${globalConfig.app_upload_domain}${url}`,data:formData,headers:headers
- });
- } catch(err) {
- result = szjcomo.appResult(err.message,err.data,true,err.status);
- } finally {
- if(loadingInstance) closeLoading(loadingInstance);
- return result;
- }
- }
- };
- export default szjcomoAxios;
|