大部分手机微信授权登录的报错,错误码:40029,微信版本:8.0.16,成功日志和错误日志所传的参数appid和secret都是一样的
错误日志(隐去敏感信息):
2021-12-20 18:10:33 [6b8303082e4194b9923e9408f6fb2d11,923e9408f6fb2d11] [http-nio-9048-exec-6] INFO com.xinhao.task.util.HttpsUtils-get:53 - url=https://api.weixin.qq.com/sns/jscode2session?appid=wx6123fe64a157XXXX&secret=4b43a941458578271a75f4683586XXXX&js_code=56685c28f6b79efaabce2e2379c60528a263faa3968fb3fd74ddd1526ce61074&grant_type=authorization_code
2021-12-20 18:10:33 [6b8303082e4194b9923e9408f6fb2d11,923e9408f6fb2d11] [http-nio-9048-exec-6] INFO c.x.t.s.wxConfig.impl.WechatConfigServiceImpl-getUserWxInfo:47 - 登录请求微信返回:{"errcode":40029,"errmsg":"invalid code, rid: 61c05699-79d94140-4685b391"}
2021-12-20 18:10:33 [6b8303082e4194b9923e9408f6fb2d11,923e9408f6fb2d11] [http-nio-9048-exec-6] INFO c.x.task.service.login.impl.AppLoginServiceImpl-doLoginByWeChat:501 - encryptedData:2wnIV4VMF4SIXujKGCo2zJpqYDYKfVUyg1+qo9EpCkALnBT0NSFNrJzHlQYBr6xcSvGVl87fxoOHLeqENu+9DwLtMriic7KEq4+8RqqgP2nZkftV94e3JFwJnM1H5SIHq4LRux9QuBtMqXHBKon1FV3FeNQdIvfzOyolOQXB3kmAc24Qmgoo02mCnieMQohWf6VxubEBZGd4+XpN6+bgcA==, session_key:null, iv:bHlel7dzs1AjOEnFYZ6NyQ==
2021-12-20 18:10:33 [6b8303082e4194b9923e9408f6fb2d11,923e9408f6fb2d11] [http-nio-9048-exec-6] ERROR c.x.task.service.login.impl.AppLoginServiceImpl-doLoginByWeChat:516 - purePhoneNumber 获取异常:
2021-12-20 18:10:33 [6b8303082e4194b9923e9408f6fb2d11,923e9408f6fb2d11] [http-nio-9048-exec-6] ERROR com.xinhao.task.exception.BasicRuntimeException-:21 - 获取手机号码失败:Missing argument
2021-12-20 18:10:33 [6b8303082e4194b9923e9408f6fb2d11,923e9408f6fb2d11] [http-nio-9048-exec-6] ERROR com.xinhao.task.controller.app.AppLoginController-doLoginByWeChat:265 - /app/doLoginByWeChat 微信授权登录异常com.xinhao.task.exception.BasicRuntimeException: 获取手机号码失败:Missing argument
成功日志(隐去敏感信息):
2021-12-20 18:18:26 [5b204d8ec4e8201405e6419ca70617b8,05e6419ca70617b8] [http-nio-9048-exec-2] INFO com.xinhao.task.util.HttpsUtils-get:53 - url=https://api.weixin.qq.com/sns/jscode2session?appid=wx6123fe64a157XXXX&secret=4b43a941458578271a75f4683586XXXX&js_code=031p4cHa1ZrDlC0ny0Ga1mA5QF2p4cHL&grant_type=authorization_code
2021-12-20 18:18:26 [5b204d8ec4e8201405e6419ca70617b8,05e6419ca70617b8] [http-nio-9048-exec-2] INFO c.x.t.s.wxConfig.impl.WechatConfigServiceImpl-getUserWxInfo:47 - 登录请求微信返回:{"session_key":"YK3QaW3tECuWXM7SnKcDYg==","openid":"oZCV95CIITrsLX9kZW8uu1OVXXXX"}
前端关键代码:
const code = await getWxCode()
const res = await requestw({
url: '/app/doLoginByWeChat',
data: {
code,
companyTag: isSafe ? 'FWB' : 'YPG',
...params,
},
})
console.log('登录结果', res)
if (!res || res.code !== 200) {
try {
uni.hideLoading()
} catch (e) {}
uni.showToast({
title: (res && res.message) || '网络异常',
icon: 'none'
})
resolve(false)
return
}
//成功 拿到token
const token = res.data.TOKEN
const phone = res.data.PHONE_NUMBER
resolve({
token,
phone,
})
})
}
export const getWxCode = () => {
return new Promise((resolve) => {
wx.login({
success: (res) => {
console.log("临时凭证", res)
resolve(res.code)
}
})
})
}
后端关键代码:
public static final String WX_USER_SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";//微信登录获取个人信息url
String url = WX_USER_SESSION_URL.replaceAll("APPID", appId).replaceAll("SECRET", appSecret).replaceAll("JSCODE", code);
String response = HttpsUtils.get(url);
log.info("登录请求微信返回:{}", response);
if (response == null) {
return null;
}
JSONObject object = JSON.parseObject(response);
String openId = object.getString("openid");
String sessionKey = object.getString("session_key");
String unionid = object.getString("unionid");
JSONObject jsonObject = new JSONObject();
jsonObject.put("openId", openId);
jsonObject.put("sessionKey", sessionKey);
jsonObject.put("unionid", unionid);
我的是这个接口可以获取 ,获取手机号报40029 😭
<!-- 操作 -->
<view class="btn_wrap">
<block v-if="tabIndex == 0">
<button
style="width:601rpx;border-radius:44rpx;"
class="btn btn2"
hover-class="btn2_active"
hover-stay-time="110"
open-type="getPhoneNumber"
@getphonenumber="onGetPhoneNumber"
>
<image src="https://file.xhlhyg.com/wx_icon.png" style="width:34rpx;height:28rpx;margin-right:9rpx;"></image>
微信一键授权
</button>
</block>
<block v-if="tabIndex == 1">
<view class="btn btn1" style="margin-right:24rpx;" hover-class="btn1_active" hover-stay-time="110" @tap.stop="close">取消</view>
<view class="btn btn2" hover-class="btn2_active" hover-stay-time="110" @tap.stop="loginSubmit">登录</view>
</block>
</view>