spring boot整合第三方微信开发工具 weixin-java-miniapp 实现小程序微信登录

canca10个月前 (07-14)微信297

有时候项目需要用到微信登录或获取用户的手机号码,weixin-java-miniapp是一个好用的第三方工具,不用我们自己写httpcline调用。

导入jar包

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

添加一个resource.properties文件,写上小程序的appid和secret

wx.miniapp.appid: xxxxxxxxxxxx
wx.miniapp.secret: xxxxxxxxxxxxxxxxxxxxxxxxxx
wx.miniapp.msgDataFormat: JSON

添加两个配置文件
WxMaProperties.java

@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMaProperties {

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

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

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

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getMsgDataFormat() {
        return msgDataFormat;
    }

    public void setMsgDataFormat(String msgDataFormat) {
        this.msgDataFormat = msgDataFormat;
    }

WxMaConfiguration.java

@Configuration
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {

    @Bean
    public WxMaService wxMaService(WxMaProperties properties) {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppid());
        config.setSecret(properties.getSecret());
        config.setMsgDataFormat(properties.getMsgDataFormat());

        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }

}


如何使用

    @Autowired
	private WxMaService wxMaService;

    @ApiOperation("获取微信授权信息")
    @ApiImplicitParam(name = "code",value = "前端授权登录后传来的code", required = true,paramType = "query")
    @RequestMapping(value = "/wechatSession", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult wechatSession(@RequestParam String code) {
        //获取openId、unionid、session_key
        WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
    }


    @ApiOperation("小程序手机号登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionKey", value = "sessionKey", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "encryptedData", value = "加密串", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "iv", value = "偏移量", paramType = "query", dataType = "string", required = true)
    })
    @RequestMapping(value = "/wechatLogin", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult wechatLogin(HttpServletRequest request,                                     
                                      @RequestParam @NotBlank(message = "sessionKey不能为空") String sessionKey,                                    
                                      @RequestParam @NotBlank(message = "加密串不能为空") String encryptedData,
                                      @RequestParam @NotBlank(message = "偏移串不能为空") String iv) {
        WxMaPhoneNumberInfo phoneInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
        return phoneInfo.getPurePhoneNumber(); 
    }


相关文章

通过微信分享链接,后面被加上from=singlemessage&isappinstalled=1导致网页打不开

微信分享会根据分享的不同,为原始链接拼接如下参数: 朋友圈   from=timeline&isappinstalled=0 微信群   from=g...

微信公众账号文字消息加链接,用户点击链接相当于自动留言

<a href="weixin://bizmsgmenu?msgmenucontent=今天打卡有什么&msgmenuid=1">打卡</a>msg...

微信支付默认关注公众号

微信支付有四种支付方式:刷卡支付,公众号支付,扫码支付,APP支付;前三种支付方式有默认关注公众号功能,APP支付需要额外申请。微信支付成功后推荐默认自动关注的规则有以下几点:刷卡支付默认有推荐关注公...

公众号支付报错:“当前页面的URL未注册”

公众号支付报错:“当前页面的URL未注册”

问题公众号支付报错:“当前页面的URL未注册”解决方案一句话概述请检查下单接口中使用的商户号是否在商户平台配置了对应的支付目录。可能原因及详细解决方案可能导致该报错的原因以及详细的解决方案如下一、支付...

微信分享JSSDK-invalid signature签名错误的解决方案

核对官方步骤,确认签名算法。确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验。确认confi...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。