From 9860e221460a0a4ac1903dad2c97160d0eed0e63 Mon Sep 17 00:00:00 2001 From: long <515897141@qq.com> Date: 星期五, 03 三月 2023 10:59:42 +0800 Subject: [PATCH] 初始化 --- src/utils/jun_http.js | 289 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 289 insertions(+), 0 deletions(-) diff --git a/src/utils/jun_http.js b/src/utils/jun_http.js new file mode 100644 index 0000000..71aca81 --- /dev/null +++ b/src/utils/jun_http.js @@ -0,0 +1,289 @@ +/** + * Http 璇锋眰 + * * 灏忕▼搴忚浣跨敤澧炲己缂栬瘧 + * + * 璋冪敤渚嬪瓙 + * Req.http.post({ + * url: '', + * data: {}, + * }) + */ + +import Axios from '../libs/axios' + +/* json杞琭ormdata锛堝簭鍒楀寲锛夛紝浠呮敮鎸佷竴绾у瓧闈㈤噺鍜屽瓧闈㈤噺鏁扮粍 */ +function jsonToFormData (json) { + var arr = [] + var e = encodeURIComponent + for (var key in json) { + var item = json[key] + var res + if (item instanceof Array) { + res = []; + item.map(function (o, i) { + res.push(e(key) + '=' + e(o)) + }); + res = res.join('&') + } else { + res = e(key) + '=' + e(item) + } + arr.push(res) + } + return arr.join('&') +} + +/** + * Http 绫� + * @param http_option {Object} + * -@key {string} baseUrl --璇锋眰閾炬帴鍓嶇疆 + * -@key {object} getChangeRequestParameter --get璇锋眰鍙傛暟 + * -@key {object} postChangeRequestParameter --post璇锋眰鍙傛暟 + * -@key {function} beforeRequest --璇锋眰鍓嶅鐞� + * -@key {function} beforeFlow --璇锋眰鍓嶆祦绋� + * -@key {function} successChangeData --璇锋眰瀹屾垚鍚庯紝澶勭悊鏁版嵁 + * -@key {function} httpEventCode --璇锋眰瀹屾垚鍚庯紝澶勭悊濮旀墭 + * -@key {function} afterFlow --璇锋眰瀹屾垚鍚庢祦绋� + * -@key {function} afterRequest --璇锋眰鍚庝簨浠� + * -@key {function} afterMultiRequests --鍚屾椂澶氭璇锋眰瀹屾垚涔嬪悗 + */ +function Http (http_option) { + // 璇锋眰閰嶇疆鏁扮粍锛屾爣璁版槸鍚︽壒閲忚姹� + var requestArr = [] + // 璇锋眰鎬荤嚎 + function Request (request_option) { + // 榛樿 data 涓哄璞� + request_option.data = request_option.data || {} + // 璇锋眰閰嶇疆鍔犲叆鏁扮粍 + requestArr.push(request_option) + + // 瑙﹀彂璇锋眰鍓嶄簨浠� + if (http_option.beforeRequest) { + http_option.beforeRequest({http_option, request_option}) + } + // 浣跨敤 promise 瀹屾垚璇锋眰 + return new Promise ((resolve, reject) => { + // vue-cli涓敼鐢╩ockjs + // mock鍋囨暟鎹祦绋� + // 20210121 - 鏆傚仠浣跨敤mockjs + if (http_option.mockFlow && http_option.ismock) { + resolve(http_option.mockFlow({http_option, Request, request_option})) + return + } + // 璇锋眰鍓嶈嚜瀹氫箟娴佺▼ + if (http_option.beforeFlow && !request_option.skip_before_flow) { + // skip 涓� true 鏃讹紝涓嶄粎濡傛姝ゆ祦绋� + requestArr.splice(0, 1) + resolve(http_option.beforeFlow({http_option, Request, request_option})) + return + } + var url = request_option.url + if (!url) { + throw new Error('url涓嶈兘涓虹┖') + } + // 涓嶅鍔燿omain璁惧畾 + if (request_option.udData && request_option.udData.nodomain) { + if (!/^https?:\/\//.test(url)) { + // 浣跨敤鏈」鐩煙鍚� + if (/^\//.test(url)) { + url = location.origin + url + } else { + // 淇璁块棶鏈湴json闂 + url = location.origin + location.pathname.replace('index.html', '') + url + } + } + } else if (!/^https?:\/\//.test(url)) { + url = http_option.baseUrl + url + } + // 瀹氫箟璇锋眰閰嶇疆 + var request_config = { + method: request_option.method, + url, + data: request_option.data, + header: request_option.header || {'Content-type':'application/x-www-form-urlencoded'}, + // 璇锋眰鎴愬姛 + success (res) { + // 璇锋眰鎴愬姛鍚庯紝澶勭悊鏁版嵁 + if (http_option.successChangeData) { + res = http_option.successChangeData(res) + } + // 瑙﹀彂 code 濮旀墭浜嬩欢 + if (http_option.httpEventCode && http_option.httpEventCode['code'+res.status]) { + http_option.httpEventCode['code'+res.status](res); + } + // 璇锋眰鍚庤嚜瀹氫箟娴佺▼ + if (http_option.afterFlow) { + resolve(http_option.afterFlow({ + res, + request_config, + request_option, + http_option, + Request, + })) + } + // 瑙﹀彂璇锋眰鍚庝簨浠� + if (http_option.afterRequest) { + http_option.afterRequest({http_option, res}) + } + + if (request_option.udData) { + // 杩斿洖鍏ㄩ儴鏁版嵁 + if (request_option.udData.fullData === true) { + resolve(res) + } + } + resolve(res.data) + }, + + // 璇锋眰澶辫触 + fail (err) { + // console.error(err) + var code = err.response && err.response.status + // 瑙﹀彂 code 濮旀墭浜嬩欢 + if (code && http_option.httpEventCode && http_option.httpEventCode['code'+code]) { + http_option.httpEventCode['code'+code](err, url) + } + // alert('璇锋眰澶辫触锛岄敊璇唬鍙凤細'+code) + reject(err) + }, + + // 鏃犺鎴愬姛鎴栬�呭け璐ラ兘浼氭墽琛� + complete () { + requestArr.splice(0, 1) + if (requestArr.length === 0) { + // http_option.debug && console.log('瑙﹀彂 afterMultiRequests', request_config) + // 鎵归噺璇锋眰鍏ㄩ儴瀹屾垚锛岃Е鍙戜簨浠� + if (http_option.afterMultiRequests) { + http_option.afterMultiRequests(request_option) + } + } + } + }; + + // get 鏂规硶鏃讹紝鍘绘帀 data + if (request_option.method === 'GET') { + delete request_config.data + } + + // 鎵撳嵃璇锋眰淇℃伅 + http_option.debug && console.log('璇锋眰', request_config) + // 澶勭悊header鍜宒ata鐨勫叧绯� + if (request_config.header['Content-type'] == 'application/x-www-form-urlencoded') { + request_config.data = request_config.data ? jsonToFormData(request_config.data) : {} + } + + // 寮�濮嬭姹� + // wx.request(request_config) + Axios({ + method: request_config.method, + headers: request_config.header, + url: request_config.url, + data: request_config.data + }).then((res) => { + // http_option.debug && console.log('鎴愬姛鍥炶皟', res) + request_config.success(res) + request_config.complete(res) + }).catch((res) => { + // http_option.debug && console.log('澶辫触鍥炶皟', res) + request_config.fail(res) + request_config.complete(res) + }) + }) + } + + var obj = { + // get璇锋眰 + // @param get_option {Object} 璇锋眰閰嶇疆 + // -@key url {String} 璇锋眰璺緞 + // -@key params {Object} 璇锋眰鍙傛暟 + // -@key udData {Object} 鑷畾涔夋墿灞曞瓧娈� + + // @return {Promise} + get (get_option) { + // 璇锋眰鍓嶏紝缁熶竴澶勭悊璇锋眰鍙傛暟 + if (http_option.getChangeRequestOption) { + get_option = http_option.getChangeRequestOption(get_option) + } + + // params瀵硅薄搴忓垪鍖栧埌url涓� + if (get_option.params) { + let arr = [] + for (let i in get_option.params) { + if (get_option.params.hasOwnProperty(i)) { + arr.push(encodeURIComponent(i) + '=' + encodeURIComponent(get_option.params[i])) + } + } + if (/\?/.test(get_option.url)) { + get_option.url += '&' + arr.join('&') + } else { + get_option.url += '?' + arr.join('&') + } + + } + + // 浣跨敤promise瀹屾垚Request璋冪敤 + return new Promise ((resolve, reject) => { + Request({ + method: 'GET', + url: get_option.url, + udData: get_option.udData, + header: get_option.header, + mockData: get_option.mockData, + }).then((data) => { + resolve(data) + }).catch((res)=>{ + // 鈫撯啌鈫撯啌鈫撯啌鈫撯啌鈫撯啌 涓存椂澶勭悊澶辫触 + // var junPage = getApp().getCurPage() + // junPage = junPage && junPage.selectComponent('#junPage') + // if (junPage && junPage.fail) { + // console.log('璁剧疆fail') + // junPage.fail() + // } + // 鈫戔啈鈫戔啈鈫戔啈鈫戔啈鈫戔啈 涓存椂澶勭悊澶辫触 + reject(res) + }) + }) + }, + + // post璇锋眰 + // @param post_option {Object} 璇锋眰閰嶇疆 + // -@key url {String} 璇锋眰璺緞 + // -@key data {Object} 璇锋眰鍙傛暟 + // -@key udData {Object} 鑷畾涔夋墿灞曞瓧娈� + + // @return {Promise} + post (post_option) { + // 璇锋眰鍓嶏紝缁熶竴澶勭悊璇锋眰鍙傛暟 + if (http_option.postChangeRequestOption) { + post_option = http_option.postChangeRequestOption(post_option) + } + // 浣跨敤promise瀹屾垚Request璋冪敤 + return new Promise ((resolve, reject) => { + Request({ + method: 'POST', + url: post_option.url, + data: post_option.data, + udData: post_option.udData, + header: post_option.header, + mockData: post_option.mockData, + }).then((data) => { + resolve(data) + }).catch((res)=>{ + // 鈫撯啌鈫撯啌鈫撯啌鈫撯啌鈫撯啌 涓存椂澶勭悊澶辫触 + // var junPage = getApp().getCurPage() + // junPage = junPage && junPage.selectComponent('#junPage') + // if (junPage && junPage.fail) { + // console.log('璁剧疆fail') + // junPage.fail() + // } + // 鈫戔啈鈫戔啈鈫戔啈鈫戔啈鈫戔啈 涓存椂澶勭悊澶辫触 + reject(res) + }) + }) + } + } + obj.getFN = obj.get + obj.postFN = obj.post + return obj; +} + +export default Http \ No newline at end of file -- Gitblit v1.8.0