|
@@ -0,0 +1,163 @@
|
|
|
+package vip.xiaonuo.weixin.gongzhong.config;
|
|
|
+
|
|
|
+import cn.hutool.core.net.URLEncodeUtil;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import me.chanjar.weixin.common.api.WxConsts;
|
|
|
+import me.chanjar.weixin.common.bean.menu.WxMenu;
|
|
|
+import me.chanjar.weixin.common.bean.menu.WxMenuButton;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.common.session.WxSessionManager;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpMessageHandler;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpMessageRouter;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
|
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
|
+import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
|
|
+import me.chanjar.weixin.mp.config.impl.WxMpMapConfigImpl;
|
|
|
+import me.chanjar.weixin.mp.enums.WxMpApiUrl;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import vip.xiaonuo.coldchain.modular.alarmuser.service.AlarmUserService;
|
|
|
+import vip.xiaonuo.coldchain.modular.push.config.PushConfigure;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * WxJava 公众号配置
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Configuration
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class WxMpConfiguration {
|
|
|
+
|
|
|
+ private final PushConfigure properties;
|
|
|
+
|
|
|
+ private final AlarmUserService alarmUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 授权页面地址
|
|
|
+ */
|
|
|
+ private static final String REDIRECT_URL = URLEncodeUtil.encodePathSegment("https://coldchain.nzkcloud.com/coldchain/wx/mp/auth/page");
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Bean
|
|
|
+ public WxMpService wxMpService() {
|
|
|
+ WxMpDefaultConfigImpl configStorage = new WxMpMapConfigImpl();
|
|
|
+ configStorage.setAppId(properties.getAppId());
|
|
|
+ configStorage.setSecret(properties.getSecret());
|
|
|
+ configStorage.setTemplateId(properties.getTemplateId());
|
|
|
+
|
|
|
+ WxMpService service = new WxMpServiceImpl();
|
|
|
+ service.setWxMpConfigStorage(configStorage);
|
|
|
+ //创建菜单
|
|
|
+ service.getMenuService().menuCreate(createMenu());
|
|
|
+ return service;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建公众号菜单
|
|
|
+ */
|
|
|
+ private WxMenu createMenu() {
|
|
|
+ //小程序
|
|
|
+ WxMenuButton button1 = new WxMenuButton();
|
|
|
+ button1.setType("miniprogram");
|
|
|
+ button1.setName("冷链中心");
|
|
|
+ button1.setAppId(properties.getMiniProgram());
|
|
|
+ button1.setUrl("http://mp.weixin.qq.com");
|
|
|
+ button1.setPagePath("pages/home/home");
|
|
|
+
|
|
|
+ WxMenuButton button2 = new WxMenuButton();
|
|
|
+ button2.setType("click");
|
|
|
+ button2.setName("关于我们");
|
|
|
+ button2.setKey("aboutUs");
|
|
|
+
|
|
|
+ WxMenuButton button3 = new WxMenuButton();
|
|
|
+ button3.setType("view");
|
|
|
+ button3.setName("授权用户");
|
|
|
+ button3.setUrl(getAuthUrl());
|
|
|
+
|
|
|
+ WxMenu wxMenu = new WxMenu();
|
|
|
+ wxMenu.getButtons().add(button1);
|
|
|
+ wxMenu.getButtons().add(button2);
|
|
|
+ wxMenu.getButtons().add(button3);
|
|
|
+
|
|
|
+ return wxMenu;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取授权地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getAuthUrl() {
|
|
|
+ return String.format(WxMpApiUrl.OAuth2.CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(null),
|
|
|
+ properties.getAppId(), REDIRECT_URL, "snsapi_userinfo", "STATE");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public WxMpMessageRouter wxMpMessageRouter() {
|
|
|
+ WxMpMessageRouter router = new WxMpMessageRouter(wxMpService());
|
|
|
+
|
|
|
+ // 关注事件
|
|
|
+ router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE)
|
|
|
+ .handler(new WxMpMessageHandler() {
|
|
|
+ @Override
|
|
|
+ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
|
|
|
+ log.info("用户关注 {}", wxMessage);
|
|
|
+
|
|
|
+ return WxMpXmlOutMessage.TEXT()
|
|
|
+ .content("【冷链驿站】为您提供温湿度、二氧化碳等环境监控数据查询及实时报警通知服务。请先<a href=\"" + getAuthUrl() + "\">完善信息</a>")
|
|
|
+ .fromUser(wxMessage.getToUser())
|
|
|
+ .toUser(wxMessage.getFromUser())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .end();
|
|
|
+
|
|
|
+ // 取消关注事件
|
|
|
+ router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE)
|
|
|
+ .handler(new WxMpMessageHandler() {
|
|
|
+ @Override
|
|
|
+ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
|
|
|
+ log.info("用户取消关注 {}", wxMessage);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .end();
|
|
|
+
|
|
|
+ // 点击菜单事件
|
|
|
+ router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.CLICK)
|
|
|
+ .handler(new WxMpMessageHandler() {
|
|
|
+ @Override
|
|
|
+ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
|
|
|
+ log.info("用户点击 {}", wxMessage);
|
|
|
+
|
|
|
+ if (wxMessage.getEventKey().equals("aboutUs")) {
|
|
|
+ return WxMpXmlOutMessage.TEXT()
|
|
|
+ .content("【冷链驿站】为您提供温湿度、二氧化碳等环境监控数据查询及实时报警通知服务。您可以随时查看设备数据、设置报警阈值,确保冷链环境的安全与稳定。如有任何问题或需要帮助,请随时留言,我们将尽快为您服务。感谢您的支持!")
|
|
|
+ .fromUser(wxMessage.getToUser())
|
|
|
+ .toUser(wxMessage.getFromUser())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .end();
|
|
|
+
|
|
|
+ // 点击view事件
|
|
|
+ router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.VIEW)
|
|
|
+ .handler(new WxMpMessageHandler() {
|
|
|
+ @Override
|
|
|
+ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
|
|
|
+ log.info("用户打开view {}", wxMessage);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .end();
|
|
|
+
|
|
|
+ return router;
|
|
|
+ }
|
|
|
+}
|