微信小程序的异步请求(微信小程序异步阻塞)

微信免密支付 需要实现异步阻塞模式,通过定时双函数实现详细如下:

myAsyncFunc: Function () { return new Promise(function (resolve, reject) { setTimeout(function () { console.log("myAsyncFunction done!"); resolve({ data: "Hello,World" }); }, 2000); }); }, myAsyncFunc2: function () { return new Promise(function (resolve, reject) { setTimeout(function () { console.log("myAsyncFunction2 done!"); resolve({ data: "I am coming" }); }, 2000); }); }, doit: async function () { var res = await this.myAsyncFunc(); console.log(" res: " res.data); var res2 = await this.myAsyncFunc2(); console.log(" res2: " res2.data); return res2.data res.data ; },

调用的时候

onLoad: function(options) { this.doit().then(res => { console.log(' onLoad: ' res); }) }

打印结果:

微信小程序的异步请求(微信小程序异步阻塞)(1)

本文中下面的代码直接来源于直接项目中,所以无法直接拷贝使用!

1.调用的函数 获取用户信息(new Promise(function (resolve, reject) resolve({ data: ‘xxxx’ });)

/** * 获取OpenId信息 * add by wp 20180906 */ getopenId: function() { return new Promise(function (resolve, reject) { var openId = ''; var seKey = ''; // 登录 let that = this; wx.login({ success: function (res) { // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { //发起网络请求 wx.request({ url: util.url.baseUrl 'wxpro/jscode2session', data: { appid: util.appConfig.appId, secret: util.appConfig.secret, js_code: res.code, grant_type: util.appConfig.grantType, }, header: { "Content-Type": "application/x-www-form-urlencoded" }, method: 'POST', success: function (result) { if(result.data.code == 0){ seKey = result.data.msg.session_key; openId = result.data.msg.openid; wx.setStorageSync(util.key.openId, openId); wx.setStorageSync(util.key.seKey, seKey); resolve({ data: openId }); }else{ return wx.showToast({ title: '请求失败', icon: 'none', duration: 2000 }) } }, fail: function (res) { return wx.showToast({ title: '请求失败', icon: 'none', duration: 2000 }); } }) } else { console.log('--获取openid失败--'); } }, fail: function (res) { console.log('微信登录失败'); } }) }); },

2. 获取地址位置信息(new Promise(function (resolve, reject) resolve({ data: ‘xxxx’ });)

function getLocation(ck) { return new Promise((resolve, reject)=>{ const app = getApp(); wx.getLocation({ type: appConfig.locType, success: function(res) { var lat = res.latitude; var lng = res.longitude; log(TAG, 'lat=' lat ",lng=" lng); if (lat == 'undefined' || lng == 'undefined') return; wx.request({ url: url.locInfo(lat, lng), method: 'GET', success: function(res) { if (res) { var cityInfo = res.data.result.addressComponent; var cityCode = String(cityInfo.adcode).substring(0, 4).concat('00'); var cityName = cityInfo.city; wx.setStorageSync(key.cityName, cityName); wx.setStorageSync(key.cityCode, cityCode); app.globalData.baseInfo.cityCode = cityCode; app.globalData.baseInfo.cityName = cityName; resolve({ data: cityCode }); if(ck && ck.success)ck.success(cityName, cityCode); } else { // ck.fail(res); log("Error", "loc fail..."); } }, fail: function(res) { ck.fail(res); log("Error", res.errMsg); } }) }, fail:function(res){ log(TAG,'获取经纬度失败~'); }, }) }); }

3.获取用户信息(new Promise(function (resolve, reject) resolve({ data: ‘xxxx’ });)

getBaseUserInfo(){ return new Promise(function (resolve, reject) { var openId = wx.getStorageSync(util.key.openId);//用户id var cityCode = wx.getStorageSync(util.key.cityCode); if(!cityCode)return; if (openId == '') return; let that = this; wx.request({ url: util.url.baseUrl 'wxpro/getUserInfo', data: { openId: openId, cityCode: cityCode }, header: { "Content-Type": "application/x-www-form-urlencoded" }, method: 'POST', success: function (res) { console.log(res.data); if (res.data.code == 0 || res.data.code == 1) { wx.setStorageSync(util.key.bindStatus, res.data.msg.bindStatus ? res.data.msg.bindStatus : '0'); var bindStatus = wx.getStorageSync(util.key.bindStatus); if (bindStatus == 0) wx.navigateTo({ url: '../login/login'}); resolve({ data: bindStatus }); } else{ // return wx.showToast({ // title: '获取用户信息失败,错误码:' res.data.code, // icon: 'none', // duration: 2000 // }) } } }) }); }

4.异步阻塞函数响应处理(await)

//异步阻塞操作 add by wupeng 20180925 start: async function () { //获取openid var openId = wx.getStorageSync(util.key.openId);//用户id if (openId == '') { await app.getOpenId(); } //获取获取城市代码 var cityCode = wx.getStorageSync(util.key.cityCode); if (!cityCode) { await util.getLocation(); } //获取绑定状态 var bindStatus = await app.getBaseUserInfo(); //获取证书 var ctf = wx.getStorageSync(util.key.ctf); if (util.isNull(ctf)){ await this.loadCtf(); } return bindStatus.data; },

5.在onload中执行调用 (this.start().then(res =>)

onLoad: function(options) { var bindStatus = app.globalData.baseInfo.bindStatus || wx.getStorageSync(util.key.bindStatus); console.log(TAG ' onLoad: ' bindStatus); if ( 0 == bindStatus){ this.start().then(res => { var city = options.city || wx.getStorageSync(util.key.cityName); this.setData({ loc: { addr: city, } }); if (!options.flag) this.loadCtf(); this.flushQRCode(); this.loadNotify(); this.loadAds(); this.getPhoneHeight(); }) }else{ var city = options.city || wx.getStorageSync(util.key.cityName); this.setData({ loc: { addr: city, } }); if (!options.flag) this.loadCtf(); this.flushQRCode(); this.loadNotify(); this.loadAds(); this.getPhoneHeight(); } },

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页