欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在Spring Boot中利用WxJava SDK实现微信小程序登录的方法

最编程 2024-02-12 22:32:54
...

概述

WxJava SDK是一个比较实用的第三方微信开发 Java SDK
github地址:https://github.com/Wechat-Group/WxJava

SpringBoot项目中使用WxJava SDK中的weixin-java-miniapp

  1. pom文件中加入依赖
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-miniapp</artifactId>
        </dependency>
  1. 配置文件yml中加入配置信息
demo:
  # 开发者应该设置成自己的wx相关信息
  wx:
    app-id: wx60fac1f18be01481
    app-secret: 318ceca0f27ffeae6e6baafd3a5730bd
    mch-id: 123123
    mch-key: xxxxxx
    notify-url: http://www.example.com/wx/order/pay-notify
    # 商户证书文件路径
    # 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
    key-path: xxxxx
  1. 创建WxProperties.java
@Configuration
@ConfigurationProperties(prefix = "demo.wx")
public class WxProperties {

    private String appId;

    private String appSecret;

    private String mchId;

    private String mchKey;

    private String notifyUrl;

    private String keyPath;

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getMchKey() {
        return mchKey;
    }

    public void setMchKey(String mchKey) {
        this.mchKey = mchKey;
    }

    public String getAppId() {
        return this.appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getKeyPath() {
        return keyPath;
    }

    public void setKeyPath(String keyPath) {
        this.keyPath = keyPath;
    }
}
  1. 创建WxConfig配置类
@Configuration
public class WxConfig {
    @Autowired
    private WxProperties properties;

    @Bean
    public WxMaConfig wxMaConfig() {
        WxMaInMemoryConfig config = new WxMaInMemoryConfig();
        config.setAppid(properties.getAppId());
        config.setSecret(properties.getAppSecret());
        return config;
    }


    @Bean
    public WxMaService wxMaService(WxMaConfig maConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(maConfig);
        return service;
    }





}

5.做完上述准备后,在接口层调试一下,创建WxAuthController.java

/**
 * 鉴权服务
 */
@RestController
@RequestMapping("/wx/auth")
@Validated
public class WxAuthController {

    @Autowired
    private WxMaService wxService;

@PostMapping("login_by_weixin")
    public Object loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {
        String code = wxLoginInfo.getCode();

        UserInfo userInfo = wxLoginInfo.getUserInfo();

        if (code == null || userInfo == null) {
            return ResponseUtil.badArgument();
        }

        String sessionKey = null;
        String openId = null;
        try {
            WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);
            sessionKey = result.getSessionKey();
            openId = result.getOpenid();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (sessionKey == null || openId == null) {
            return ResponseUtil.fail();
        }

        // TODO openId 获取后的业务逻辑实现,如获取用户信息或者未注册用户创建新账号等等
        
        Map<Object, Object> result = new HashMap<Object, Object>();
        // TODO返回数据填充
        return ResponseUtil.ok(result);
    }
}

总结

OK,以上就是SpringBoot中,使用WxJava SDK 实现微信小程序登录的方法,学会了么?在微信小程序开发中用得比较多的。