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_httpEvent.js | 376 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 376 insertions(+), 0 deletions(-) diff --git a/src/utils/jun_httpEvent.js b/src/utils/jun_httpEvent.js new file mode 100644 index 0000000..6b25175 --- /dev/null +++ b/src/utils/jun_httpEvent.js @@ -0,0 +1,376 @@ +/** + * http浜嬩欢濮旀墭 + * + * udData 璇存槑 + * udData.noloading 涓嶉渶瑕佽姹� + * udData.fullData 浣跨敤 reponse 杩斿洖鐨勫璞¤繘琛� success 鍥炶皟 锛坖un_http.js锛� + * udData.nodomain 浠呴檺浜巜eb绔娇鐢紝涓嶉�傜敤 baseUrl 鍓嶇疆锛堝嵆褰撳墠缃戝潃鏍硅矾寰勶級 锛坖un_http.js锛� + * udData.nokey 涓嶉渶瑕乵pToken鍙傛暟 + */ + +import Login from './jun_login' +// import { MessageBox, Message, Loading } from 'element-ui' +import fn from './fn' +import resStatusCode from './jun_httpStatus' +// 杩囨护 html +function filterHtml (str) { + if (typeof str !== 'string') { + return str + } + return str.replace(/\n|\t|\s/g, '').replace(/<script\s?.+><\/script>/g, '').replace(/<[^>]+>/g, '') +} + +/** + * 浣跨敤 element-ui loading service + */ +// let loadingInstance // 瀹氫箟loading鍙橀噺 +// // 寮�濮媗oading +// function startLoading(){ +// loadingInstance = Loading.service({ +// lock: true, +// text: '鍔犺浇涓�...', +// background: 'rgba(255,255,255,0.7)', +// fullscreen: true, +// }) +// } +// // 缁撴潫loading +// function endLoading(){ +// loadingInstance && loadingInstance.close() +// } + +// 寮�濮媗oading +function startLoading(request_option){ + // + // console.log(request_option.udData) + // request_option.udData && typeof request_option.udData.loading === 'function' && request_option.udData.loading() + typeof window.appLoading === 'function' && window.appLoading() +} + +// 缁撴潫loading +function endLoading(request_option){ + // + // request_option.udData && typeof request_option.udData.hideLoading === 'function' && request_option.udData.hideLoading() + typeof window.appHideLoading === 'function' && window.appHideLoading() +} + +// 璇锋眰鍓嶅鐞嗗弬鏁� - get +function getChangeRequestOption (get_option) { + if (!get_option.udData || !get_option.udData.nokey) { + if (!get_option.params) { + get_option.params = {} + } + // 琛ュ厖鍙傛暟 + // 闇�瑕� we_session + var we_session = fn.getLocalStorage('we_session') + if (we_session && we_session.we_session) { + get_option.params.key = we_session.we_session + get_option.params.mpToken = we_session.we_session + } + } + return get_option +} +// 璇锋眰鍓嶅鐞嗗弬鏁� - post +function postChangeRequestOption (post_option) { + if (!post_option.udData || !post_option.udData.nokey) { + if (!post_option.data) { + post_option.data = {} + } + // 琛ュ厖鍙傛暟 + // 闇�瑕� we_session + var we_session = fn.getLocalStorage('we_session') + if (we_session && we_session.we_session) { + post_option.data.key = we_session.we_session + post_option.data.mpToken = we_session.we_session + } + } + return post_option +} + +var httpEventCode = { + code200 (data) { + // console.log(data, 200) + }, + code404 (data, url) { + // element-ui + // Message.error('鏃犳硶璁块棶鎺ュ彛锛岀姸鎬�404锛�' + url) + }, + code500 (data, url) { + // element-ui + // Message.error('璇锋眰澶辫触锛岀姸鎬�500锛�' + url) + } +} + +var g_login_counter = 0 // 鐧诲綍璇锋眰娆℃暟 +const g_LOGIN_MAX = 10 // 鏈�澶ц姹傛鏁� + +var g_flag_loading = false // 鏄惁宸茬粡loading +var g_flag_login_requested = false // 鏄惁姝e湪璇锋眰鐧诲綍 +var g_login_result = null // 鐧诲綍杩斿洖鏁版嵁 +var g_flag_config_requested = false // 鍏朵綑閰嶇疆璇锋眰 +var g_config_result = null // 閰嶇疆璇锋眰杩斿洖 +// 璇锋眰鍓� +function beforeRequest (res) { + // 寮�鍚痩oading + if (!g_flag_loading && (!res.request_option.udData || !res.request_option.udData.noLoading)) { + res.http_option.debug && console.log('jun_httpEvent beforeRequest loading') + // wx.showLoading({ + // title: '鍔犺浇涓�', + // mask: true + // }) + // loading + startLoading(res.request_option) + g_flag_loading = true + } + + // 閲嶇疆鐧诲綍璇锋眰鏍囪 + var we_session = fn.getLocalStorage('we_session') + if (we_session && we_session.we_session) { + g_flag_login_requested = false + } +} + +// 璇锋眰鍚� +function afterRequest (res) { + // console.log('璇锋眰鍚�') +} + +// 澶勭悊杩斿洖鏁版嵁 +// @return {Object} 澶勭悊杩囩殑鏁版嵁 +function successChangeData (res) { + return res +} + +// 鎵归噺璇锋眰瀹屾垚 +function afterMultiRequests (request_option) { + // console.log("澶氫釜璇锋眰缁撴潫涔嬪悗") + // 鍏抽棴loading + if (g_flag_loading) { + // wx.hideLoading() + endLoading(request_option) + g_flag_loading = false + } +} + +function updateKey (res, key) { + // 鏇存柊 key 鍊� + if (res.request_option.method === 'GET' && (!res.request_option.udData || !res.request_option.udData.nokey)) { + res.request_option.url = res.request_option.url.replace(/mpToken=[^&]*/g, `mpToken=${key}`) + } + if (res.request_option.method === 'POST' && (!res.request_option.udData || !res.request_option.udData.nokey)) { + res.request_option.data.mpToken = key + } + return res +} + +// 妯℃嫙鏁版嵁澶勭悊娴佺▼ +function mockFlow (res) { + return new Promise(async (resolve, reject) => { + // 鎵撳嵃璇锋眰淇℃伅 + res.http_option.debug && console.log('妯℃嫙璇锋眰', res.request_option) + + var mockData = res.request_option.mockData || {code: 100, data: {}, message: 'success'} + + // 鎵撳嵃杩斿洖淇℃伅 + res.http_option.debug && console.log('妯℃嫙杩斿洖', {data: mockData}) + res.http_option.debug && console.log('寮�濮嬫ā鎷熺瓑寰�800ms') + var timer = setTimeout(()=>{ + clearTimeout(timer) + // 鍏抽棴loading + if (g_flag_loading) { + // wx.hideLoading() + endLoading(res.request_option) + g_flag_loading = false + } + res.http_option.debug && console.log('缁撴潫妯℃嫙绛夊緟800ms') + if (res.request_option.udData && res.request_option.fullData) { + resolve({data: mockData}) + } else { + resolve(mockData) + } + }, 800) + }) +} + +// 璇锋眰鍓嶅鐞嗘祦绋� +function beforeFlow (res) { + return new Promise(async (resolve, reject) => { + // 棰勭暀璇锋眰鍓嶅鐞� + // 璁剧疆涓簍rue锛屼笅娆″鐞嗚烦杩� beforeFlow锛岄伩鍏嶆寰幆 + res.request_option.skip_before_flow = true + // 鍐嶆璇锋眰 + resolve(res.Request(res.request_option)) + }) +} + +// 棰勭暀鍏朵綑澶勭悊 +function configRequest(){ + return new Promise((resolve, reject) => { + resolve({}) + }) +} + +// 鐧诲綍澶勭悊 +function appLogin (option={}) { + return new Promise((resolve, reject) => { + // Login.checkLogin({ + // force: !!option.forceLogin, + // callback: (key)=>{ + // resolve(key) + // } + // }) + Login.toLongUrl() + }) +} + +// 璇锋眰鍚庡鐞� +function afterFlow (res) { + return new Promise((resolve, reject)=>{ + var data = res.res.data + + // 鐧诲綍瓒呮椂 + if ( + (data && data.res && data.res.status == 2) + || (data && data.code == 603) + ) { + res.http_option.debug && console.log('鐧诲綍瓒呮椂锛岄渶瑕侀噸鏂扮櫥褰�', res.res) + // 鐧诲綍瓒呮椂锛岄渶瑕侀噸鏂扮櫥褰� + // 娓呯┖we_session + // getApp().globalData.we_session = null + fn.removeLocalStorage('we_session') + resolve(retryLogin(res)) + return + } + + // status涓嶄负0 + if ( + (data && data.res && data.res.status != 0) + || (data && data.code != 100) + ) { + if (data.res) { + console.error('status涓嶄负0锛�' + (res.res.errMsg || ''), res.res) + // 寮瑰嚭鎻愮ず + // wx.showModal({ + // title: '璇锋眰鎻愮ず', + // content: data.res.errMsg || `璇锋眰鏈夎锛宻tatus=${data.res.status}`, + // confirmText: '纭畾', + // confirmColor: '#576B95', + // showCancel: false + // }) + + // element-ui + // Message.error(data.res.errMsg || `璇锋眰鏈夎锛宻tatus=${data.res.status}`) + + // 鏍规嵁status澶勭悊 + if (typeof resStatusCode['status' + data.res.status] === 'function') { + resStatusCode['status' + data.res.status](data) + } + } + + if (typeof data.code !== 'undefined') { + console.error('code涓嶄负100锛�' + data.code, data.msg, res.res) + } + + reject(res.res) + return + } + + + // status涓虹┖ + if (data && !data.res && typeof data.code === 'undefined') { + console.error('status涓虹┖', res.res) + let tips = '' + if (res.res.statusCode=='500') { + tips = '褰撳墠缃戠粶鐘舵�佷笉浣筹紝璇风◢鍚庡啀璇曪細statusCode=500;' + } else if (res.res.statusCode=='200') { + if (typeof res.data === 'string') { + tips = filterHtml(res.data) + } + if (typeof res.data === 'object') { + tips = filterHtml(JSON.stringify(res.data)) + } + tips = '缃戠粶鐘跺喌涓嶄匠锛岀偣鍑荤‘瀹氶噸璇曪細statusCode=200;' + tips + } else { + tips = `杩炴帴鏈嶅姟鍣ㄥけ璐ワ紝鐐瑰嚮纭畾閲嶈瘯锛歴tatusCode=${res.res.statusCode};` + } + // 璇锋眰鎻愮ず + + // 灏忕▼搴� + // wx.showModal({ + // title: '璇锋眰鎻愮ず', + // content: tips, + // confirmText: '纭畾', + // confirmColor: '#576B95', + // showCancel: false, + // success (res) { + // if (res.confirm) { + // // 鐐瑰嚮纭鍚庨噸鏂拌姹� + // resolve(res.Request(res.request_option)) + // } + // } + // }) + + // element-ui + // MessageBox.alert(tips, '缃戠粶鎻愮ず', { + // confirmButtonText: '纭畾', + // type: 'error' + // }).then(()=>{ + // resolve(res.Request(res.request_option)) + // }).catch(()=>{ + // // 鐐瑰嚮鍙栨秷 + // }) + return + } + + // 鎵撳嵃杩斿洖淇℃伅 + res.http_option.debug && console.log('杩斿洖', res.res) + if (res.request_option.udData) { + // 杩斿洖鍏ㄩ儴鏁版嵁 + if (res.request_option.udData.fullData === true) { + resolve(res.res) + } + } + g_login_counter && (g_login_counter = 0) + resolve(data) + }) +} + +// 閲嶆柊鐧诲綍 +async function retryLogin (res) { + // 娆℃暟+1 + g_login_counter++ + return new Promise(async (resolve, reject) => { + // 绛夊緟鐧诲綍 + var login_result = await appLogin({forceLogin: true}) + // 鐧诲綍澶辫触 + if (!login_result && g_login_counter < g_LOGIN_MAX) { + // 2绉掑悗閲嶈瘯 + var timer = setTimeout(()=>{ + clearTimeout(timer) + resolve(retryLogin(res)) + }, 2000) + } + // 鐧诲綍鎴愬姛 + if (login_result) { + // 鏇存柊key鍊� + res = updateKey(res, login_result) + // 鍐嶆璇锋眰 + resolve(res.Request(res.request_option)) + } + }) +} + +export default { + httpEventCode, + + getChangeRequestOption, + postChangeRequestOption, + successChangeData, + + beforeRequest, + afterRequest, + afterMultiRequests, + + mockFlow, + beforeFlow, + afterFlow, +} \ No newline at end of file -- Gitblit v1.8.0