1.情况描述
H5页面。开发微信JSAPI支付时,在支付时需要传递openid,经研究通过静默方式(scope=snsapi_base&)获取openid,获取openid失败。
2.前端代码
getCode(){
console.log('getcode方法进入', location.href)
// 微信网页授权
if(common.getUrlSearch('code')){
let code = common.getUrlSearch('code')
let data = {
logTraceID:new Date().getTime(),
code,
appid:'授权公众号appid',
}
getOpenid(data).then(res=>{
console.log('获取penid的接口', res)
})
}
if(this.from.indexOf('iswx')>-1 && this.from.indexOf('iswxmini')==-1 && this.haswx){
/** 在微信中,且支付方式含有微信支付 */
let appid = '授权公众号appid'
debugger
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(location.href)}&response_type=code&state=123&connect_redirect=1#wechat_redirect`
}
},
3.后端的结果
获取openid失败:40029, invalid code
参考一下https://developers.weixin.qq.com/community/develop/doc/00062e41a4c8d077c89b90e1053400?highLine=H5%25E8%258E%25B7%25E5%258F%2596%25E5%25BE%25AE%25E4%25BF%25A1openid%25E5%25BC%2582%25E5%25B8%25B8%25EF%25BC%259A40029%25EF%25BC%259F