diff --git a/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java b/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java index cd646bb..19143e6 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java +++ b/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java @@ -14,4 +14,14 @@ public class OfficialAccountParamConstant { public static final String RAW = "raw"; public static final String AES = "aes"; public static final String MSG_SIGNATURE = "msg_signature"; + + + /** + * 处理器名 + */ + public static final String SCAN_HANDLER = "scanHandler"; + public static final String SUBSCRIBE_HANDLER = "subscribeHandler"; + public static final String UNSUBSCRIBE_HANDLER = "unSubscribeHandler"; + + } diff --git a/austin-support/src/main/java/com/java3y/austin/support/config/WeChatLoginAccountConfig.java b/austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java similarity index 71% rename from austin-support/src/main/java/com/java3y/austin/support/config/WeChatLoginAccountConfig.java rename to austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java index 585137c..57d8669 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/config/WeChatLoginAccountConfig.java +++ b/austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java @@ -1,9 +1,11 @@ -package com.java3y.austin.support.config; +package com.java3y.austin.web.config; +import com.java3y.austin.common.constant.OfficialAccountParamConstant; import com.java3y.austin.common.dto.account.WeChatOfficialAccount; import com.java3y.austin.support.utils.WxServiceUtils; import lombok.Data; import me.chanjar.weixin.common.api.WxConsts; +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.config.impl.WxMpDefaultConfigImpl; @@ -14,6 +16,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import javax.annotation.PostConstruct; +import java.util.Map; /** @@ -26,7 +29,7 @@ import javax.annotation.PostConstruct; @Configuration @ConditionalOnProperty(name = "austin.login.officialAccount.enable", havingValue = "true") @Data -public class WeChatLoginAccountConfig { +public class WeChatLoginConfig { @Value("${austin.login.official.account.appId}") private String appId; @@ -38,10 +41,16 @@ public class WeChatLoginAccountConfig { @Autowired private WxServiceUtils wxServiceUtils; + /** + * 微信服务号 登录 相关对象 + */ private WxMpService officialAccountLoginService; private WxMpDefaultConfigImpl config; private WxMpMessageRouter wxMpMessageRouter; + @Autowired + private Map WxMpMessageHandlers; + @PostConstruct private void init() { @@ -53,11 +62,13 @@ public class WeChatLoginAccountConfig { /** * 初始化路由器 + * 扫码、关注、取消关注 */ private void initRouter() { wxMpMessageRouter = new WxMpMessageRouter(officialAccountLoginService); - wxMpMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(null).end(); - wxMpMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE).handler(null).end(); + wxMpMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(WxMpMessageHandlers.get(OfficialAccountParamConstant.SUBSCRIBE_HANDLER)).end(); + wxMpMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SCAN).handler(WxMpMessageHandlers.get(OfficialAccountParamConstant.SCAN_HANDLER)).end(); + wxMpMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE).handler(WxMpMessageHandlers.get(OfficialAccountParamConstant.UNSUBSCRIBE_HANDLER)).end(); } /** diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java index 3f59eae..6b5b97e 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java @@ -14,6 +14,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; + /** * 获取数据接口(全链路追踪) * @@ -49,7 +51,7 @@ public class DataController { @ApiOperation("/获取短信下发数据") public BasicResultVO getSmsData(@RequestBody DataParam dataParam) { if (dataParam == null || dataParam.getDateTime() == null || dataParam.getReceiver() == null) { - return new BasicResultVO<>(RespStatusEnum.SUCCESS, new SmsTimeLineVo()); + return new BasicResultVO<>(RespStatusEnum.SUCCESS, SmsTimeLineVo.builder().items(new ArrayList<>()).build()); } SmsTimeLineVo smsTimeLineVo = dataService.getTraceSmsInfo(dataParam); diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java index 0bc09a7..dfd29dc 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java @@ -7,23 +7,22 @@ import com.alibaba.fastjson.JSON; import com.google.common.base.Throwables; import com.java3y.austin.common.constant.CommonConstant; import com.java3y.austin.common.constant.OfficialAccountParamConstant; -import com.java3y.austin.common.dto.account.WeChatOfficialAccount; import com.java3y.austin.common.enums.RespStatusEnum; import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.support.utils.WxServiceUtils; +import com.java3y.austin.web.config.WeChatLoginConfig; import com.java3y.austin.web.utils.Convert4Amis; import com.java3y.austin.web.vo.amis.CommonAmisVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; -import me.chanjar.weixin.mp.api.WxMpMessageRouter; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import me.chanjar.weixin.mp.bean.template.WxMpTemplate; -import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -48,9 +47,11 @@ public class OfficialAccountController { @Autowired private WxServiceUtils wxServiceUtils; - String appId = "wx2xxxxxb325"; - String secret = "203xxx6db46fa99"; - String token = "austin123"; + @Autowired + private WeChatLoginConfig configService; + + @Autowired + private StringRedisTemplate redisTemplate; /** * @param id 账号Id @@ -110,13 +111,7 @@ public class OfficialAccountController { @ApiOperation("/接收微信的事件消息") public String receiptMessage(HttpServletRequest request, HttpServletResponse response) { try { - WxMpService wxMpService = wxServiceUtils.initOfficialAccountService(WeChatOfficialAccount.builder().appId(appId).secret(secret).token(token).build()); - WxMpMessageRouter wxMpMessageRouter = new WxMpMessageRouter(wxMpService); - - WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); - config.setAppId(appId); - config.setSecret(secret); - config.setToken(token); + WxMpService wxMpService = configService.getOfficialAccountLoginService(); String echoStr = request.getParameter(OfficialAccountParamConstant.ECHO_STR); String signature = request.getParameter(OfficialAccountParamConstant.SIGNATURE); @@ -137,14 +132,14 @@ public class OfficialAccountController { if (OfficialAccountParamConstant.RAW.equals(encryptType)) { WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream()); log.info("raw inMessage:{}", JSON.toJSONString(inMessage)); - WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage); + WxMpXmlOutMessage outMessage = configService.getWxMpMessageRouter().route(inMessage); response.getWriter().write(outMessage.toXml()); } else if (OfficialAccountParamConstant.AES.equals(encryptType)) { String msgSignature = request.getParameter(OfficialAccountParamConstant.MSG_SIGNATURE); - WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), config, timestamp, nonce, msgSignature); + WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), configService.getConfig(), timestamp, nonce, msgSignature); log.info("aes inMessage:{}", JSON.toJSONString(inMessage)); - WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage); - response.getWriter().write(outMessage.toEncryptedXml(config)); + WxMpXmlOutMessage outMessage = configService.getWxMpMessageRouter().route(inMessage); + response.getWriter().write(outMessage.toEncryptedXml(configService.getConfig())); } return RespStatusEnum.SUCCESS.getMsg(); } catch (Exception e) { @@ -163,15 +158,10 @@ public class OfficialAccountController { @ApiOperation("/生成 服务号 二维码") public BasicResultVO getQrCode() { try { - String id = IdUtil.getSnowflake().nextIdStr(); - - // 获取服务号二维码 - WxMpService wxMpService = wxServiceUtils.initOfficialAccountService(WeChatOfficialAccount.builder().appId(appId).secret(secret).build()); + WxMpService wxMpService = configService.getOfficialAccountLoginService(); WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(id, 2592000); String url = wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket()); - - // 存入Redis做校验 return BasicResultVO.success(Convert4Amis.getWxMpQrCode(url)); } catch (Exception e) { log.error("OfficialAccountController#getQrCode fail:{}", Throwables.getStackTraceAsString(e)); diff --git a/austin-web/src/main/java/com/java3y/austin/web/handler/ScanHandler.java b/austin-web/src/main/java/com/java3y/austin/web/handler/ScanHandler.java new file mode 100644 index 0000000..33b70ed --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/handler/ScanHandler.java @@ -0,0 +1,36 @@ +package com.java3y.austin.web.handler; + +import cn.hutool.core.date.DateTime; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.session.WxSessionManager; +import me.chanjar.weixin.mp.api.WxMpMessageHandler; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; +import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @author 3y + * 微信服务号扫码处理器 + */ +@Component("scanHandler") +@Slf4j +public class ScanHandler implements WxMpMessageHandler { + + @Autowired + private StringRedisTemplate redisTemplate; + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService, WxSessionManager sessionManager) { + String content = "您在" + (new DateTime().toString("yyyy-MM-dd HH:mm:ss")) + "通过微信扫码登录austin,感谢您的使用。"; + String openId = wxMessage.getFromUser(); + // 将场景值和用户信息存入redis + //redisTemplate.opsForValue().set(wxMessage.getEventKey(), user, 2, TimeUnit.MINUTES); + return WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) + .content(content).build(); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java b/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java new file mode 100644 index 0000000..eb59506 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java @@ -0,0 +1,38 @@ +package com.java3y.austin.web.handler; + +import cn.hutool.core.date.DateTime; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.session.WxSessionManager; +import me.chanjar.weixin.mp.api.WxMpMessageHandler; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; +import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + + +/** + * @author 3y + * 微信服务号 关注 事件 处理器 + */ +@Component("subscribeHandler") +@Slf4j +public class SubscribeHandler implements WxMpMessageHandler { + + @Autowired + private StringRedisTemplate redisTemplate; + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService, WxSessionManager sessionManager) { + String content = "您在" + (new DateTime().toString("yyyy-MM-dd HH:mm:ss")) + "关注登录austin,感谢您的使用。"; + String openId = wxMessage.getFromUser(); + // 将场景值和用户信息存入redis + redisTemplate.opsForValue().set(wxMessage.getEventKey(), openId, 2, TimeUnit.MINUTES); + return WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) + .content(content).build(); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/handler/UnSubscribeHandler.java b/austin-web/src/main/java/com/java3y/austin/web/handler/UnSubscribeHandler.java new file mode 100644 index 0000000..37c4a80 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/handler/UnSubscribeHandler.java @@ -0,0 +1,31 @@ +package com.java3y.austin.web.handler; + +import cn.hutool.core.date.DateTime; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.session.WxSessionManager; +import me.chanjar.weixin.mp.api.WxMpMessageHandler; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; +import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.Map; + +@Component("unSubscribeHandler") +@Slf4j +public class UnSubscribeHandler implements WxMpMessageHandler { + @Autowired + private StringRedisTemplate redisTemplate; + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService, WxSessionManager sessionManager) { + String content = "您在" + (new DateTime().toString("yyyy-MM-dd HH:mm:ss")) + "关注登录austin,感谢您的使用。"; + String openId = wxMessage.getFromUser(); + // 将场景值和用户信息存入redis + //redisTemplate.opsForValue().set(wxMessage.getEventKey(), user, 2, TimeUnit.MINUTES); + return WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) + .content(content).build(); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/vo/MessageTemplateParam.java b/austin-web/src/main/java/com/java3y/austin/web/vo/MessageTemplateParam.java index 13bba44..74d3af2 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/vo/MessageTemplateParam.java +++ b/austin-web/src/main/java/com/java3y/austin/web/vo/MessageTemplateParam.java @@ -24,13 +24,13 @@ public class MessageTemplateParam { * 当前页码 */ @NotNull - private Integer page; + private Integer page = 1; /** * 当前页大小 */ @NotNull - private Integer perPage; + private Integer perPage = 10; /** * 模板ID