1.寫微信支付遇到狀況,通過wx.login獲取code,然后向微信服務器獲取openid,獲取失敗:{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: pYM6ma0957th24 ]"}
2.錯誤信息提示無效的code,網上查一下說是二次使用code導致失效..但是我這代碼明顯就是拿到code后直接請求獲取openid
wx.login({
success: function (res) {
console.log("code="+res.code)
//獲取openid
wx.request({
url: "https://api.weixin.qq.com/sns/jscode2session?appid=wx21c06d123456789f&secret=94a111111a61b2222fcf333e76e4b6d1&js_code=" + res.code + "&grant_type=authorization_code",
data: {},
method: 'GET',
success: function (res) {
console.log('返回openId:'+res.data.openid)
console.log(res.data)
that.generateOrder(res.data.openid)
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
}
});
3.解決問題:通過查看項目的appid,發現appid跟自己的appid不一樣(建立項目時候使用的是公司給的appid...),重新建立自己的appid項目。
獲取openid正確的方法應該是:項目appid,請求appid,以及請求密鑰是配套的..

