|
|
|
@ -5,18 +5,22 @@ import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; |
|
|
|
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; |
|
|
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; |
|
|
|
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler; |
|
|
|
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.google.common.collect.Maps; |
|
|
|
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import java.io.File; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author <a href="https://github.com/binarywang">Binary Wang</a> |
|
|
|
@ -27,31 +31,51 @@ public class WxMaConfig { |
|
|
|
|
|
|
|
private WxMaProperties properties; |
|
|
|
|
|
|
|
private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap(); |
|
|
|
private static Map<String, WxMaService> maServices = Maps.newHashMap(); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
public WxMaConfig(WxMaProperties properties) { |
|
|
|
this.properties = properties; |
|
|
|
} |
|
|
|
|
|
|
|
@Bean |
|
|
|
public WxMaService wxMaService() { |
|
|
|
WxMaProperties wxMaProperties = this.properties; |
|
|
|
if (wxMaProperties == null) { |
|
|
|
public static WxMaService getMaService(String appid) { |
|
|
|
WxMaService wxService = maServices.get(appid); |
|
|
|
if (wxService == null) { |
|
|
|
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid)); |
|
|
|
} |
|
|
|
|
|
|
|
return wxService; |
|
|
|
} |
|
|
|
|
|
|
|
public static WxMaMessageRouter getRouter(String appid) { |
|
|
|
return routers.get(appid); |
|
|
|
} |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
List<WxMaProperties.Config> configs = this.properties.getConfigs(); |
|
|
|
if (configs == null) { |
|
|
|
throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!"); |
|
|
|
} |
|
|
|
WxMaInMemoryConfig config = new WxMaInMemoryConfig(); |
|
|
|
config.setAppid(wxMaProperties.getAppid()); |
|
|
|
config.setSecret(wxMaProperties.getSecret()); |
|
|
|
config.setToken(wxMaProperties.getToken()); |
|
|
|
config.setAesKey(wxMaProperties.getAesKey()); |
|
|
|
config.setMsgDataFormat(wxMaProperties.getMsgDataFormat()); |
|
|
|
|
|
|
|
WxMaService maService = new WxMaServiceImpl(); |
|
|
|
maService.setWxMaConfig(config); |
|
|
|
return maService; |
|
|
|
|
|
|
|
maServices = configs.stream() |
|
|
|
.map(a -> { |
|
|
|
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); |
|
|
|
config.setAppid(a.getAppid()); |
|
|
|
config.setSecret(a.getSecret()); |
|
|
|
config.setToken(a.getToken()); |
|
|
|
config.setAesKey(a.getAesKey()); |
|
|
|
config.setMsgDataFormat(a.getMsgDataFormat()); |
|
|
|
|
|
|
|
WxMaService service = new WxMaServiceImpl(); |
|
|
|
service.setWxMaConfig(config); |
|
|
|
routers.put(a.getAppid(), this.newRouter(service)); |
|
|
|
return service; |
|
|
|
}).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); |
|
|
|
} |
|
|
|
|
|
|
|
@Bean |
|
|
|
public WxMaMessageRouter newRouter(WxMaService service) { |
|
|
|
private WxMaMessageRouter newRouter(WxMaService service) { |
|
|
|
final WxMaMessageRouter router = new WxMaMessageRouter(service); |
|
|
|
router |
|
|
|
.rule().handler(logHandler).next() |
|
|
|
@ -62,24 +86,29 @@ public class WxMaConfig { |
|
|
|
return router; |
|
|
|
} |
|
|
|
|
|
|
|
private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> |
|
|
|
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() |
|
|
|
.templateId("此处更换为自己的模板id") |
|
|
|
.formId("自己替换可用的formid") |
|
|
|
.data(Lists.newArrayList( |
|
|
|
new WxMaTemplateData("keyword1", "339208499", "#173177"))) |
|
|
|
.toUser(wxMessage.getFromUser()) |
|
|
|
.build()); |
|
|
|
private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> { |
|
|
|
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() |
|
|
|
.templateId("此处更换为自己的模板id") |
|
|
|
.formId("自己替换可用的formid") |
|
|
|
.data(Lists.newArrayList( |
|
|
|
new WxMaTemplateData("keyword1", "339208499", "#173177"))) |
|
|
|
.toUser(wxMessage.getFromUser()) |
|
|
|
.build()); |
|
|
|
return null; |
|
|
|
}; |
|
|
|
|
|
|
|
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> { |
|
|
|
System.out.println("收到消息:" + wxMessage.toString()); |
|
|
|
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson()) |
|
|
|
.toUser(wxMessage.getFromUser()).build()); |
|
|
|
return null; |
|
|
|
}; |
|
|
|
|
|
|
|
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> |
|
|
|
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息") |
|
|
|
.toUser(wxMessage.getFromUser()).build()); |
|
|
|
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> { |
|
|
|
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息") |
|
|
|
.toUser(wxMessage.getFromUser()).build()); |
|
|
|
return null; |
|
|
|
}; |
|
|
|
|
|
|
|
private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> { |
|
|
|
try { |
|
|
|
@ -95,6 +124,8 @@ public class WxMaConfig { |
|
|
|
} catch (WxErrorException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
}; |
|
|
|
|
|
|
|
private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> { |
|
|
|
@ -110,6 +141,8 @@ public class WxMaConfig { |
|
|
|
} catch (WxErrorException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|