微信服务号登录,事件路由接口定义

master
3y 3 years ago
parent bd35a5bba5
commit c9aafca83c

@ -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";
}

@ -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<String, WxMpMessageHandler> 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();
}
/**

@ -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);

@ -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));

@ -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<String, Object> 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();
}
}

@ -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<String, Object> 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();
}
}

@ -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<String, Object> 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();
}
}

@ -24,13 +24,13 @@ public class MessageTemplateParam {
*
*/
@NotNull
private Integer page;
private Integer page = 1;
/**
*
*/
@NotNull
private Integer perPage;
private Integer perPage = 10;
/**
* ID

Loading…
Cancel
Save