小程序端使用唤醒授权
<buttonopen-type="getPhoneNumber"bindgetphonenumber="getPhoneNumber"></button>Page({ getPhoneNumber (e) { console.log(e.detail.code) // 动态令牌console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)console.log(e.detail.errno) // 错误码(失败时返回) } })
后端获取手机号码
获取手机号码的代码如下
description="PhoneNumber") (publicstaticclassPhoneNumberimplementsSerializable { privatestaticfinallongserialVersionUID=7658930891407547798L; privatestaticfinalStringWX_GET_PHONE_NUMBER="/wxa/business/getuserphonenumber"; description="错误码") ("errcode") (privateIntegererrCode; description="错误详情") ("errmsg") (privateStringerrMsg; description="手机号码") ("phone_info") (privatePhoneInfopInfo; description="phone_info") (publicstaticclassPhoneInfoimplementsSerializable { privatestaticfinallongserialVersionUID=7658930891407547798L; description="用户绑定的手机号(国外手机号会有区号)") (privateStringphoneNumber; description="没有区号的手机号") (privateStringpurePhoneNumber; description="区号") (privateStringcountryCode; } publicstaticOptional<String>getPhoneNumber(Stringurl , StringphoneCode , StringaccessToken) { // UrlBuilder 是hutool的一个工具类StringbuildUrl=UrlBuilder.of(url+WX_GET_PHONE_NUMBER) .addQuery("access_token", accessToken).build(); log.info("BusUserDto-WxRegisterLoginUser-PhoneNumber-getPhoneNumber-buildUrl :{}" , buildUrl); Stringstr=HttpUtil.post(buildUrl, Map.of("code", phoneCode)); log.info("BusUserDto-WxRegisterLoginUser-PhoneNumber-getPhoneNumber-str :{}" , str); PhoneNumberphoneNumber=JacksonUtil.toObject(str, PhoneNumber.class); PhoneInfoinfo=phoneNumber.getPInfo(); if (ObjectUtil.isNull(info)){ returnOptional.empty(); } returnOptional.of(info.getPhoneNumber()); } }
获取accessToken
java 代码
description="AccessToken") (publicstaticclassAccessTokenimplementsSerializable { //微信access缓存keyprivatestaticfinalStringWX_ACCESS_TOKEN_CACHE="wx_access_token_cache_"; // 缓存过期时间 官方 7200sprivatestaticfinalLongCACHE_TIME_OUT=7100L; // 获取accessToken的链接后缀privatestaticStringWX_ACCESS_TOKEN_URL="/cgi-bin/token"; privatestaticfinallongserialVersionUID=7658930891407547798L; description="access_token") ("access_token") (privateStringaccessToken; description="过期时间 默认7200s") ("expires_in") (privateStringexpiresIn; description="错误码") ("errcode") (privateIntegererrCode; description="错误详情") ("errmsg") (privateStringerrMsg; publicstaticStringgetAccessToken(Stringurl , Stringappid , Stringsecret) { StringaccessToken=RedisUtils.get(WX_ACCESS_TOKEN_CACHE); if (StrUtil.isNotEmpty(accessToken)){ returnaccessToken; } StringbuildUrl=UrlBuilder.of(url+WX_ACCESS_TOKEN_URL) .addQuery("grant_type", "client_credential") .addQuery("appid", appid) .addQuery("secret", secret).build(); log.info("BusUserDto-WxRegisterLoginUser-getAccessToken-buildUrl:{}" , buildUrl); Stringstr=HttpUtil.get(buildUrl); log.info("BusUserDto-WxRegisterLoginUser-getAccessToken-str :{}" , str); AccessTokentoObject=JacksonUtil.toObject(str, AccessToken.class); // 放到redis中设置过期时间,单位为秒(s)RedisUtils.set(WX_ACCESS_TOKEN_CACHE , toObject.getAccessToken(), CACHE_TIME_OUT , TimeUnit.SECONDS); returntoObject.getAccessToken(); } }
最后吐槽一句
微信获取用户手机号码在2023-08-26的时候要开始收费了 , 如果没有去购买 , 那么不能再前端唤醒授权按钮 , 但是不会提示欠费信息。看吧自己也知道这个事情不能告诉微信用户............