|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
package com.java3y.austin.web.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
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;
|
|
|
|
@ -13,18 +16,23 @@ 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.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 微信服务号
|
|
|
|
@ -32,14 +40,18 @@ import java.util.Map;
|
|
|
|
|
* @author 3y
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/officialAccount")
|
|
|
|
|
@RestController
|
|
|
|
|
@Api("微信服务号")
|
|
|
|
|
public class OfficialAccountController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private WxServiceUtils wxServiceUtils;
|
|
|
|
|
|
|
|
|
|
String appId = "wx2xxxxxb325";
|
|
|
|
|
String secret = "203xxx6db46fa99";
|
|
|
|
|
String token = "austin123";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param id 账号Id
|
|
|
|
|
* @return
|
|
|
|
@ -94,43 +106,77 @@ public class OfficialAccountController {
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/receipt")
|
|
|
|
|
@RequestMapping(value = "/receipt", produces = {CommonConstant.CONTENT_TYPE_XML})
|
|
|
|
|
@ApiOperation("/接收微信的事件消息")
|
|
|
|
|
public String receiptMessage(String signature, String timestamp, String nonce, String echostr) {
|
|
|
|
|
log.info("get weixin signature:{},:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr);
|
|
|
|
|
if (StrUtil.isNotBlank(echostr)) {
|
|
|
|
|
return echostr;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
String echoStr = request.getParameter(OfficialAccountParamConstant.ECHO_STR);
|
|
|
|
|
String signature = request.getParameter(OfficialAccountParamConstant.SIGNATURE);
|
|
|
|
|
String nonce = request.getParameter(OfficialAccountParamConstant.NONCE);
|
|
|
|
|
String timestamp = request.getParameter(OfficialAccountParamConstant.TIMESTAMP);
|
|
|
|
|
|
|
|
|
|
// echoStr!=null,说明只是微信调试的请求
|
|
|
|
|
if (StrUtil.isNotBlank(echoStr)) {
|
|
|
|
|
return echoStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
|
|
|
|
|
return RespStatusEnum.CLIENT_BAD_PARAMETERS.getMsg();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
String encryptType = StrUtil.isBlank(request.getParameter(OfficialAccountParamConstant.ENCRYPT_TYPE)) ? OfficialAccountParamConstant.RAW : request.getParameter(OfficialAccountParamConstant.ENCRYPT_TYPE);
|
|
|
|
|
|
|
|
|
|
if (OfficialAccountParamConstant.RAW.equals(encryptType)) {
|
|
|
|
|
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
|
|
|
|
|
log.info("raw inMessage:{}", JSON.toJSONString(inMessage));
|
|
|
|
|
WxMpXmlOutMessage outMessage = wxMpMessageRouter.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);
|
|
|
|
|
log.info("aes inMessage:{}", JSON.toJSONString(inMessage));
|
|
|
|
|
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
|
|
|
|
response.getWriter().write(outMessage.toEncryptedXml(config));
|
|
|
|
|
}
|
|
|
|
|
return RespStatusEnum.SUCCESS.getMsg();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("OfficialAccountController#receiptMessage fail:{}", Throwables.getStackTraceAsString(e));
|
|
|
|
|
return RespStatusEnum.SERVICE_ERROR.getMsg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 临时给微信服务号登录使用(生成二维码),正常消息推送平台不会有此接口
|
|
|
|
|
*
|
|
|
|
|
* @param accountId 消息推送平台体系内的Id
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/qrCode")
|
|
|
|
|
@PostMapping("/qrCode")
|
|
|
|
|
@ApiOperation("/生成 服务号 二维码")
|
|
|
|
|
public BasicResultVO getQrCode(Long accountId) {
|
|
|
|
|
if (accountId == null) {
|
|
|
|
|
return BasicResultVO.fail(RespStatusEnum.CLIENT_BAD_PARAMETERS);
|
|
|
|
|
}
|
|
|
|
|
public BasicResultVO getQrCode() {
|
|
|
|
|
try {
|
|
|
|
|
// 生成随机值,获取服务号二维码 且 用于校验登录
|
|
|
|
|
String id = IdUtil.getSnowflake().nextIdStr();
|
|
|
|
|
WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(accountId);
|
|
|
|
|
WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(id, 2592000);
|
|
|
|
|
|
|
|
|
|
String id = IdUtil.getSnowflake().nextIdStr();
|
|
|
|
|
|
|
|
|
|
// 获取服务号二维码
|
|
|
|
|
WxMpService wxMpService = wxServiceUtils.initOfficialAccountService(WeChatOfficialAccount.builder().appId(appId).secret(secret).build());
|
|
|
|
|
WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(id, 2592000);
|
|
|
|
|
String url = wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket());
|
|
|
|
|
Map<Object, Object> result = MapUtil.of(new String[][]{{"url", url}, {"id", id}});
|
|
|
|
|
return BasicResultVO.success(result);
|
|
|
|
|
|
|
|
|
|
// 存入Redis做校验
|
|
|
|
|
return BasicResultVO.success(Convert4Amis.getWxMpQrCode(url));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("OfficialAccountController#getQrCode fail:{}", Throwables.getStackTraceAsString(e));
|
|
|
|
|
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|