From bdb3123664234ec9bfe7f43be21fac7250f9479f Mon Sep 17 00:00:00 2001 From: 王恒 <318726284@qq.com> Date: 星期三, 13 八月 2025 20:00:51 +0800 Subject: [PATCH] '修改字段、处理空值' --- common/http.interceptor.js | 182 ++++++++++++++++++++++---------------------- 1 files changed, 91 insertions(+), 91 deletions(-) diff --git a/common/http.interceptor.js b/common/http.interceptor.js index 9099a99..deb692d 100644 --- a/common/http.interceptor.js +++ b/common/http.interceptor.js @@ -1,96 +1,96 @@ import { - ACCESSTOKEN, - config, - DEBUG + ACCESSTOKEN, + config, + DEBUG } from '@/common/config.js' import util from '@/common/util.js' -function getToken() { - let token = uni.getStorageSync("sessionToken") - return util.isBlank(token) ? '' : token - } -module.exports = (vm) => { - const url = config.serverUrl - uni.$u.http.setConfig((config) => { - config.baseURL = url - config.header = { - 'Accept': 'application/json, text/plain, */*', - // 'Content-Type': 'application/x-www-form-urlencoded', - } - return config - }); - - - // 璇锋眰鎷︽埅锛岄厤缃甌oken绛夊弬鏁� - uni.$u.http.interceptors.request.use((config) => { - // 寮曠敤token - const TOKEN = getToken() || null; - if (TOKEN) { - config.header[ACCESSTOKEN] = TOKEN; - } else { - delete config.header[ACCESSTOKEN] - } - if (config.custom.loading) { - uni.showLoading({ - title: '鍔犺浇涓�', - mask: true - }) - } - DEBUG && console.log('璇锋眰鍙傛暟', config); - return config; - }, config => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣� - return Promise.reject(config) - }) - // 鍝嶅簲鎷︽埅锛屽垽鏂姸鎬佺爜鏄惁閫氳繃 - uni.$u.http.interceptors.response.use((res) => { - uni.hideLoading() - DEBUG && console.log('杩斿洖缁撴灉', res); - if(res.statusCode!=200){ - vm.$u.toast(res.data?.msg || "璇锋眰寮傚父锛�"); //閿欒鎻愮ず淇℃伅 - return false; - } - - if (res.data.code == 200 || !res.data.code) { - return res - } else if (res.data.code == 10002) { - return res - } else if (res.data.code == 401) { - const TOKEN = uni.getStorageSync("sessionToken") - if (TOKEN) { - // vm.$u.vuex('userInfo', null) - // vm.$u.vuex('token', null) - // uni.removeStorageSync("sessionToken") - uni.clearStorageSync() // 娓呮鎵�鏈夌殑缂撳瓨 - uni.showToast({ - title: '鎺堟潈杩囨湡,璇烽噸鏂扮櫥褰�', - icon: 'none', - mask: true - }) - setTimeout(() => { - uni.navigateTo({ - url: '/pages/index/index' - }) - }, 900) - } else { - uni.showToast({ - title: res.data?.msg || "璇锋眰寮傚父锛�", - icon: 'none', - mask: true - }) - } - return false; - } else { - vm.$u.toast(res.data.msg || "璇锋眰寮傚父锛�"); //閿欒鎻愮ず淇℃伅 - return false; - } - - }, (response) => { - // 瀵瑰搷搴旈敊璇仛鐐逛粈涔� 锛坰tatusCode !== 200锛� - uni.hideLoading() - if(response.config.custom?.loading) { - uni.hideLoading() - } - return Promise.reject(response) - }) +function getToken() { + let token = uni.getStorageSync("sessionToken") + return util.isBlank(token) ? '' : token } - +module.exports = (vm) => { + const url = config.serverUrl + uni.$u.http.setConfig((config) => { + config.baseURL = url + config.header = { + 'Accept': 'application/json, text/plain, */*', + // 'Content-Type': 'application/x-www-form-urlencoded', + } + return config + }); + + + // 璇锋眰鎷︽埅锛岄厤缃甌oken绛夊弬鏁� + uni.$u.http.interceptors.request.use((config) => { + // 寮曠敤token + const TOKEN = getToken() || null; + if (TOKEN) { + config.header[ACCESSTOKEN] = TOKEN; + } else { + delete config.header[ACCESSTOKEN] + } + if (config.custom.loading) { + uni.showLoading({ + title: '鍔犺浇涓�', + mask: true + }) + } + DEBUG && console.log('璇锋眰鍙傛暟', config); + return config; + }, config => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣� + return Promise.reject(config) + }) + + // 鍝嶅簲鎷︽埅锛屽垽鏂姸鎬佺爜鏄惁閫氳繃 + uni.$u.http.interceptors.response.use((res) => { + uni.hideLoading() + DEBUG && console.log('杩斿洖缁撴灉', res); + if (res.statusCode != 200) { + vm.$u.toast(res.data?.msg || "璇锋眰寮傚父锛�"); //閿欒鎻愮ず淇℃伅 + return false; + } + + if (res.data.code == 200 || !res.data.code) { + return res + } else if (res.data.code == 10002) { + return res + } else if (res.data.code == 401) { + const TOKEN = uni.getStorageSync("sessionToken") + if (TOKEN) { + // vm.$u.vuex('userInfo', null) + // vm.$u.vuex('token', null) + // uni.removeStorageSync("sessionToken") + uni.clearStorageSync() // 娓呮鎵�鏈夌殑缂撳瓨 + uni.showToast({ + title: '鎺堟潈杩囨湡,璇烽噸鏂扮櫥褰�', + icon: 'none', + mask: true + }) + setTimeout(() => { + uni.reLaunch({ + url: '/pages/index/index' + }) + }, 900) + } else { + uni.showToast({ + title: res.data?.msg || "璇锋眰寮傚父锛�", + icon: 'none', + mask: true + }) + } + return false; + } else { + vm.$u.toast(res.data.msg || "璇锋眰寮傚父锛�"); //閿欒鎻愮ず淇℃伅 + return false; + } + + }, (response) => { + // 瀵瑰搷搴旈敊璇仛鐐逛粈涔� 锛坰tatusCode !== 200锛� + uni.hideLoading() + if (response.config.custom?.loading) { + uni.hideLoading() + } + return Promise.reject(response) + }) +} \ No newline at end of file -- Gitblit v1.9.1