From 4b4920c2d811f4a8b4fdc6cad7b06454905fc29c Mon Sep 17 00:00:00 2001 From: jazz <jazzxhunter@163.com> Date: 星期二, 26 十二月 2023 19:12:21 +0800 Subject: [PATCH] update_202312261911_卸麻通知和称谓显示v4 --- src/utils/jun_httpEvent.js | 581 ++++++++++++++++++++++++++++++--------------------------- 1 files changed, 308 insertions(+), 273 deletions(-) diff --git a/src/utils/jun_httpEvent.js b/src/utils/jun_httpEvent.js index 6b25175..ff51b2f 100644 --- a/src/utils/jun_httpEvent.js +++ b/src/utils/jun_httpEvent.js @@ -1,6 +1,6 @@ /** * http浜嬩欢濮旀墭 - * + * * udData 璇存槑 * udData.noloading 涓嶉渶瑕佽姹� * udData.fullData 浣跨敤 reponse 杩斿洖鐨勫璞¤繘琛� success 鍥炶皟 锛坖un_http.js锛� @@ -11,13 +11,29 @@ import Login from './jun_login' // import { MessageBox, Message, Loading } from 'element-ui' import fn from './fn' +import xioFn from '../utils/xio_fn.js' import resStatusCode from './jun_httpStatus' // 杩囨护 html -function filterHtml (str) { - if (typeof str !== 'string') { - return str +function filterHtml(str) { + if (typeof str !== 'string') { + return str + } + return str.replace(/\n|\t|\s/g, '').replace(/<script\s?.+><\/script>/g, '').replace(/<[^>]+>/g, '') +} +// 瀵硅薄杞瑄rl鍙傛暟 +function urlEncode(param, key, encode) { + if (param == null) return '' + var paramStr = '' + var t = typeof (param) + if (t === 'string' || t === 'number' || t === 'boolean') { + paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param) + } else { + for (var i in param) { + var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i) + paramStr += urlEncode(param[i], k, encode) } - return str.replace(/\n|\t|\s/g, '').replace(/<script\s?.+><\/script>/g, '').replace(/<[^>]+>/g, '') + }screenLeft + return paramStr } /** @@ -39,65 +55,80 @@ // } // 寮�濮媗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() +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() +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 +function getChangeRequestOption(get_option) { + get_option = xioFn.httpOptionMd5Handle(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 +function postChangeRequestOption(post_option) { + // console.log('============================>', post_option) + // adminToken + // var adminToken = fn.getSessionStorage('adminToken') + // var urlEncodes = {} + // if (adminToken && (!post_option.udData || !post_option.udData.nokey)) { + // urlEncodes = { adminToken: adminToken, mpToken: adminToken } + // } + // // // 灏嗚姹傚弬鏁版斁鍒伴摼鎺ュ悗闈� + // var paramsUrl = urlEncode(urlEncodes) + // if (!/\?/.test(post_option.url)) { + // paramsUrl = paramsUrl.replace('&', '?') + // } + // post_option.url = post_option.url + paramsUrl + + // 璇锋眰澶磎d5澶勭悊 + post_option = xioFn.httpOptionMd5Handle(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) - } + code200() { + // console.log(data, 200) + }, + code404(_url) { + // element-ui + // Message.error('鏃犳硶璁块棶鎺ュ彛锛岀姸鎬�404锛�' + url) + }, + code500(_data, __url) { + // element-ui + // Message.error('璇锋眰澶辫触锛岀姸鎬�500锛�' + url) + } } var g_login_counter = 0 // 鐧诲綍璇锋眰娆℃暟 @@ -109,268 +140,272 @@ 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 - } +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 - } + // 閲嶇疆鐧诲綍璇锋眰鏍囪 + var we_session = fn.getLocalStorage('we_session') + if (we_session && we_session.we_session) { + g_flag_login_requested = false + } } // 璇锋眰鍚� -function afterRequest (res) { - // console.log('璇锋眰鍚�') +function afterRequest(_res) { + // console.log('璇锋眰鍚�') } // 澶勭悊杩斿洖鏁版嵁 // @return {Object} 澶勭悊杩囩殑鏁版嵁 -function successChangeData (res) { - return res +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 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 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) +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'} + 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) - }) + // 鎵撳嵃杩斿洖淇℃伅 + 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 beforeFlow(res) { + return new Promise(async(resolve) => { + // 棰勭暀璇锋眰鍓嶅鐞� + // 璁剧疆涓簍rue锛屼笅娆″鐞嗚烦杩� beforeFlow锛岄伩鍏嶆寰幆 + res.request_option.skip_before_flow = true + // 鍐嶆璇锋眰 + resolve(res.Request(res.request_option)) + }) } // 棰勭暀鍏朵綑澶勭悊 -function configRequest(){ - return new Promise((resolve, reject) => { - resolve({}) - }) +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 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 - } +function afterFlow(res) { + return new Promise((resolve, reject) => { + var data = res.res.data + // console.log('0000000000000000000', res) + // 鐧诲綍瓒呮椂 + 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)) + // todo + // // 娓呯┖鐧诲綍鍑瘉鍜岀敤鎴锋暟鎹� + // sessionStorage.clear() + // // 鏈櫥褰曪紝璺宠浆鐧诲綍椤甸潰 + // this.$router.replace({ path: '/login' }) + 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) - } - } + // 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 + // }) - if (typeof data.code !== 'undefined') { - console.error('code涓嶄负100锛�' + data.code, data.msg, res.res) - } + // element-ui + // Message.error(data.res.errMsg || `璇锋眰鏈夎锛宻tatus=${data.res.status}`) - reject(res.res) - return - } - + // 鏍规嵁status澶勭悊 + if (typeof resStatusCode['status' + data.res.status] === 'function') { + resStatusCode['status' + data.res.status](data) + } + } - // 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};` - } - // 璇锋眰鎻愮ず + if (typeof data.code !== 'undefined') { + console.error('code涓嶄负100锛�' + data.code, data.msg, res.res) + } - // 灏忕▼搴� - // wx.showModal({ - // title: '璇锋眰鎻愮ず', - // content: tips, - // confirmText: '纭畾', - // confirmColor: '#576B95', - // showCancel: false, - // success (res) { - // if (res.confirm) { - // // 鐐瑰嚮纭鍚庨噸鏂拌姹� - // resolve(res.Request(res.request_option)) - // } - // } - // }) + reject(res.res) + return + } - // element-ui - // MessageBox.alert(tips, '缃戠粶鎻愮ず', { - // confirmButtonText: '纭畾', - // type: 'error' - // }).then(()=>{ - // resolve(res.Request(res.request_option)) - // }).catch(()=>{ - // // 鐐瑰嚮鍙栨秷 - // }) - 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};` + } + // 璇锋眰鎻愮ず - // 鎵撳嵃杩斿洖淇℃伅 - 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) - }) + // 灏忕▼搴� + // 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)) - } - }) +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, + httpEventCode, - beforeRequest, - afterRequest, - afterMultiRequests, + getChangeRequestOption, + postChangeRequestOption, + successChangeData, - mockFlow, - beforeFlow, - afterFlow, -} \ No newline at end of file + beforeRequest, + afterRequest, + afterMultiRequests, + + mockFlow, + beforeFlow, + afterFlow +} -- Gitblit v1.8.0