没更新小程序的情况下,2023年7月20日突然小程序真机调试出现:WAServiceMainContext.js:1 [wxapplib]] Uncaught (in promise) FrameworkError {"errMsg":"hideLoading:fail:toast can't be found"}。
用电脑模拟器则显示为正常,什么原因呢?
代码片段:
var _ = require('underscore.js');
var md5 = require('md5.js');
var app = getApp();
var option = option ? option : {};
option.cachetime = option.cachetime ? option.cachetime : 0;
option.showLoading = typeof option.showLoading != 'undefined' ? option.showLoading : true;
var sessionid = wx.getStorageSync('userInfo').sessionid;
var url = option.url;
if (url.indexOf('http://') == -1 && url.indexOf('https://') == -1) {
url = util.url(url);
}
var state = getUrlParam(url, 'state');
if (!state && !(option.data && option.data.state) && sessionid) {
url = url + '&state=we7sid-' + sessionid
}
if (!option.data || !option.data.m) {
var nowPage = getCurrentPages();
if (nowPage.length) {
nowPage = nowPage[getCurrentPages().length - 1];
if (nowPage && nowPage.__route__) {
url = url + '&m=' + nowPage.__route__.split('/')[0];
}
}
}
var sign = getSign(url, option.data);
if (sign) {
url = url + "&sign=" + sign;
}
if (!url) {
return false;
}
wx.showNavigationBarLoading();
if (option.showLoading) {
util.showLoading();
}
if (option.cachetime) {
var cachekey = md5(url);
var cachedata = wx.getStorageSync(cachekey);
var timestamp = Date.parse(new Date());
if (cachedata && cachedata.data) {
if (cachedata.expire > timestamp) {
if (option.complete && typeof option.complete == 'function') {
option.complete(cachedata);
}
if (option.success && typeof option.success == 'function') {
option.success(cachedata);
}
console.log('cache:' + url);
util.hideLoading();//wx.hideLoading();
wx.hideNavigationBarLoading();
return true;
} else {
wx.removeStorageSync(cachekey)
}
}
}
wx.request({
'url': url,
'data': option.data ? option.data : {},
'header': option.header ? option.header : {},
'method': option.method ? option.method : 'GET',
'header': {
'content-type': 'application/x-www-form-urlencoded'
},
'success': function (response) {
wx.hideNavigationBarLoading();
util.hideLoading();//wx.hideLoading();
if (response.data.errno) {
if (response.data.errno == '41009') {
wx.setStorageSync('userInfo', '');
util.getUserInfo(function () {
util.request(option)
});
return;
} else {
if (option.fail && typeof option.fail == 'function') {
option.fail(response);
} else {
if (response.data.message) {
if (response.data.data != null && response.data.data.redirect) {
var redirect = response.data.data.redirect;
} else {
var redirect = '';
}
app.util.message(response.data.message, redirect, 'error');
}
}
return;
}
} else {
if (option.success && typeof option.success == 'function') {
option.success(response);
}
//写入缓存,减少HTTP请求,并且如果网络异常可以读取缓存数据
if (option.cachetime) {
var cachedata = { 'data': response.data, 'expire': timestamp + option.cachetime * 1000 };
wx.setStorageSync(cachekey, cachedata);
}
}
},
'fail': function (response) {
wx.hideNavigationBarLoading();
util.hideLoading();//wx.hideLoading();
//如果请求失败,尝试从缓存中读取数据
var md5 = require('md5.js');
var cachekey = md5(url);
var cachedata = wx.getStorageSync(cachekey);
if (cachedata && cachedata.data) {
if (option.success && typeof option.success == 'function') {
option.success(cachedata);
}
console.log('failreadcache:' + url);
return true;
} else {
if (option.fail && typeof option.fail == 'function') {
option.fail(response);
}
}
},
'complete': function (response) {
// wx.hideNavigationBarLoading();
// wx.hideLoading();
if (option.complete && typeof option.complete == 'function') {
option.complete(response);
}
}
});