处理网络连接不可用
This commit is contained in:
parent
a9f2f540b6
commit
e9a80b7c8b
27
App.vue
27
App.vue
|
|
@ -13,12 +13,39 @@
|
||||||
console.log('已设置测试 token:', testToken)
|
console.log('已设置测试 token:', testToken)
|
||||||
}
|
}
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
|
// 检测初始网络状态
|
||||||
|
this.checkNetworkStatus()
|
||||||
|
|
||||||
|
// 监听网络状态变化
|
||||||
|
uni.onNetworkStatusChange((res) => {
|
||||||
|
if (res.isConnected === false || res.networkType === 'none') {
|
||||||
|
uni.$uv.toast('网络连接不可用,请检查网络设置')
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
onShow: function() {
|
onShow: function() {
|
||||||
console.log('App Show')
|
console.log('App Show')
|
||||||
|
// 每次显示时检测网络状态
|
||||||
|
this.checkNetworkStatus()
|
||||||
},
|
},
|
||||||
onHide: function() {
|
onHide: function() {
|
||||||
console.log('App Hide')
|
console.log('App Hide')
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
checkNetworkStatus() {
|
||||||
|
uni.getNetworkType({
|
||||||
|
success: (res) => {
|
||||||
|
if (res.networkType === 'none') {
|
||||||
|
uni.$uv.toast('网络连接不可用,请检查网络设置')
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: (err) => {
|
||||||
|
console.error('获取网络状态失败:', err)
|
||||||
|
uni.$uv.toast('无法检测网络状态,请检查网络连接')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -59,3 +59,4 @@ const onLogout = async () => {
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,40 @@ export const Request = () => {
|
||||||
// 初始化请求配置
|
// 初始化请求配置
|
||||||
uni.$uv.http.setConfig((config) => {
|
uni.$uv.http.setConfig((config) => {
|
||||||
/* config 为默认全局配置*/
|
/* config 为默认全局配置*/
|
||||||
// config.baseURL = 'http://192.168.1.5:4001'; /* 根域名 */
|
config.baseURL = 'http://192.168.1.5:4001'; /* 根域名 */
|
||||||
config.baseURL = 'https://pm.ccttiot.com/prod-api'; /* 根域名 */
|
// config.baseURL = 'https://pm.ccttiot.com/prod-api'; /* 根域名 */
|
||||||
return config
|
return config
|
||||||
})
|
})
|
||||||
|
|
||||||
// 请求拦截
|
// 请求拦截
|
||||||
uni.$uv.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
uni.$uv.http.interceptors.request.use(async (config) => { // 可使用async await 做异步操作
|
||||||
|
// 检测网络状态
|
||||||
|
try {
|
||||||
|
const networkInfo = await new Promise((resolve, reject) => {
|
||||||
|
uni.getNetworkType({
|
||||||
|
success: resolve,
|
||||||
|
fail: reject
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// 如果没有网络连接,阻止请求并提示
|
||||||
|
if (networkInfo.networkType === 'none') {
|
||||||
|
uni.$uv.toast('网络连接不可用,请检查网络设置')
|
||||||
|
return Promise.reject({
|
||||||
|
errMsg: '网络连接不可用',
|
||||||
|
networkType: 'none'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取网络状态失败:', error)
|
||||||
|
// 如果获取网络状态失败,也提示用户
|
||||||
|
uni.$uv.toast('无法检测网络状态,请检查网络连接')
|
||||||
|
return Promise.reject({
|
||||||
|
errMsg: '无法检测网络状态',
|
||||||
|
error
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
||||||
config.data = config.data || {}
|
config.data = config.data || {}
|
||||||
|
|
||||||
|
|
@ -62,6 +89,8 @@ export const Request = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (data.code !== 200) {
|
if (data.code !== 200) {
|
||||||
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
|
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
|
||||||
if (custom?.toast !== false) {
|
if (custom?.toast !== false) {
|
||||||
|
|
@ -86,9 +115,35 @@ export const Request = () => {
|
||||||
const { code, msg, message, ...rest } = data
|
const { code, msg, message, ...rest } = data
|
||||||
return Object.keys(rest).length > 0 ? rest : data
|
return Object.keys(rest).length > 0 ? rest : data
|
||||||
}
|
}
|
||||||
}, (response) => {
|
}, async (response) => {
|
||||||
// 对响应错误做点什么 (statusCode !== 200)
|
// 对响应错误做点什么 (statusCode !== 200)
|
||||||
// 网络错误或服务器错误处理
|
// 网络错误或服务器错误处理
|
||||||
|
|
||||||
|
// 检测是否是网络错误
|
||||||
|
if (!response.statusCode || response.statusCode === 0) {
|
||||||
|
// 可能是网络连接问题,再次检测网络状态
|
||||||
|
try {
|
||||||
|
const networkInfo = await new Promise((resolve, reject) => {
|
||||||
|
uni.getNetworkType({
|
||||||
|
success: resolve,
|
||||||
|
fail: reject
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
if (networkInfo.networkType === 'none') {
|
||||||
|
uni.$uv.toast('网络连接不可用,请检查网络设置')
|
||||||
|
return Promise.reject({
|
||||||
|
errMsg: '网络连接不可用',
|
||||||
|
networkType: 'none'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
uni.$uv.toast('网络连接异常,请检查网络设置')
|
||||||
|
}
|
||||||
|
uni.$uv.toast('网络连接异常,请稍后重试')
|
||||||
|
return Promise.reject(response)
|
||||||
|
}
|
||||||
|
|
||||||
if (response.statusCode === 401 || response.statusCode === 403) {
|
if (response.statusCode === 401 || response.statusCode === 403) {
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
userStore.logout()
|
userStore.logout()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user