這個是工具類的方法,因爲首頁初次加載要加載很多請求,並且是依次執行。
function requestPromiseRes(url, data) {
var m = 'POST';
var promise = new Promise(function (resolve, reject) {
wx.request({
url: url,
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
},
data: data,
method: m,
success: function (res1) {
if (res1.data.code == "200") {
// success(res1.data.data);
resolve(res1.data.data);
} else {
console.log("數據---自行查看");
resolve(res1.data);
}
},
fail: function (err) {
wx.hideLoading();
wx.showModal({
title: '提示',
content: '網絡請求超時!',
showCancel: false,
success: function (res) { }
})
console.log("err", err);
reject();
}
})
});
return promise;
}
首頁
//首頁加載頁面的所有執行方法
executeMethodList() {
this.getSysTemConfig().then(() => {//讀取配置信息
this.getBanner().then(() => {//獲取輪播圖
this.getGroupOrder().then(() => {//獲取拼團列表
})
})
})
},
//讀取配置信息
getSysTemConfig() {
let that = this
return requestPromiseRes(
url.url.getSysTemConfig,
{
}
).then((res) => {
app.globalData.defaultCity = res.default_city//默認推廣的城市
pageParams.pageParams.endTimeScope = parseInt(res.group_over_time)//拼團結束的事件
app.globalData.appointmentTimeDay = res.order_guide_time//推廣的天數
}).catch((e) => { console.log("回調失敗" + e); })
},
//讀取輪播圖
getBanner() {
let that = this
return requestPromiseRes(
url.url.getBanner,
{
tourist_id: app.globalData.touristId,
}
).then((res) => {
that.setData({
"bannerList": res.adsense
})
}).catch((e) => { console.log("回調失敗" + e); })
},
首頁初次加載獲取openId
getAuthKey: function() {
var that = this;
return new Promise(function(resolve, reject) {
// 登錄從後臺讀取openId
wx.login({
success(res) {
console.log(res)
if (res.code) {
// 發送 res.code 到後臺換取 openId, sessionKey, unionId
wx.request({
url: url.url.getOpenId,
data: {
code: res.code
},
success: function(res) {
console.log(res)
if (res.data.code == '200') {
//告知頁面開始授權訪問
pageParams.pageParams.isState = "2" //告知頁面授權訪問
//openId
//將獲取的openid賦值到全局變量
that.globalData.openId = res.data.openId;
//存儲tourist對象與touristId 賦值
if (res.data.tourist) {
that.globalData.touristId = res.data.tourist.tourist_id;
// that.globalData.touristId = "22fc3a89cebc4bb7827faad93d21820e7"
that.globalData.tourist = res.data.tourist;
}
var res = {
status: 200,
}
resolve(res);
} else if (res.data.code == '401' || res.data.code == '402' || res.data.code == '403') {
//將獲取的openid賦值到全局變量
that.globalData.openId = res.data.openId;
console.log("用戶未綁定跳轉")
wx.navigateTo({
url: '../register/register'
});
}
},
})
} else {
var res = {
status: 300,
}
reject(res);
console.log("獲取參數異常");
}
}
})
});
},