微信小程序授权获取微信绑定手机号码-造轮子

952 阅读3分钟

项目简介

  • 需求:点开微校小程序需要手机号码发送验证码登录
  • 方案一:用户自己手动输入手机号码获取
  • 方案二:用户点进微信小程序,用户点击授权按钮,获取用户手机号码。
  • 两个方案都需要实现,本项目复盘只复盘对接微信小程序的授权获取手机号码

项目背景

  • 需支持多个微信小程序接入

微信小程序API权限说明

  • 获取微信用户绑定的手机号,需先调用wx.login接口。
  • 因为需要用户主动触发才能发起获取手机号接口,所以该功能不由API来调用,需用button组件的点击来触发。
  • 注意:目前该接口针对非个人开发者,且完成了认证的小程序开放(不包含海外主体)。需谨慎使用,若用户举报较多或被发现在不必要场景下使用,微信有权永久回收该小程序的该接口权限。

引入微信小程序工具类

  • 源码地址
  • 引入版本:最新的4.0本文项目基于版本3.4开发的
<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>(不同模块参考下文)</artifactId>
  <version>4.0.0</version>
</dependency>
  • 引入微信小程序配置类
@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMiniProperties {

    private List<Config> configs;

    @Data
    public static class Config {
        /**
         * 设置微信小程序的appid
         */
        private String appid;

        /**
         * 设置微信小程序的Secret
         */
        private String secret;

        /**
         * 设置微信小程序消息服务器配置的token
         */
        private String token;

        /**
         * 设置微信小程序消息服务器配置的EncodingAESKey
         */
        private String aesKey;

        /**
         * 消息格式,XML或者JSON
         */
        private String msgDataFormat;
    }

}
  • 将多个配置的微信公众号信息放入本地缓存只放新增和删除的方法
/**
     * @param
     * @return java.lang.Boolean
     * @auther 
     * @desc 将微信小程序信息从数据库读取放入缓存
     * @date 
     */
    public static Boolean addConfig(String appid, String secret, String token, String aesKey) {
        WxMaInMemoryConfig configStorage = new WxMaInMemoryConfig();
        configStorage.setAppid(appid);
        configStorage.setSecret(secret);
        configStorage.setToken(token);
        configStorage.setAesKey(aesKey);
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(configStorage);
        routers.put(appid, newRouter(service));
        maServices.put(service.getWxMaConfig().getAppid(), service);
        //WxMaConfiguration.getMaService(appid).getAccessToken();
        return true;
    }

    /**
     * @param
     * @return java.lang.Boolean
     * @auther 
     * @desc 从缓存删除小程序信息
     * @date 
     */
    public static Boolean deleteConfig(String appid) {
        routers.remove(appid);
        maServices.remove(appid);
        return true;
    }
  • 授权获取手机号码后端代码:返回对象的phoneNumber就是用户手机号码
    @GetMapping("/info")
    public String info(@PathVariable String appid, String sessionKey,
                       String signature, String rawData, String encryptedData, String iv) {
        final WxMaService wxService = WxMiniConfiguration.getMaService(appid);

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }

        // 解密用户信息
        WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);

        return JsonUtils.toJson(userInfo);
    }

  • 请求参数说明:为wx.login登录返回的参数,登录完可能会刷新登录态。此时服务器使用code换取的sessionKey不是加密时使用的sessionKey,导致解密失败。建议开发者提前进行login;或者在回调中先使用checkSession进行登录态检查,避免login刷新登录态。

总结思考

  • 微信工具类已经更新到4.0版本,本项目需更新版本升级。
  • 此接口需按微信小程序规范使用,一旦使用过度像微信服务号的模板消息滥用一样,会导致接口回收,于整个微信生态开发不利。
  • 微信小程序的开发务必遵守微信小程序的规范和约定。

备注

本文正在参与「掘金 2021 春招闯关活动」, 点击查看活动详情