From edd078a40ff4906aebca6fb02ce9ab62462cce96 Mon Sep 17 00:00:00 2001 From: lijuncheng Date: Mon, 12 Aug 2024 08:48:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B2=A1=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/httpRequest3.js | 130 ++++++++++++++++++++++---------------- src/pages/login/index.vue | 4 +- 2 files changed, 79 insertions(+), 55 deletions(-) diff --git a/src/api/httpRequest3.js b/src/api/httpRequest3.js index 0b362130..30c879c4 100644 --- a/src/api/httpRequest3.js +++ b/src/api/httpRequest3.js @@ -1,10 +1,10 @@ - import storage from '../common/utils/storage' import { removeToken } from '@/common/utils/auth' - + function service(options = {}) { + var token = storage.getStorage(storage.constant.token) var tenantId = storage.getStorage('tenantId') // if(getApp()!=undefined){ @@ -16,63 +16,85 @@ function service(options = {}) { // } var requestUrl = "" requestUrl = import.meta.env.VITE_BASE_URL - + options.header = { "content-type": "application/json", - "Authorization": "Bearer "+token, - "dataType":"json", - "dataSource":"PDA", - "tenant-id":tenantId + "Authorization": "Bearer " + token, + "dataType": "json", + "dataSource": "PDA", + "tenant-id": tenantId }; options.timeout = 300000 - - options.url = requestUrl+options.url - + + options.url = requestUrl + options.url + return new Promise((resolve, reject) => { - options.success = (res) => { - if (res ) { - if(res.statusCode == 200){ - if(res.data.code==0){ - resolve(res.data); - }else { - if(res.data.code==401){ - uni.clearStorageSync() - uni.removeStorageSync('overPackageRecord') // 删除直接翻包中缓存的来源库位 - removeToken(); - uni.reLaunch({ - url: '/pages/login/index' - }) - } - reject("系统异常:"+res.data.msg) - console.log("系统异常",res.data.msg) - } - - }else { - reject("系统异常:"+ res.data.msg) - console.log("系统异常",res.data.msg) - } - + options.success = (res) => { + if (res) { + if (res.statusCode == 200) { + if (res.data.code == 0) { + resolve(res.data); } else { - reject("系统异常:"+res.data.msg) - console.log("系统异常",res.data.msg) - } - }; - options.fail = (error) => { - var message = error.errMsg - if (message === 'Network Error') { - message = '接口连接异常' - } else if (message.includes('timeout')) { - message = '接口请求超时' - } else if (message.includes('Request failed with status code')) { - message = '接口' + message.substr(message.length - 3) + '异常' + if (res.data.code == 401) { + uni.clearStorageSync() + uni.removeStorageSync('overPackageRecord') // 删除直接翻包中缓存的来源库位 + removeToken(); + uni.reLaunch({ + url: '/pages/login/index' + }) + } + reject("系统异常:" + res.data.msg) + console.log("系统异常", res.data.msg) } - reject("系统异常:"+message); - console.log("系统异常",message) - }; - uni.request(options); - }); - } - - - export default service; + } else { + reject("系统异常:" + res.data.msg) + console.log("系统异常", res.data.msg) + } + + } else { + reject("系统异常:" + res.data.msg) + console.log("系统异常", res.data.msg) + } + }; + options.fail = (error) => { + var message = error.errMsg + if (message === 'Network Error') { + message = '接口连接异常' + } else if (message.includes('timeout')) { + message = '接口请求超时' + } else if (message.includes('Request failed with status code')) { + message = '接口' + message.substr(message.length - 3) + '异常' + } + reject("系统异常:" + message); + console.log("系统异常", message) + }; + // #ifdef APP + uni.getNetworkType({ + success: function(res) { + const networkType = res.networkType; + console.log("网络类型", networkType) + if (networkType == "none") { + reject("当前无网络"); + } else { + uni.request(options); + } + } + }) + // #endif + // #ifdef H5 + console.log("网s络H5", navigator.onLine) + if(navigator.onLine){ + uni.request(options); + }else { + reject("当前无网络"); + } + // #endif + + + }); +} + + + +export default service; \ No newline at end of file diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index 20a86f20..46030461 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -261,7 +261,9 @@ this.imageSrc = base64.replace(/[\r\n]/g, "") this.uuid = res.data.uuid } - }) + }).catch(error => { + this.showErrorMessage(error); + }) }, getAppVersion() { const systemInfo = uni.getSystemInfoSync();