美文网首页
weixin-java-miniapp 基本用法

weixin-java-miniapp 基本用法

作者: _小生不才_ | 来源:发表于2021-03-10 15:20 被阅读0次

    WxJava - 微信开发 Java SDK

    maven 依赖

    <dependency>

    <groupId>com.github.binarywang</groupId>

    <artifactId>weixin-java-miniapp</artifactId>

    <version>4.0.0</version>

    </dependency>

    application.yml配置

    wechat:

    miniapp:

        appId: wx701*********

        appSecret: a374ae************************

    WxMaConfiguration

    /**

    * 微信小程序

    */

    @Data

    @Configuration

    @ConditionalOnClass(WxMaService.class)

    @ConfigurationProperties(prefix ="wechat.miniapp")

    public class WxMaConfiguration {

    /**

    * 设置微信小程序的appId

    */

        private StringappId;

    /**

    * 设置微信小程序的Secret

    */

        private StringappSecret;

    @Bean

    @ConditionalOnMissingBean(WxMaService.class)

    public WxMaService wxMaService() {

    WxMaDefaultConfigImpl config =new WxMaDefaultConfigImpl();

    config.setAppid(this.getAppId());

    config.setSecret(this.getAppSecret());

    WxMaService service =new WxMaServiceImpl();

    service.setWxMaConfig(config);

    return service;

    }

    }

    使用

    public String login(String code, String signature, String rawData, String encryptedData, String iv)throws WxErrorException {

    WxMaJscode2SessionResult res =wxMaService.getUserService().getSessionInfo(code);

    String sessionKey = res.getSessionKey();

    // 用户信息校验

        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {

    return R.error("微信授权失败");

    }

    // 解密用户信息

        WxMaUserInfo userInfo =wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);

    String openId=userInfo.getOpenId();

    return openId;

    }

    相关文章

      网友评论

          本文标题:weixin-java-miniapp 基本用法

          本文链接:https://www.haomeiwen.com/subject/axpwqltx.html