From 3fc70951bbd386c8c85e522cae80deba8823052e Mon Sep 17 00:00:00 2001 From: zhangwei <1504152376@qq.com> Date: 星期四, 15 八月 2024 17:28:20 +0800 Subject: [PATCH] 登录 --- src/common/request/request.js | 238 +++++++++++++++++++++++++++++++---------------------------- 1 files changed, 126 insertions(+), 112 deletions(-) diff --git a/src/common/request/request.js b/src/common/request/request.js index acebc6c..7833e73 100644 --- a/src/common/request/request.js +++ b/src/common/request/request.js @@ -1,117 +1,131 @@ -锘縤mport { apiBaseUrl } from '@/common/setting/constVarsHelper.js'; +锘� +// 寮曞叆luch-request +import { http } from '@/uni_modules/uview-plus' +import { + apiBaseUrl +} from '@/common/setting/constVarsHelper.js'; import * as db from '@/common/utils/dbHelper.js' //寮曞叆common - -// 姝m鍙傛暟涓洪〉闈㈢殑瀹炰緥锛屽彲浠ラ�氳繃瀹冨紩鐢╲uex涓殑鍙橀噺 -module.exports = (vm) => { - // 鍒濆鍖栬姹傞厤缃� - uni.$u.http.setConfig((defaultConfig) => { - /* defaultConfig 涓洪粯璁ゅ叏灞�閰嶇疆 */ - defaultConfig.baseURL = apiBaseUrl; /* 鏍瑰煙鍚� */ - //defaultConfig.header = { - // 'Content-type': 'application/json1' - //}; - defaultConfig.method = 'POST'; - defaultConfig.dataType = 'json'; - // #ifndef MP-ALIPAY - defaultConfig.responseType = 'text'; - // #endif - // 娉細濡傛灉灞�閮╟ustom涓庡叏灞�custom鏈夊悓鍚嶅睘鎬э紝鍒欏悗闈㈢殑灞炴�т細瑕嗙洊鍓嶉潰鐨勫睘鎬э紝鐩稿綋浜嶰bject.assign(鍏ㄥ眬锛屽眬閮�) - //defaultConfig.custom = {}; // 鍏ㄥ眬鑷畾涔夊弬鏁伴粯璁ゅ�� - // #ifdef APP-PLUS || MP-ALIPAY || MP-WEIXIN - defaultConfig.timeout = 60000; - // #endif - // #ifdef APP-PLUS - defaultConfig.sslVerify = true; - // #endif - // #ifdef APP-PLUS - defaultConfig.firstIpv4 = false; // DNS瑙f瀽鏃朵紭鍏堜娇鐢╥pv4 浠� App-Android 鏀寔 (HBuilderX 2.8.0+) - // #endif - // 灞�閮ㄤ紭鍏堢骇楂樹簬鍏ㄥ眬锛岃繑鍥炲綋鍓嶈姹傜殑task,options銆傝鍕垮湪姝ゅ淇敼options銆傞潪蹇呭~ - // getTask: (task, options) => { - // 鐩稿綋浜庤缃簡璇锋眰瓒呮椂鏃堕棿500ms - // setTimeout(() => { - // task.abort() - // }, 500) - // }, - // 鍏ㄥ眬鑷畾涔夐獙璇佸櫒銆傚弬鏁颁负statusCode 涓斿繀瀛樺湪锛屼笉鐢ㄥ垽鏂┖鎯呭喌銆� - defaultConfig.validateStatus = (statusCode) => { // statusCode 蹇呭瓨鍦ㄣ�傛澶勭ず渚嬩负鍏ㄥ眬榛樿閰嶇疆 - return statusCode >= 200 && statusCode < 501 - } - - return defaultConfig - }) - - // 璇锋眰鎷︽埅 - uni.$u.http.interceptors.request.use((config) => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣� - // 鍒濆鍖栬姹傛嫤鎴櫒鏃讹紝浼氭墽琛屾鏂规硶锛屾鏃禿ata涓簎ndefined锛岃祴浜堥粯璁} - config.data = config.data || {} - if (config?.custom?.needToken) { - // 鑾峰彇鐢ㄦ埛token - const userToken = db.get("userToken"); - if (!userToken) { - //console.log("寮�鍚脊绐�"); - vm.$store.commit('showLoginTip', true); - //console.log("寮圭獥宸茬粡寮�鍚�"); - // return false; - } else { - config.header.Authorization = 'Bearer ' + userToken; +// 鍒濆鍖栬姹傞厤缃� +const initRequest=(vm)=>{ + http.setConfig((defaultConfig) => { + /* defaultConfig 涓洪粯璁ゅ叏灞�閰嶇疆 */ + defaultConfig.baseURL = apiBaseUrl; /* 鏍瑰煙鍚� */ + //defaultConfig.header = { + // 'Content-type': 'application/json1' + //}; + defaultConfig.method = 'POST'; + defaultConfig.dataType = 'json'; + // #ifndef MP-ALIPAY + defaultConfig.responseType = 'text'; + // #endif + // 娉細濡傛灉灞�閮╟ustom涓庡叏灞�custom鏈夊悓鍚嶅睘鎬э紝鍒欏悗闈㈢殑灞炴�т細瑕嗙洊鍓嶉潰鐨勫睘鎬э紝鐩稿綋浜嶰bject.assign(鍏ㄥ眬锛屽眬閮�) + //defaultConfig.custom = {}; // 鍏ㄥ眬鑷畾涔夊弬鏁伴粯璁ゅ�� + // #ifdef APP-PLUS || MP-ALIPAY || MP-WEIXIN + defaultConfig.timeout = 60000; + // #endif + // #ifdef APP-PLUS + defaultConfig.sslVerify = true; + // #endif + // #ifdef APP-PLUS + defaultConfig.firstIpv4 = false; // DNS瑙f瀽鏃朵紭鍏堜娇鐢╥pv4 浠� App-Android 鏀寔 (HBuilderX 2.8.0+) + // #endif + // 灞�閮ㄤ紭鍏堢骇楂樹簬鍏ㄥ眬锛岃繑鍥炲綋鍓嶈姹傜殑task,options銆傝鍕垮湪姝ゅ淇敼options銆傞潪蹇呭~ + // getTask: (task, options) => { + // 鐩稿綋浜庤缃簡璇锋眰瓒呮椂鏃堕棿500ms + // setTimeout(() => { + // task.abort() + // }, 500) + // }, + // 鍏ㄥ眬鑷畾涔夐獙璇佸櫒銆傚弬鏁颁负statusCode 涓斿繀瀛樺湪锛屼笉鐢ㄥ垽鏂┖鎯呭喌銆� + defaultConfig.validateStatus = (statusCode) => { // statusCode 蹇呭瓨鍦ㄣ�傛澶勭ず渚嬩负鍏ㄥ眬榛樿閰嶇疆 + return statusCode >= 200 && statusCode < 501 } - - } - - //棰濆闇�姹� - if (config.custom.methodName == 'user.share'||config.custom.methodName =="pages.getpageconfig" || config.custom.methodName == "goods.goodsList") { - const userToken = db.get("userToken"); - config.header.Authorization = 'Bearer ' + userToken; - } - return config - }, config => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣� - return Promise.reject(config) - }) + return defaultConfig + }) - // 鍝嶅簲鎷︽埅 - uni.$u.http.interceptors.response.use((response) => { /* 瀵瑰搷搴旀垚鍔熷仛鐐逛粈涔� 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�*/ - //console.log(response); - const data = response.data - if (response.statusCode == 200) { - let pages = getCurrentPages(); - var page = pages[pages.length - 1]; + // 璇锋眰鎷︽埅 + http.interceptors.request.use((config) => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣� + // 鍒濆鍖栬姹傛嫤鎴櫒鏃讹紝浼氭墽琛屾鏂规硶锛屾鏃禿ata涓簎ndefined锛岃祴浜堥粯璁} + config.data = config.data || {} + if (config?.custom?.needToken) { + // 鑾峰彇鐢ㄦ埛token + const userToken = db.get("userToken"); + if (!userToken) { + //console.log("寮�鍚脊绐�"); + vm.$store.commit('showLoginTip', true); + //console.log("寮圭獥宸茬粡寮�鍚�"); + // return false; + } else { + config.header.Authorization = 'Bearer ' + userToken; + } - if (!data.status && page) { - // 鐧诲綍淇℃伅杩囨湡鎴栬�呮湭鐧诲綍 - if (data.data === 14007 || data.data === 14006) { - // #ifdef APP-PLUS || APP-PLUS-NVUE - if (page.route.indexOf('pages/login/index') < 0) { - db.del("userToken"); - uni.showToast({ - title: result.msg, - icon: 'none', - duration: 1000, - complete: function () { - setTimeout(function () { - uni.hideToast(); - uni.navigateTo({ - url: '/pages/login/index' - }); - }, - 1000); - } - }); - } - // #endif - // #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO - db.del("userToken"); - vm.$store.commit('userInfo', null); - //console.log("寮�鍚櫥褰曞脊绐�"); - vm.$store.commit('hasLogin', false); - // #endif - } - } - } - return data === undefined ? {} : data - }, (response) => { - // 瀵瑰搷搴旈敊璇仛鐐逛粈涔� 锛坰tatusCode !== 200锛� - return Promise.reject(response) - }) -} \ No newline at end of file + } + + //棰濆闇�姹� + if (config.custom.methodName == 'user.share' || config.custom.methodName == "pages.getpageconfig" || + config.custom.methodName == "goods.goodsList") { + const userToken = db.get("userToken"); + config.header.Authorization = 'Bearer ' + userToken; + } + + return config + }, config => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣� + return Promise.reject(config) + }) + + // 鍝嶅簲鎷︽埅 + http.interceptors.response.use((response) => { + /* 瀵瑰搷搴旀垚鍔熷仛鐐逛粈涔� 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�*/ + //console.log(response); + const data = response.data + if (response.statusCode == 200) { + let pages = getCurrentPages(); + var page = pages[pages.length - 1]; + + if (!data.status && page) { + // 鐧诲綍淇℃伅杩囨湡鎴栬�呮湭鐧诲綍 + if (data.data === 14007 || data.data === 14006) { + // #ifdef APP-PLUS || APP-PLUS-NVUE + if (page.route.indexOf('pages/login/index') < 0) { + db.del("userToken"); + uni.showToast({ + title: result.msg, + icon: 'none', + duration: 1000, + complete: function() { + setTimeout(function() { + uni.hideToast(); + uni.navigateTo({ + url: '/pages/login/index' + }); + }, + 1000); + } + }); + } + // #endif + // #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO + db.del("userToken"); + vm.$store.commit('userInfo', null); + //console.log("寮�鍚櫥褰曞脊绐�"); + vm.$store.commit('hasLogin', false); + // #endif + } + } + } + return data === undefined ? {} : data + }, (response) => { + // 瀵瑰搷搴旈敊璇仛鐐逛粈涔� 锛坰tatusCode !== 200锛� + return Promise.reject(response) + }) + // requestInterceptors() + // responseInterceptors() +} +export { + initRequest +} + + + + -- Gitblit v1.9.1