由於wx.login是異步函數,導致在我們獲取微信小程序返回的code去請求我們的登錄接口時code的值會異常。現在用promise封裝一下,將他success的結果返回,在登陸函數中await就可以了。
loginwx() { return new Promise(function (reslove, reject) { wx.login({ success(res) { wx.setStorageSync("loginCode", res.code); reslove(res.code); }, }); }); }
async getPhoneNumber(e) { let code= await this.loginwx(); }
