Kaynağa Gözat

fixed: 邀请紧急联系人

orgycat 3 yıl önce
ebeveyn
işleme
7281e8b628

+ 20 - 1
src/main/java/com/care/client/controller/ContactApiController.java

@@ -115,7 +115,7 @@ public class ContactApiController {
     @ApiOperation(tags = {"我的紧急联系人,我的设备"},value = "紧急联系人新增 -- 三期新增")
     public Result<Object> addOrUpdateMyContact(@RequestBody HouseContactVO vo){
         try {
-            if (this.pinanbaoContactService.createOrUpdateMyContact(vo)) {
+            if (this.pinanbaoContactService.createOrUpdateMyContact(null, vo)) {
                 return Result.success("保存成功!");
             } else {
                 return Result.error("保存失败!");
@@ -129,6 +129,25 @@ public class ContactApiController {
         }
     }
 
+    @PostMapping("/my/getMyContact")
+    @ApiOperation(tags = {"我的紧急联系人,我的设备"},value = "紧急联系人查询 -- 三期新增")
+    public Result<HouseContactVO> getMyContact(@RequestBody HouseContactVO vo){
+        try {
+            HouseContactVO myContact = this.pinanbaoContactService.getMyContact(vo.getMemberId(), vo.getContactPhone(), vo.getOpenId());
+            if (myContact != null) {
+                return Result.success("查询成功!", myContact);
+            } else {
+                return Result.error("查询失败!");
+            }
+        }catch (BDException e) {
+            log.error("保存紧急联系人-出现异常",e);
+            return Result.error(e.getMessage());
+        } catch (Exception e) {
+            log.error("保存联系人: 保存紧急联系人出现异常",e);
+            return Result.error("保存失败!");
+        }
+    }
+
     @DeleteMapping("/my/delete/{contactId}")
     @ApiOperation(tags = {"我的紧急联系人"},value = "删除紧急联系人 -- 二期新增")
     public Result<Object> deleteMyContact(HttpServletRequest request,@RequestHeader("token") String token,

+ 52 - 34
src/main/java/com/care/client/controller/WxMsgController.java

@@ -2,7 +2,10 @@ package com.care.client.controller;
 
 import cn.hutool.core.util.XmlUtil;
 import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
 import com.care.client.dto.TemplateDataDTO;
+import com.care.client.service.PinanbaoContactService;
+import com.care.client.vo.HouseContactVO;
 import com.care.client.vo.WxAccessTokenVO;
 import com.care.common.cache.RedisKeyConstant;
 import com.care.common.cache.RedisUtil;
@@ -10,6 +13,7 @@ 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.WxQrCodeService;
 import com.care.common.service.WxTemplateService;
 import com.care.common.util.CheckoutUtil;
 import com.care.common.util.MessageUtil;
@@ -17,6 +21,7 @@ import com.care.common.util.WxTemplateUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -50,7 +55,7 @@ public class WxMsgController {
     private WxTemplateService wxService;
 
     @Resource
-    private CareHouseContactService careHouseContactService;
+    private PinanbaoContactService pinanbaoContactService;
 
     @Resource
     private WxTemplateService wxTemplateService;
@@ -61,6 +66,9 @@ public class WxMsgController {
     @Resource
     private CareDeviceService careDeviceService;
 
+    @Resource
+    private WxQrCodeService wxQrCodeService;
+
     /**
      * 微信消息接收和token验证
      * @param request
@@ -105,7 +113,8 @@ public class WxMsgController {
         resp.setCharacterEncoding("UTF-8");
         String message = "success";
         try {
-            WxAccessTokenVO wxAccessTokenVO = wxService.processAccessToken();
+//            WxAccessTokenVO wxAccessTokenVO = wxService.processAccessToken();
+            WxAccessTokenVO wxAccessTokenVO = wxQrCodeService.processOfficialToken();
             if (wxAccessTokenVO == null) {
                 return "failure";
             }
@@ -122,8 +131,8 @@ public class WxMsgController {
 //            String nickName = getUserNickName(fromUserName);
             if(MessageUtil.MSGTYPE_EVENT.equals(msgType)){//如果为事件类型
                 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");
 
                     String eventKey = ObjectUtils.toString(map.get("EventKey"));
@@ -146,22 +155,7 @@ public class WxMsgController {
                         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")) {
+                    if (parseUnionObject != null) {
 //                        CareOfficialMemberInfo careOfficialMemberInfo = new CareOfficialMemberInfo();
 //                        careOfficialMemberInfo.setNickname(parseUnionObject.getStr("nickname"));
 //                        careOfficialMemberInfo.setOpenId(fromUserName);
@@ -175,16 +169,40 @@ public class WxMsgController {
 //                        } else {
 //                            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);
+                        HouseContactVO houseContactVO = new HouseContactVO();
+                        houseContactVO.setContactName(parseUnionObject.getStr("nickname"));
+                        houseContactVO.setCreateTime(new Date());
+                        String gender = "";
+                        if ("0".equalsIgnoreCase(parseUnionObject.getStr("sex"))) {
+                            gender = "M";
+                        } else if ("1".equalsIgnoreCase(parseUnionObject.getStr("sex"))) {
+                            gender = "W";
+                        }
+                        houseContactVO.setGender(gender);
+                        houseContactVO.setOpenId(fromUserName);
+                        houseContactVO.setMemberId(NumberUtils.toLong(arrs[1]));
+                        boolean saved = pinanbaoContactService.createOrUpdateMyContact(NumberUtils.toLong(arrs[0]), houseContactVO);
+                        if (!saved) {
+                            log.error("保存订阅用户出现错误: " + JSONUtil.toJsonStr(houseContactVO));
+                            return "failure";
+                        }
+
+                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        // 正式环境
+                        String templateId = "W7tT8QGmrBQNVimt6l6w55DBwSH5MBUiFgJxf3gjIV4";
+                        // 测试环境
+//                    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;
+                        log.info("生成小程序跳转URL: " + "/pages/supplement/index?memberId=" + arrs[1] + "&openId=" + fromUserName);
+                        boolean flag = wxTemplateService.sendTemplateMsgOriginal(openId, templateId, "wxda02870032b8f928", "/pages/supplement/index?memberId=" + arrs[1] + "&openId=" + openId, data);
+                        log.info("发送消息模板: openId: " + openId + ", templateId: " + templateId + " 状态: " + flag);
 
 //                        String eventKey = ObjectUtils.toString(map.get("EventKey"));
 //                        if (StringUtils.isNotBlank(eventKey) && eventKey.indexOf("_") != -1) {
@@ -210,18 +228,18 @@ public class WxMsgController {
 
 //                        careHouseContactService.save();
                         return "success";
-//                    }
+                    }
                 } else if (MessageUtil.UNSUBSCIBE_EVENT.equals(eventType)) {//处理取消订阅事件
                     System.out.println("事件类型为:"+eventType);
-                    JSONObject parseUnionObject = WxTemplateUtil.parseWxOpenIdBySubscribe(wxAccessTokenVO.getAccessToken(), fromUserName);
-                    if (parseUnionObject != null && parseUnionObject.containsKey("unionid")) {
+//                    JSONObject parseUnionObject = WxTemplateUtil.parseWxOpenIdBySubscribe(wxAccessTokenVO.getAccessToken(), fromUserName);
+//                    if (parseUnionObject != null && parseUnionObject.containsKey("unionid")) {
 //                        boolean updated = careOfficialMemberInfoService.cancelSubscribe(parseUnionObject.getStr("unionid"));
 //                        if (updated) {
 //                            return "success";
 //                        } else {
 //                            return "failure";
 //                        }
-                    }
+//                    }
                     return "success";
 //                    message = MessageUtil.unsubscribe(toUserName, fromUserName);
                 }

+ 42 - 3
src/main/java/com/care/client/service/PinanbaoContactService.java

@@ -5,12 +5,15 @@ import cn.hutool.core.collection.CollUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.care.client.vo.HouseContactVO;
 import com.care.common.entity.*;
+import com.care.common.enums.OrderStatusEnum;
 import com.care.common.enums.RelationTypeEnum;
+import com.care.common.exception.BDException;
 import com.care.common.service.*;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.compress.utils.Lists;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -34,6 +37,10 @@ public class PinanbaoContactService {
 
     @Resource
     private CareHouseContactRelService careHouseContactRelService;
+
+    @Resource
+    private PinanbaoDeviceService pinanbaoDeviceService;
+
     /**
      * 查询紧急联系人
      * @param memberId
@@ -71,15 +78,39 @@ public class PinanbaoContactService {
         return this.careHouseContactService.save(careContact);
     }
 
+    /**
+     * 查询紧急联系人
+     * @return
+     */
+    public HouseContactVO getMyContact(Long memberId, String phone, String openId) {
+        if (memberId == null) return null;
+        if (StringUtils.isBlank(phone) && StringUtils.isBlank(openId)) return null;
+        QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
+        queryWrapper.lambda().eq(CareHouseContact::getMemberId, memberId).and(wrapper->
+                wrapper.eq(StringUtils.isNotBlank(phone), CareHouseContact::getContactPhone, phone)
+                        .or().eq(StringUtils.isNotBlank(openId), CareHouseContact::getOpenId, openId));
+        List<CareHouseContact> contactList = careHouseContactService.list(queryWrapper);
+
+        if (CollectionUtils.isNotEmpty(contactList)) {
+            HouseContactVO vo = new HouseContactVO();
+            BeanUtil.copyProperties(contactList.get(0), vo);
+            vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
+            return vo;
+        }
+        return null;
+    }
+
     /**
      * 新增修改紧急联系人
      * @param vo
      * @return
      */
     @Transactional(rollbackFor = Exception.class)
-    public Boolean createOrUpdateMyContact(HouseContactVO vo) {
+    public Boolean createOrUpdateMyContact(Long devId, HouseContactVO vo) {
         QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
-        queryWrapper.lambda().eq(CareHouseContact::getMemberId, vo.getMemberId()).eq(CareHouseContact::getContactPhone, vo.getContactPhone());
+        queryWrapper.lambda().eq(CareHouseContact::getMemberId, vo.getMemberId()).and(wrapper->
+                wrapper.eq(StringUtils.isNotBlank(vo.getContactPhone()), CareHouseContact::getContactPhone, vo.getContactPhone())
+                    .or().eq(StringUtils.isNotBlank(vo.getOpenId()), CareHouseContact::getOpenId, vo.getOpenId()));
         List<CareHouseContact> contactList = careHouseContactService.list(queryWrapper);
         if (CollectionUtils.isEmpty(contactList)) {
             //保存被监护人
@@ -87,11 +118,19 @@ public class PinanbaoContactService {
             BeanUtil.copyProperties(vo,careContact);
             careContact.setMemberId(vo.getMemberId());
             careContact.setCreateTime(new Date());
-            return this.careHouseContactService.save(careContact);
+            boolean saved = this.careHouseContactService.save(careContact);
+            if (saved) {
+                boolean childSaved = pinanbaoDeviceService.bindHouseContact(devId, careContact.getId());
+                if (childSaved == false) {
+                    throw new BDException("该设备绑定联系人出错");
+                }
+            }
+            return saved;
         } else {
             //修改被监护人
             CareHouseContact careContact = contactList.get(0);
             careContact.setContactName(vo.getContactName());
+            careContact.setContactPhone(vo.getContactPhone());
             careContact.setAddr(vo.getAddr());
             careContact.setLatitude(vo.getLatitude());
             careContact.setLongitude(vo.getLongitude());

+ 3 - 0
src/main/java/com/care/client/vo/HouseContactVO.java

@@ -66,6 +66,9 @@ public class HouseContactVO implements Serializable {
     @ApiModelProperty("纬度")
     private Double latitude;
 
+    @ApiModelProperty("公众号OPENID")
+    private String openId;
+
 
     @ApiModelProperty("createTime")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")

+ 3 - 0
src/main/java/com/care/common/entity/CareHouseContact.java

@@ -82,6 +82,9 @@ public class CareHouseContact implements Serializable {
     @TableField("LATITUDE")
     private Double latitude;
 
+    @ApiModelProperty("公众号OPENID")
+    @TableField("openid")
+    private String openId;
 
     @ApiModelProperty("createTime")
     @TableField("CREATE_TIME")