我是靠谱客的博主 热心航空,这篇文章主要介绍微信小程序网络超时怎么办?,现在分享给大家,希望可以做个参考。

微信小程序网络超时的解决办法:

onLaunch通过这个我们可以获取用户的基本信息,或者定位用来做下一步处理,如果这个无法获取数据,会导致整个小程序的失败。

所以我建议可以把错误分两个级别,假如是开发者服务器连接不上,可以通过重载页面来处理,但是如果是onLaunch中的数据也无法获取就必须让用户退出小程序,重新打开再试了。

app.json 中配置用来设置超时时间,默认为6000毫秒,也就是6秒

复制代码
1
2
3
4
"networkTimeout": { "request": 6000, "downloadFile": 10000 }
登录后复制

一、在onLaunch运行任务如果超时,我把错误级别定为0,并转向错误页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
wx.login({ success(res) { if (res.code) { //console.log(res.code); //发起网络请求 wx.request({ url: 'https://**/index/zz/getuserinfo', data: { code: res.code }, success: res => { wx.setStorageSync('open_id', res.data.openid); wx.setStorageSync('session_id', res.data.session_id); wx.setStorageSync('session_key', res.data.session_key); that.globalData.isSessionkey=true; //console.log(res.data); if (that.sessionCallback) { that.sessionCallback(res); } },fail:f=> { wx.showModal({ title: '提示', showCancel: false, content: '可能网络不太好,请重试!', success: function () { wx.navigateTo({ url: '/pages/reload?error=0' }); } }); } }) } else { console.log('登录失败!' + res.errMsg) } }, fail: function () { wx.showModal({ title: '提示', showCancel: false, content: '可能网络不太好,请重试!', success: function () { wx.navigateTo({ url: '/pages/reload?error=0' }); } }); } });
登录后复制

二、如果是页面请求超时,我把错误定为2,可以通过重试来修复

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
wx.request({ url: webUrl + model.url, data: model.param, method: model.method, success: function (res) { }, fail: function (res) { wx.hideLoading(); wx.showModal({ title: '提示', showCancel: false, content: '可能网络不太好,请重试!', success: function () { wx.navigateTo({ url: '/pages/reload?error=1' }); } }); } })
登录后复制

三、处理页面:要使用getCurrentPages()获取上一页对象,必须使用wx.navigateTo转向此页

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/** * 页面的初始数据 */ data: { error:0 // 0:需要退出小程序 1:可以重新发起网络请求重试 }, reLoad:function(error) { var pages = getCurrentPages();//获取页面栈 if (pages.length > 1) { //上一个页面实例对象 var prePage = pages[pages.length - 2]; let url=prePage.route; var options = prePage.options //如果要获取url中所带的参数可以查看options console.log('options', options); //拼接url的参数 var urlWithArgs = url + '?' for (var key in options) { var value = options[key] urlWithArgs += key + '=' + value + '&' } urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1) wx.reLaunch({ url: '/' + urlWithArgs, fail:function(e) { wx.switchTab({ url: '/' + prePage.route, }) } }); } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ error: options.error}); // this.reLoad(options.error); },
登录后复制

以上就是微信小程序网络超时怎么办?的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是热心航空最近收集整理的关于微信小程序网络超时怎么办?的全部内容,更多相关微信小程序网络超时怎么办内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(90)

评论列表共有 0 条评论

立即
投稿
返回
顶部