微信小程序电话号码授权

前端:

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

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

html 复制代码
<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
js 复制代码
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,有效期是两小时,需要缓存,

java 复制代码
    @GetMapping("/getTelNumber")
    @LoginRequired
    @ApiOperation(value = "getTelNumber",notes = "getTelNumber")
    public RespBean getTelNumber(String code){

        userService.getWechatTelNumber(code);

        return RespBean.ok("授权成功");

    }
java 复制代码
    /**
     * 获取微信的 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;
    }
java 复制代码
    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

相关推荐
风等雨归期36 分钟前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
李宥小哥2 小时前
微信小程序07-开发进阶
微信小程序·小程序·notepad++
艾小逗3 小时前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
程序员阿龙3 小时前
【2025】儿童疫苗接种预约小程序(源码+文档+解答)
小程序·毕业设计·小程序开发·预约小程序·疫苗管理小程序·出勤数据分析·智能考勤
818源码资源站3 小时前
表情包创作、取图小程序端(带流量主)
小程序
2401_8459375313 小时前
PHP一键约课高效健身智能健身管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
程序员入门进阶15 小时前
基于微信小程序的科创微应用平台设计与实现+ssm(lw+演示+源码+运行)
微信小程序·小程序
计算机源码社1 天前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
DreamByte1 天前
Python Tkinter小程序
开发语言·python·小程序
说私域1 天前
开源 AI 智能名片小程序:开启内容营销新境界
人工智能·小程序