使用weixin-java-miniapp配置进行单个小程序的配置,需要遵循下面的步骤:
1. 引入依赖
在pom.xml文件中引入以下依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.9.0</version>
</dependency>
如果需要使用CDN(Content Delivery Network,内容分发网络)服务,可以引入以下依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp-cdn</artifactId>
<version>3.9.0</version>
</dependency>
2. 配置属性
在application.properties文件中添加以下属性:
# 小程序AppID
wx.miniapp.appid=your-appid
# 小程序AppSecret
wx.miniapp.secret=your-secret
# 小程序token
wx.miniapp.token=your-token
# 小程序EncodingAESKey
wx.miniapp.aesKey=your-aesKey
需要将上述属性替换成自己小程序的AppID、AppSecret、token及EncodingAESKey。
3. 创建配置类
创建以下配置类:
@Configuration
public class WxMpConfiguration {
private static final Logger log = LoggerFactory.getLogger(WxMpConfiguration.class);
@Autowired
private WxMpProperties wxMpProperties;
private static Map<String, WxMpService> mpServices = new HashMap<>();
/**
* 初始化微信公众号服务
*
* @return {@link WxMpService}
*/
public WxMpService initMpService() {
WxMpService mpService = new WxMpServiceImpl();
WxMpConfigStorage configStorage = new WxMpInMemoryConfigStorage();
configStorage.setAppId(wxMpProperties.getAppid());
configStorage.setSecret(wxMpProperties.getSecret());
configStorage.setToken(wxMpProperties.getToken());
configStorage.setAesKey(wxMpProperties.getAesKey());
mpService.setWxMpConfigStorage(configStorage);
mpService.setKefuService(wxMpKefuService());
mpServices.put(configStorage.getAppId(), mpService);
log.info("Init WxMpService for appid: {}", configStorage.getAppId());
return mpService;
}
/**
* 获取微信公众号服务
*
* @return {@link WxMpService}
*/
public WxMpService getMpService() {
String appId = wxMpProperties.getAppid();
WxMpService mpService = mpServices.get(appId);
if (mpService == null) {
mpService = initMpService();
}
return mpService;
}
/**
* 获取微信公众号客服服务
*
* @return {@link WxMpKefuService}
*/
@Bean
public WxMpKefuService wxMpKefuService() {
return new WxMpKefuServiceImpl(getMpService());
}
}
其中,WxMpServiceImpl、WxMpInMemoryConfigStorage和WxMpKefuServiceImpl分别为weixin-java-tools中提供的微信公众号服务的实现类和配置存储类。
4. 使用配置类
在需要使用微信公众号服务的类中,可以直接注入WxMpService类实例或使用WxMpConfiguration.getMpService()方法获取实例。示例代码如下:
@Service
public class WxServiceImpl implements WxService {
@Autowired
private WxMpKefuService wxMpKefuService;
@Override
public void sendTextMessage(String openid, String content) {
WxMpKefuMessage message = WxMpKefuMessage
.TEXT()
.toUser(openid)
.content(content)
.build();
try {
wxMpKefuService.sendKefuMessage(message);
} catch (WxErrorException e) {
throw new RuntimeException(e.getMessage());
}
}
}
示例说明
假设有一个小程序,名称为test-mini-program,AppID为ABC,AppSecret为123。
- 引入依赖
在pom.xml文件中添加以下依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.9.0</version>
</dependency>
- 配置属性
在application.properties文件中设置以下属性:
wx.miniapp.appid=ABC
wx.miniapp.secret=123
wx.miniapp.token=your-token
wx.miniapp.aesKey=your-aesKey
- 创建配置类
创建以下配置类:
@Configuration
public class WxMiniProgramConfiguration {
private static final Logger log = LoggerFactory.getLogger(WxMiniProgramConfiguration.class);
@Autowired
private WxMiniProgramProperties wxMiniProgramProperties;
private static Map<String, WxMaService> maServices = new HashMap<>();
/**
* 初始化小程序服务
*
* @return {@link WxMaService}
*/
public WxMaService initMaService() {
WxMaService maService = new WxMaServiceImpl();
WxMaInMemoryConfig config = new WxMaInMemoryConfig();
config.setAppid(wxMiniProgramProperties.getAppid());
config.setSecret(wxMiniProgramProperties.getSecret());
config.setToken(wxMiniProgramProperties.getToken());
config.setAesKey(wxMiniProgramProperties.getAesKey());
maService.setWxMaConfig(config);
maServices.put(config.getAppid(), maService);
log.info("Init WxMaService for appid: {}", config.getAppid());
return maService;
}
/**
* 获取小程序服务
*
* @return {@link WxMaService}
*/
public WxMaService getMaService() {
String appId = wxMiniProgramProperties.getAppid();
WxMaService maService = maServices.get(appId);
if (maService == null) {
maService = initMaService();
}
return maService;
}
}
其中,WxMaServiceImpl和WxMaInMemoryConfig分别为weixin-java-minapp中提供的小程序服务的实现类和配置存储类。
- 使用配置类
在需要使用小程序服务的类中,可以直接注入WxMaService类实例或使用WxMiniProgramConfiguration.getMaService()方法获取实例。示例代码如下:
@Service
public class WxMaServiceImpl implements WxMaService {
@Autowired
private WxMaConfiguration wxMaConfiguration;
@Override
public void sendSubscribeMessage(String openid, WxMaTemplateMessage templateMessage) {
WxMaService wxMaService = wxMaConfiguration.getMaService();
try {
wxMaService.getSubscribeMsgService().sendSubscribeMsg(openid, templateMessage);
} catch (WxErrorException e) {
throw new RuntimeException(e.getMessage());
}
}
}
以上代码演示了使用WxMaService发送订阅消息的方式。需要注意的是,在使用订阅消息之前,需要事先在小程序后台设置好相关的订阅消息模板。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用weixin-java-miniapp配置进行单个小程序的配置详解 - Python技术站