您的位置:首页 > 娱乐 > 八卦 > 微信小程序电话号码授权

微信小程序电话号码授权

2024/10/6 12:23:52 来源:https://blog.csdn.net/qq_36022463/article/details/141262954  浏览:    关键词:微信小程序电话号码授权

前端:
文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html

uniapp调用的时候,要将bind用@替换

<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
function getPhoneNumber(e){console.log(e.detail.code)  // 动态令牌console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)console.log(e.detail.errno)  // 错误码(失败时返回)if (e.detail.code){User.getUserTelNumber(e.detail.code).then(res=>{// 授权成功setTimeout(()=>{uni.navigateBack({delta:1 //返回的页面层级,1为返回上一个页面})},1000)}).catch((err)=>{console.log(err,"errrrrrr")})}else{// 拒绝授权toast("拒绝授权","error")setTimeout(()=>{uni.navigateBack({delta:1 //返回的页面层级,1为返回上一个页面})},1000)}}

java:
文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
掉这个获取电话号码,需要先获取access_token
https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
获取token,有效期是两小时,需要缓存,

    @GetMapping("/getTelNumber")@LoginRequired@ApiOperation(value = "getTelNumber",notes = "getTelNumber")public RespBean getTelNumber(String code){userService.getWechatTelNumber(code);return RespBean.ok("授权成功");}
    /*** 获取微信的 accessToken,, 需要缓存起来,,, 两小时过期* @return*/public String getAccessToken(){String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", wechatPayConfig.getAppId(), wechatPayConfig.getAppSecret());Map map = restTemplate.getForObject(tokenUrl, Map.class);String accessToken = (String) map.get("access_token");System.out.println("accessToken = " + accessToken);return accessToken;}
    public void getWechatTelNumber(String code) {try {String accessToken= globalMap.get("access_token");if (accessToken == null){accessToken = getAccessToken();globalMap.put("access_token",accessToken);}//通过token和code来获取用户手机号String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken + "&code=" + code;Map<String, String> map = new HashMap<>();map.put("code",code);Object obj = restTemplate.postForObject(url, map, Object.class);System.out.println("obj = " + obj);Map<String,Object> resultMap = (Map<String,Object>) obj;Map<String,Object> phoneInfo = (Map<String, Object>) resultMap.get("phone_info");System.out.println("phoneInfo = " + phoneInfo);String phoneNumber = (String) phoneInfo.get("phoneNumber");// 将number更新到user表User user = SecurityContextHolder.getUser();user.setTel(phoneNumber);this.updateById(user);} catch (RestClientException e) {throw new ServiceException(e.getMessage());}}

因为授权手机号,必须要通过点击按钮,,用户授权,不能够直接调api,,必须要按钮,,可以将按钮展示为一个授权页面,,跳过去授权,,成功之后跳回

引用:https://blog.csdn.net/qq_46940224/article/details/136865901

https://blog.csdn.net/weixin_41698051/article/details/97777993

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com