From c4044fb60e53e5c83da4ee645d5160f914e422a3 Mon Sep 17 00:00:00 2001 From: YANGDL <114714267@qq.com> Date: 星期五, 05 三月 2021 16:17:41 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- src/utils/axios.js | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 214 insertions(+), 0 deletions(-) diff --git a/src/utils/axios.js b/src/utils/axios.js new file mode 100644 index 0000000..a5bb1ab --- /dev/null +++ b/src/utils/axios.js @@ -0,0 +1,214 @@ +import Vue from 'vue' +import axios from 'axios' +import * as $CONST from '@/utils/constant' +import * as $T from '@/utils/tools' +// import $store from '@/store' + +/** + * 瀹氫箟浼犲叆鍙傛暟鏁版嵁鏍煎紡 + * + * === 鍒犻櫎鏁版嵁 === + * { + * key:['dataId1','dataId2'] + * } + * + */ + +/** + * 瀹氫箟杩斿洖鐨刯son鏁版嵁鏍煎紡 + * + * === 鍒嗛〉鏁版嵁 === + * { + * code:200, --- 鐩墠妗嗘灦璁や负0鍜�200閮芥槸鎴愬姛鐘舵�� + * message:'鎸変笟鍔¤嚜瀹氫箟', + * data:{ + * records:[{...},{...}], --- 鍙寜鑷繁涓氬姟鏇挎崲灞炴�� + * total:xx, --- 鍙寜鑷繁涓氬姟鏇挎崲灞炴�� + * size:xx, --- 鍙寜鑷繁涓氬姟鏇挎崲灞炴�� + * current:xx, --- 鍙寜鑷繁涓氬姟鏇挎崲灞炴�� + * pages:xx --- 鍙寜鑷繁涓氬姟鏇挎崲灞炴�� + * }, + * } + * + * === 鍒楄〃鏁版嵁 === + * + * { + * code:200, + * message:'鎸変笟鍔¤嚜瀹氫箟', + * data:[{..},{..}...] + * } + * + * === 涓�鏉℃暟鎹� === + * + * { + * code:200, + * message:'鎸変笟鍔¤嚜瀹氫箟', + * data:{...} + * } + * + * === 鏃犺繑鍥炴暟鎹� === + * + * { + * code:200, + * message:'鎸変笟鍔¤嚜瀹氫箟', + * data: null + * } + * + * === 鏍戝舰缁撴瀯 === + * + * { + * code:200, + * message:'鎸変笟鍔¤嚜瀹氫箟' + * data:[{ + * prop:xx, + * ... + * children: [ + * prop:xx, + * ... + * children:[...] + * ] + * }, + * { + * prop:xx, + * ... + * children: [ + * prop:xx, + * ... + * children:[...] + * ] + * }] + * } + * + */ + +// 鍒涘缓axios瀹炰緥 +const Service = axios.create({ + timeout: 1000 +}) + +const CancelToken = axios.CancelToken + +/* + function showDebugInfo(config) { + console.group('%cMethod::' + config.method + '::Url::' + config.url, 'color:red;font-size:12px;') + console.log('%c淇濆瓨/鏇存柊/鏌ョ湅鍦哄悎锛屼紶鍏ョ殑鍙傛暟::', 'font-size:12px;') + console.log(config.hasOwnProperty('params') ? JSON.stringify(config.params) : JSON.stringify(config.data)) + console.groupEnd() + } + + function filterOauthData(config) { + let url = config.url + if (config.method === 'post' && $t.isExistOauthApi(url)) { + // post璇锋眰鐨勫満鍚堜笅锛岃拷鍔犺鑹插悕绉� + } + + if (config.method === 'get' && $t.isExistOauthApi(url)) { + // get璇锋眰鐨勫満鍚堜笅锛岃拷鍔犺鑹插悕绉� + if (config.hasOwnProperty('params')) { + config.params['userCode'] = '' + } else { + config['params'] = {} + config.params['userCode'] = '' + } + } + return config + } */ + +// request鎷︽埅鍣� +Service.interceptors.request.use( + config => { + // 涓存椂杩藉姞 + // if ($store.state.user.account) { + // config.headers.account = $store.state.user.account + // } + const copyConfig = Object.assign({}, config) + // copyConfig = filterOauthData(copyConfig) + // showDebugInfo(copyConfig) + copyConfig.cancelToken = new CancelToken((c) => { + // 椤甸潰澶辨晥鍦烘櫙锛屽彇娑坧adding涓姹� + Vue.prototype.$cancels.push(c) + // todo 蹇�熷弻鍑诲彇娑堥噸澶嶈姹� + // 閲嶅璇锋眰鍦烘櫙锛屽彇娑堟帀涓婁竴涓湭padding涓姹� + }) + return copyConfig + }, + error => { + Promise.reject(error) + }) + +// respone鎷︽埅鍣� +Service.interceptors.response.use( + response => { + const res = response.data + if (Number(res.code) !== 200 && Number(res.code) !== 0) { + $T.warning(res.message) + return Promise.reject(res.message) + } else { + return res + } + }, + error => { + if (error.message && error.message.includes('timeout')) { + $T.fail($CONST.MSG_SYS_TIME_OUT) + return Promise.reject(error) + } + + if (!error.response) { + $T.fail($CONST.MSG_SYS_ERR) + return Promise.reject(error) + } + + switch (error.response.status) { + // http status handler + case 404: + $T.fail($CONST.MSG_SYS_404) + break + case 500: + $T.fail($CONST.MSG_SYS_ERR) + break + case 503: + $T.fail($CONST.MSG_SYS_503) + break + } + + return Promise.reject(error) + } +) + +const $http = {} +$http.$service = Service +$http.$axios = axios +$http.get = function (url, params = {}) { + if ($T.isInlineParams(url)) { + // 涓ユ牸閬靛惊restful鏍囧噯鐨剈rl鐨勫満鏅� + url = $T.replaceUrlParams(url, params) + params = {} + } + return Service({ + url: url, + method: 'get', + params: $T.filterParams(params) + }) +} +$http.post = function (url, data = {}) { + return Service({ + url: url, + method: 'post', + data: data + }) +} +$http.put = function (url, data = {}) { + return Service({ + url: url, + method: 'put', + data: data + }) +} +$http.delete = function (url, data = {}) { + return Service({ + url: url, + method: 'delete', + data: data + }) +} +export default $http -- Gitblit v1.8.0