Browse Source

修改没网络请求提示

lijuncheng0816
lijuncheng 3 months ago
parent
commit
edd078a40f
  1. 118
      src/api/httpRequest3.js
  2. 4
      src/pages/login/index.vue

118
src/api/httpRequest3.js

@ -1,10 +1,10 @@
import storage from '../common/utils/storage' import storage from '../common/utils/storage'
import { import {
removeToken removeToken
} from '@/common/utils/auth' } from '@/common/utils/auth'
function service(options = {}) { function service(options = {}) {
var token = storage.getStorage(storage.constant.token) var token = storage.getStorage(storage.constant.token)
var tenantId = storage.getStorage('tenantId') var tenantId = storage.getStorage('tenantId')
// if(getApp()!=undefined){ // if(getApp()!=undefined){
@ -19,60 +19,82 @@ function service(options = {}) {
options.header = { options.header = {
"content-type": "application/json", "content-type": "application/json",
"Authorization": "Bearer "+token, "Authorization": "Bearer " + token,
"dataType":"json", "dataType": "json",
"dataSource":"PDA", "dataSource": "PDA",
"tenant-id":tenantId "tenant-id": tenantId
}; };
options.timeout = 300000 options.timeout = 300000
options.url = requestUrl+options.url options.url = requestUrl + options.url
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
options.success = (res) => { options.success = (res) => {
if (res ) { if (res) {
if(res.statusCode == 200){ if (res.statusCode == 200) {
if(res.data.code==0){ if (res.data.code == 0) {
resolve(res.data); 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)
}
} else { } else {
reject("系统异常:"+res.data.msg) if (res.data.code == 401) {
console.log("系统异常",res.data.msg) uni.clearStorageSync()
} uni.removeStorageSync('overPackageRecord') // 删除直接翻包中缓存的来源库位
}; removeToken();
options.fail = (error) => { uni.reLaunch({
var message = error.errMsg url: '/pages/login/index'
if (message === 'Network Error') { })
message = '接口连接异常' }
} else if (message.includes('timeout')) { reject("系统异常:" + res.data.msg)
message = '接口请求超时' console.log("系统异常", res.data.msg)
} else if (message.includes('Request failed with status code')) {
message = '接口' + message.substr(message.length - 3) + '异常'
} }
reject("系统异常:"+message);
console.log("系统异常",message) } else {
}; reject("系统异常:" + res.data.msg)
uni.request(options); 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; export default service;

4
src/pages/login/index.vue

@ -261,7 +261,9 @@
this.imageSrc = base64.replace(/[\r\n]/g, "") this.imageSrc = base64.replace(/[\r\n]/g, "")
this.uuid = res.data.uuid this.uuid = res.data.uuid
} }
}) }).catch(error => {
this.showErrorMessage(error);
})
}, },
getAppVersion() { getAppVersion() {
const systemInfo = uni.getSystemInfoSync(); const systemInfo = uni.getSystemInfoSync();

Loading…
Cancel
Save