|
@@ -2,13 +2,21 @@ package com.care.client.controller;
|
|
|
|
|
|
import cn.hutool.core.util.XmlUtil;
|
|
import cn.hutool.core.util.XmlUtil;
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
+import com.care.client.dto.TemplateDataDTO;
|
|
import com.care.client.vo.WxAccessTokenVO;
|
|
import com.care.client.vo.WxAccessTokenVO;
|
|
|
|
+import com.care.common.cache.RedisKeyConstant;
|
|
|
|
+import com.care.common.cache.RedisUtil;
|
|
|
|
+import com.care.common.entity.CareDevice;
|
|
|
|
+import com.care.common.entity.CareHouseContact;
|
|
|
|
+import com.care.common.service.CareDeviceService;
|
|
|
|
+import com.care.common.service.CareHouseContactService;
|
|
import com.care.common.service.WxTemplateService;
|
|
import com.care.common.service.WxTemplateService;
|
|
import com.care.common.util.CheckoutUtil;
|
|
import com.care.common.util.CheckoutUtil;
|
|
import com.care.common.util.MessageUtil;
|
|
import com.care.common.util.MessageUtil;
|
|
import com.care.common.util.WxTemplateUtil;
|
|
import com.care.common.util.WxTemplateUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
@@ -21,7 +29,11 @@ import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.io.PrintWriter;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
@Controller
|
|
@Controller
|
|
@RequestMapping("/wxmsg")
|
|
@RequestMapping("/wxmsg")
|
|
@@ -37,6 +49,18 @@ public class WxMsgController {
|
|
@Resource
|
|
@Resource
|
|
private WxTemplateService wxService;
|
|
private WxTemplateService wxService;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private CareHouseContactService careHouseContactService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private WxTemplateService wxTemplateService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private CareDeviceService careDeviceService;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 微信消息接收和token验证
|
|
* 微信消息接收和token验证
|
|
* @param request
|
|
* @param request
|
|
@@ -96,10 +120,46 @@ public class WxMsgController {
|
|
// String nickName = getUserNickName(fromUserName);
|
|
// String nickName = getUserNickName(fromUserName);
|
|
if(MessageUtil.MSGTYPE_EVENT.equals(msgType)){//如果为事件类型
|
|
if(MessageUtil.MSGTYPE_EVENT.equals(msgType)){//如果为事件类型
|
|
if(MessageUtil.SUBSCIBE_EVENT.equals(eventType)){//处理订阅事件
|
|
if(MessageUtil.SUBSCIBE_EVENT.equals(eventType)){//处理订阅事件
|
|
- JSONObject parseUnionObject = WxTemplateUtil.parseWxOpenIdBySubscribe(wxAccessTokenVO.getAccessToken(), fromUserName);
|
|
|
|
- System.out.println("parseUnionObject:"+parseUnionObject.toString());
|
|
|
|
|
|
+// JSONObject parseUnionObject = WxTemplateUtil.parseWxOpenIdBySubscribe(wxAccessTokenVO.getAccessToken(), fromUserName);
|
|
|
|
+// System.out.println("parseUnionObject:"+parseUnionObject.toString());
|
|
// parseUnionObject.set("unionid", "oyVYT6Ox9b88n_33lgKWRj5uFbUY");
|
|
// parseUnionObject.set("unionid", "oyVYT6Ox9b88n_33lgKWRj5uFbUY");
|
|
- if (parseUnionObject != null && parseUnionObject.containsKey("unionid")) {
|
|
|
|
|
|
+
|
|
|
|
+ String eventKey = ObjectUtils.toString(map.get("EventKey"));
|
|
|
|
+ if (StringUtils.isNotBlank(eventKey) && eventKey.indexOf("_") != -1) {
|
|
|
|
+ eventKey = StringUtils.substring(eventKey, eventKey.indexOf("_") + 1);
|
|
|
|
+ }
|
|
|
|
+ Object obj = redisUtil.get(RedisKeyConstant.YUANSERVICE_QRCODE + ":" + eventKey);
|
|
|
|
+ log.info("新用户关注: " + fromUserName + ": " + ObjectUtils.toString(map.get("EventKey")));
|
|
|
|
+ if (obj == null) {
|
|
|
|
+ return "failure";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String[] arrs = Objects.toString(obj).split(":");
|
|
|
|
+ if (arrs == null || arrs.length != 2) {
|
|
|
|
+ return "failure";
|
|
|
|
+ }
|
|
|
|
+ CareDevice careDevice = careDeviceService.getById(arrs[0]);
|
|
|
|
+ if (careDevice == null) {
|
|
|
|
+ log.info("设备不存在: " + arrs[0]);
|
|
|
|
+ return "failure";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ // 正式环境
|
|
|
|
+ String templateId = "1M1kDRDW-kMSoOzmv9cP-SImD1kXWtezwyWe8euUJIc";
|
|
|
|
+ // 测试环境
|
|
|
|
+// String templateId = "gvJ1g3JOThT5IuQc1In6D2cwKwmKz9MBIkGdkXtXOTI";
|
|
|
|
+
|
|
|
|
+ Map<String, TemplateDataDTO> data = new HashMap<>();
|
|
|
|
+ data.put("first", new TemplateDataDTO("您已关联该监护人", "#459ae9"));
|
|
|
|
+ data.put("keyword1", new TemplateDataDTO(careDevice.getDevName(), "#459ae9"));
|
|
|
|
+ data.put("keyword2", new TemplateDataDTO(simpleDateFormat.format(new Date()), "#459ae9"));
|
|
|
|
+ data.put("remark", new TemplateDataDTO("请补全您的联系信息, 便于能及时收到事件通知", "#459ae9"));
|
|
|
|
+
|
|
|
|
+ String openId = fromUserName;
|
|
|
|
+ boolean flag = wxTemplateService.sendTemplateMsgOriginal(openId, templateId, "wxda02870032b8f928", "/pages/supplement/index?memberId=" + arrs[1], data);
|
|
|
|
+ log.info("发送消息模板: openId: " + openId + ", templateId: " + templateId + " 状态: " + flag);
|
|
|
|
+// if (parseUnionObject != null && parseUnionObject.containsKey("unionid")) {
|
|
// CareOfficialMemberInfo careOfficialMemberInfo = new CareOfficialMemberInfo();
|
|
// CareOfficialMemberInfo careOfficialMemberInfo = new CareOfficialMemberInfo();
|
|
// careOfficialMemberInfo.setNickname(parseUnionObject.getStr("nickname"));
|
|
// careOfficialMemberInfo.setNickname(parseUnionObject.getStr("nickname"));
|
|
// careOfficialMemberInfo.setOpenId(fromUserName);
|
|
// careOfficialMemberInfo.setOpenId(fromUserName);
|
|
@@ -113,8 +173,42 @@ public class WxMsgController {
|
|
// } else {
|
|
// } else {
|
|
// return "failure";
|
|
// return "failure";
|
|
// }
|
|
// }
|
|
|
|
+// CareHouseContact careHouseContact = new CareHouseContact();
|
|
|
|
+// careHouseContact.setContactName(parseUnionObject.getStr("nickname"));
|
|
|
|
+// careHouseContact.setCreateTime(new Date());
|
|
|
|
+// String gender = "";
|
|
|
|
+// if ("1".equalsIgnoreCase(parseUnionObject.getStr("sex"))) {
|
|
|
|
+// gender = "M";
|
|
|
|
+// } else if ("2".equalsIgnoreCase(parseUnionObject.getStr("sex"))) {
|
|
|
|
+// gender = "W";
|
|
|
|
+// }
|
|
|
|
+// careHouseContact.setGender(gender);
|
|
|
|
+
|
|
|
|
+// String eventKey = ObjectUtils.toString(map.get("EventKey"));
|
|
|
|
+// if (StringUtils.isNotBlank(eventKey) && eventKey.indexOf("_") != -1) {
|
|
|
|
+// eventKey = StringUtils.substring(eventKey, eventKey.indexOf("_") + 1);
|
|
|
|
+// }
|
|
|
|
+// Object obj = redisUtil.get(RedisKeyConstant.YUANSERVICE_QRCODE + ":" + eventKey);
|
|
|
|
+// if (obj == null) {
|
|
|
|
+// return "failure";
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+// String templateId = "OPENTM418029089";
|
|
|
|
+//
|
|
|
|
+// Map<String, TemplateDataDTO> data = new HashMap<>();
|
|
|
|
+// data.put("first", new TemplateDataDTO("您已成功关联该设备", "#ff5200"));
|
|
|
|
+// data.put("keyword1", new TemplateDataDTO("跌倒报警", "#000000"));
|
|
|
|
+// data.put("keyword2", new TemplateDataDTO(simpleDateFormat.format(new Date()), "#000000"));
|
|
|
|
+// data.put("remark", new TemplateDataDTO("请尽快补全您的联系信息, 便于能及时通知您", "#ff5200"));
|
|
|
|
+//
|
|
|
|
+// String openId = parseUnionObject.getStr("openid");
|
|
|
|
+// boolean flag = wxTemplateService.sendTemplateMsg(parseUnionObject.getStr("openid"), templateId, "wxda02870032b8f928", "/pages/supplement/index", data);
|
|
|
|
+// log.info("发送消息模板: openId: " + openId + ", templateId: " + templateId + " 状态: " + flag);
|
|
|
|
+
|
|
|
|
+// careHouseContactService.save();
|
|
return "success";
|
|
return "success";
|
|
- }
|
|
|
|
|
|
+// }
|
|
} else if (MessageUtil.UNSUBSCIBE_EVENT.equals(eventType)) {//处理取消订阅事件
|
|
} else if (MessageUtil.UNSUBSCIBE_EVENT.equals(eventType)) {//处理取消订阅事件
|
|
System.out.println("事件类型为:"+eventType);
|
|
System.out.println("事件类型为:"+eventType);
|
|
JSONObject parseUnionObject = WxTemplateUtil.parseWxOpenIdBySubscribe(wxAccessTokenVO.getAccessToken(), fromUserName);
|
|
JSONObject parseUnionObject = WxTemplateUtil.parseWxOpenIdBySubscribe(wxAccessTokenVO.getAccessToken(), fromUserName);
|