微信小程序授权手机号码登录


前言

因公司项目需要做微信小程序相关项目,故记录一下相关开发要点。
使用的是binarywang工具包,版本为4.1.0。
后端框架使用springboot
更多其他功能使用推荐查看https://github.com/binarywang/binarywang


一、需求描述:授权获取手机号码登录

用户授权手机号登录小程序。

二、具体操作

1.引入相关依赖

<dependency>
   	<groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>4.1.0</version>
</dependency>

2.步骤

   通过code获取openid, sessionKey
   获取手机号需要encryptedData(加密用户数据),iv(加密算法的初始向量),sessionKey(会话密钥)

3.相关代码

3.1 微信小程序开发的相关配置

在application.yml文件中配置

test: 
  wechat: 
    appid: 小程序的appid
    appSecret: 小程序的appSecret
    msgDataFormat: JSON
    mchId: 商户号
    mchKey:商户密钥
    certPath: 证书路径
    notify-url: 支付回调接口路径

3.2创建配置文件

代码如下(示例):

@Data
@ConfigurationProperties(prefix = "test.wechat")
public class WeChatProperties {

    /**
     * 微信小程序appid
     */
    private String appId;
	/**
     * 微信小程序appSecret
     */
    private String appSecret;
    
    private String  msgDataFormat;

    /**
     * 微信支付商户号
     */
    private String mchId;

    /**
     * 微信支付商户密钥
     */
    private String mchKey;

    /**
     * apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:	开头指定
     */
    private String certPath;

}

备注:其中商户号、商户密钥、证书都是后续微信小程序支付需要使用到。


3.3 实例化WxMaService

@Configuration
@EnableConfigurationProperties(WeChatProperties.class)
public class WeChatMaConfig {

    @Autowired
    private WeChatProperties properties;

    @Bean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
      payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
        payConfig.setKeyPath(StrUtil.format("classpath:{}", StrUtil.trim(properties.getCertPath())));
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean
    public WxMaService initWxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(StrUtil.trim(properties.getAppId()));
        config.setSecret(StrUtil.trim(properties.getAppSecret()));
        config.setMsgDataFormat(StrUtil.trim(properties.getMsgDataFormat()));
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }

}

3.4 手机号码授权登录

关键代码如下(示例):

@RestController
@Slf4j
public class TestContoller {
    @Autowired
    private WxMaService          wxMaService;
    
    @GetMapping("/login")
    public void login(String code, String encryptedData, String iv) {
        
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
            String openid = sessionInfo.getOpenid();
            log.info("openid:" + openid);
            String sessionKey = sessionInfo.getSessionKey();
            WxMaPhoneNumberInfo phoneInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
            String phone = phoneInfo.getPhoneNumber();
            log.info("phone:" + phone);
            // 自己业务逻辑处理
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值