使用weixin-java-miniapp配置进行单个小程序的配置

在进行小程序后端接口开发方面,使用weixin-java-tools中的weixin-java-miniapp模块,每每能够事半功倍。

引入weixin-java-tools

  • https://mvnrepository.com/中搜索weixin-java-miniapp,进入微信小程序 Java SDK这个项目中。
  • 选择相应正式版原本进行使用。
  • maven中在依赖中添加以下配置项:
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>3.3.0</version>
</dependency>
  • gradle中添加以下配置项:
compile("com.github.binarywang:weixin-java-miniapp:3.3.0")
  • 注意:以上我用的版本是3.3.0,实际中根据你要使用的版原本用。

配置文件

  • 配置文件中主要配置四项参数,分别是:java

    • appId
    • secret
    • token
    • aesKey

配置初始化:

  • weixin-java-miniapp能够使用注解来进行配置,具体步骤以下:
  1. 在config包中建立WxMaConfiguration类。
  2. 使用@Configuration注解来进行小程序相关的参数配置,可参考如下代码。
  3. 该代码示例中是单个小程序配置示例,若是须要配置多个小程序的参数,请参考官方案例点击进入
package com.diboot.miniapp.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import dibo.framework.config.BaseConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxMaConfiguration {

    // 此处获取配置的方式能够改为你本身的方式,也能够注解等方式获取配置等。
    private static final String appId = BaseConfig.getProperty("wechat.appId");
    private static final String secret = BaseConfig.getProperty("wechat.secret");
    private static final String token = BaseConfig.getProperty("wechat.token");
    private static final String aesKey = BaseConfig.getProperty("wechat.aesKey");

    private static WxMaService wxMaService = null;

    @Bean
    public Object services(){
        WxMaInMemoryConfig config = new WxMaInMemoryConfig();
        config.setAppid(appId);
        config.setSecret(secret);
        config.setToken(token);
        config.setAesKey(aesKey);

        wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);

        return Boolean.TRUE;
    }

    public static WxMaService getWxMaService(){
        return wxMaService;
    }
}

开始使用

  • 在须要使用小程序相关接口的地方,只须要经过该配置类中的静态方法getWxMaService()来获取到wxMaService便可开始使用,如:
// 获取小程序服务实例
WxMaService wxMaService = WxMaConfiguration.getWxMaService();
// 获取小程序二维码生成实例
WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();
// 即可以开始使用wxMaQrcodeService来进行二维码相关的处理了
....

Diboot - 简单高效的轻代码开发框架git

相关文章
相关标签/搜索