微信小程序如何把接口调用成功的回调函数返回的参数return出去?(promise就可以解决)

举个栗子
//获取应用实例 //const app = getApp() //const util = require('../../utils/util.js') //const sign = util.sign //var Md5 = require('../../utils/md5.js'); Page({ data: { }, onLoad: function () {
//注释:promise 要用then接收 ,或者async await
    let that = this
    that.login().then(res => {
      console.log(res) //打印出  1111 或者 2222
    })
  },
 //函数请求
  login() { 
return new Promise(function (resolve, reject) {
wx.request({ url: app.globalData.url + '/user/wx/login', data: { openId: wx.getStorageSync("openId") }, method: "POST", header: sign(),//根据自己需求 success(res) { console.log(res) if(res.code == 500){ resolve(1111) }else{ resolve(2222) } } }) }) }, })

 

posted @ 2020-02-21 00:07  大熊丨rapper  阅读(3739)  评论(0编辑  收藏  举报