فهرست منبع

微信消息发送

lin 4 سال پیش
والد
کامیت
46e2af1972

+ 60 - 0
src/main/java/com/zoniot/ccrc/commom/utils/SendWechatPush.java

@@ -0,0 +1,60 @@
+package com.zoniot.ccrc.commom.utils;
+
+import java.io.IOException;
+
+/**
+ * 微信公众号消息推送
+ */
+public class SendWechatPush {
+
+
+    /**
+     * 统一消息发送异常通知(微信公众号和小程序)
+     */
+    public static String sendAbnormalNotice(String accessToken,String oa_appid, String openId, String url,String weapp_appid, String pagepath, String first, String keyword1, String keyword2, String keyword3, String remark) {
+        if (accessToken != null && !"".equals(accessToken)) {
+            String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + accessToken;
+            String json = "{\n" +
+                    "  \"touser\": \""+openId+"\",\n" +
+                    "  \"mp_template_msg\": {\n" +
+                    "    \"appid\": \""+oa_appid +"\",\n" +
+                    "    \"template_id\": \"r8mtd2mgMt8w-sNgHsJ4deoD2bfq-0RyK9lCY3WEoxI\",\n" +
+                    "    \"url\": \""+url+"\",\n" +
+                    "    \"miniprogram\": {\n" +
+                    "      \"appid\": \""+weapp_appid+"\",\n" +
+                    "      \"path\": \""+pagepath+"\"\n" +
+                    "    },\n" +
+                    "    \"data\": {\n" +
+                    "      \"first\": {\n" +
+                    "        \"value\": \""+first+"\",\n" +
+                    "        \"color\": \"#173177\"\n" +
+                    "      },\n" +
+                    "      \"keyword1\": {\n" +
+                    "        \"value\": \""+keyword1+"\",\n" +
+                    "        \"color\": \"#173177\"\n" +
+                    "      },\n" +
+                    "      \"keyword2\": {\n" +
+                    "        \"value\": \""+keyword2+"\",\n" +
+                    "        \"color\": \"#173177\"\n" +
+                    "      },\n" +
+                    "      \"keyword3\": {\n" +
+                    "        \"value\": \""+keyword3+"\",\n" +
+                    "        \"color\": \"#173177\"\n" +
+                    "      },\n" +
+                    "      \"remark\": {\n" +
+                    "        \"value\": \""+remark+"\",\n" +
+                    "        \"color\": \"#173177\"\n" +
+                    "      }\n" +
+                    "    }\n" +
+                    "  }\n" +
+                    "}";
+            try {
+                return HttpRequest.doPost(sendUrl, json);
+            } catch (IOException e) {
+                return null;
+            }
+        } else {
+            return "accessToken不存在";
+        }
+    }
+}

+ 21 - 0
src/main/java/com/zoniot/ccrc/controller/common/TestController.java

@@ -4,6 +4,8 @@ import com.zoniot.ccrc.commom.model.AjaxMessage;
 import com.zoniot.ccrc.commom.model.ResultStatus;
 import com.zoniot.ccrc.commom.utils.ByteArrayUtils;
 import com.zoniot.ccrc.commom.utils.RedisUtil;
+import com.zoniot.ccrc.commom.utils.SendWechatPush;
+import com.zoniot.ccrc.service.AccessTokenService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
@@ -25,6 +27,8 @@ public class TestController {
 
     @Value("${files.path}")
     private String filesPath;
+    @Autowired
+    private AccessTokenService accessTokenService;
 
 
     @GetMapping("test")
@@ -60,4 +64,21 @@ public class TestController {
 
 
     }
+
+    @RequestMapping(value = "send", method = RequestMethod.GET)
+    @ApiOperation(value = "测试微信消息推送")
+    public AjaxMessage send(
+            @ApiParam(value = "opendid", required = true) @RequestParam String opendid
+    ) {
+
+        opendid = "o8iCb5HsMBWq-t_iWNVigekPeCP8";
+        String accessToken = accessTokenService.getMpAccessToken();
+        String url = "https://www.zoniot.com";
+        String pagepath = "pages/index/index";
+
+        String result = SendWechatPush.sendAbnormalNotice(accessToken, "wx6b2ce547cd593413", opendid, url, "wx27f831675081e293", pagepath, "用户用水异常", "水量预警", "单日用水量超过2吨",
+                "2021-02-23 17:59:32", "1030844808林家栋13765510000长泽花园2-1-502楼梯间用水量有异常情况,请及时关注用户情况!");
+        log.info(result);
+        return new AjaxMessage(ResultStatus.OK);
+    }
 }

+ 3 - 0
src/main/java/com/zoniot/ccrc/dao/DeviceMapper.java

@@ -77,5 +77,8 @@ public interface DeviceMapper {
             @Param("longitudeMax") Double longitudeMax,
             @Param("latitudeMin") Double latitudeMin,
             @Param("latitudeMax") Double latitudeMax);
+
     List<PriceInfo> getPriceInfo();
+
+    Device findByDeviceId(@Param("deviceId") Long deviceId);
 }

+ 1 - 1
src/main/java/com/zoniot/ccrc/dao/GridManagementMapper.java

@@ -94,5 +94,5 @@ public interface GridManagementMapper {
     List<Device> getDevices(@Param("buildingId") Integer buildingId,
                             @Param("userId") Integer userId, @Param("address") String address);
 
-    GridManagement getByDeviceId(@Param("deviceId") Long deviceId);
+    List<GridManagement> getByDeviceId(@Param("deviceId") Long deviceId);
 }

+ 48 - 7
src/main/java/com/zoniot/ccrc/scheduled/WarningMessageJob.java

@@ -2,15 +2,14 @@ package com.zoniot.ccrc.scheduled;
 
 
 
-import com.zoniot.ccrc.dao.WarningLogMapper;
-import com.zoniot.ccrc.dao.WarningMessageMapper;
-import com.zoniot.ccrc.dao.WarningRuleMapper;
-import com.zoniot.ccrc.entity.WarningLog;
-import com.zoniot.ccrc.entity.WarningMessage;
-import com.zoniot.ccrc.entity.WarningRule;
+import com.zoniot.ccrc.commom.utils.SendWechatPush;
+import com.zoniot.ccrc.dao.*;
+import com.zoniot.ccrc.entity.*;
+import com.zoniot.ccrc.service.AccessTokenService;
 import com.zoniot.ccrc.service.MongoMeterReadRecordService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -37,6 +36,20 @@ public class WarningMessageJob {
     private WarningMessageMapper warningMessageMapper;
     @Autowired
     private MongoMeterReadRecordService mongoMeterReadRecordService;
+    @Resource
+    private DeviceMapper deviceMapper;
+    @Resource
+    private GridManagementMapper gridManagementMapper;
+    @Autowired
+    private AccessTokenService accessTokenService;
+    @Resource
+    private UserMapper userMapper;
+
+    @Value("${wechat.oa.appid}")
+    private String oaAppid;
+
+    @Value("${wechat.mp.appid}")
+    private String mpAppid;
 
 
     /**
@@ -50,7 +63,9 @@ public class WarningMessageJob {
             for (WarningRule warningRule : list) {
                     Boolean flag =  mongoMeterReadRecordService.complyWithWarningRules(warningRule.getDeviceId(),warningRule.getWarningType());
                     if ( flag) {
-                        addMessageInfo(warningRule,"单日用水量超过2吨");
+                        String content =  "单日用水量超过2吨";
+                        addMessageInfo(warningRule,content);
+                        sendWechatMessage(warningRule.getDeviceId(),content);
                 }
 
 
@@ -83,4 +98,30 @@ public class WarningMessageJob {
         warningMessageNew.setDateUpdate(LocalDateTime.now());
         warningMessageMapper.insertSelective(warningMessageNew);
     }
+
+    private void sendWechatMessage(Long deviceId,String content){
+        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        String url = "https://www.zoniot.com";
+        String pagepath = "pages/index/index";
+        Device device = deviceMapper.findByDeviceId(deviceId);
+
+        String accessToken = accessTokenService.getMpAccessToken();
+
+        List<GridManagement> gridManagementList = gridManagementMapper.getByDeviceId(deviceId);
+        if (gridManagementList != null && gridManagementList.size() > 0) {
+            for (GridManagement gridManagement : gridManagementList) {
+                User user = userMapper.findUserById(gridManagement.getUserId());
+
+                if(user != null && user.getOpenid() !=null && !"".equals(user.getOpenid())){
+
+                    String result = SendWechatPush.sendAbnormalNotice(accessToken, oaAppid, user.getOpenid(), url, mpAppid, pagepath, "用户用水异常", "水量预警", content,
+                            LocalDateTime.now().format(df), device.getMeterNo()+gridManagement.getCustomerName()+gridManagement.getCustomerPhone()+device.getLocDesc()+"用水量有异常情况,请及时关注用户情况!");
+                    log.info("sendWechatMessage :"+result);
+                }
+            }
+        }
+
+
+
+    }
 }

+ 54 - 0
src/main/java/com/zoniot/ccrc/service/AccessTokenService.java

@@ -0,0 +1,54 @@
+package com.zoniot.ccrc.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.zoniot.ccrc.commom.utils.HttpRequest;
+import com.zoniot.ccrc.commom.utils.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+
+@Slf4j
+@Service
+public class AccessTokenService {
+    @Autowired
+    private RedisUtil redisUtil;
+
+
+    @Value("${wechat.mp.appid}")
+    private String mpAppid;
+    @Value("${wechat.mp.app-secret}")
+    private String mpAppSecret;
+
+    /**
+     * 获取微信小程序accessToken
+     * @return
+     */
+    public String getMpAccessToken(){
+        String token = null;
+        token = redisUtil.get("ccrcMpAccessToken");
+        if (token != null) {
+            return token;
+        }else {
+            String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +
+                    mpAppid + "&secret=" + mpAppSecret;
+            try {
+                String res = HttpRequest.doGet(tokenUrl);
+                JSONObject jsonObj = (JSONObject) JSONObject.parse(res);
+                log.info(jsonObj.toJSONString());
+                String accessTokenTem = jsonObj.getString("access_token");
+                if (accessTokenTem != null && !"".equals(accessTokenTem)) {
+                    redisUtil.setExpire("ccrcMpAccessToken",accessTokenTem,300);
+                    return accessTokenTem;
+                } else {
+                    log.info("请求token错误");
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return token;
+    }
+}

+ 13 - 7
src/main/resources/application-dev.properties

@@ -69,7 +69,8 @@ files.path=${project.path}/data
 
 #物联网url
 iot.url=http://114.135.61.188:58080
-
+iot.server=114.135.61.188
+iot.port=58088
 
 #########################################Rabbit MQ ����#############################################
 spring.rabbitmq.host=114.135.61.188
@@ -78,22 +79,27 @@ spring.rabbitmq.username=zoniot
 spring.rabbitmq.password=zcxk100
 spring.rabbitmq.virtual-host=/
 spring.rabbitmq.connection-timeout=1000ms
-# ��������ȷ��
+
 spring.rabbitmq.publisher-confirm-type=correlated
-# ��������ʧ���˻�
+
 spring.rabbitmq.publisher-returns=true
 spring.rabbitmq.template.mandatory=true
-# ����ACK
+
 spring.rabbitmq.listener.direct.acknowledge-mode=manual
 spring.rabbitmq.listener.simple.acknowledge-mode=manual
-# �������
+
 spring.rabbitmq.exchange=sync-handler-exchange
 spring.rabbitmq.device.queue=sync-device-handler-queue
 spring.rabbitmq.community.queue=sync-community-handler-queue
 spring.rabbitmq.building.queue=sync-building-handler-queue
 
-iot.server=114.135.61.188
-iot.port=58088
+
+
+#中城信科公众号
+wechat.oa.appid=wx6b2ce547cd593413
+#小程序水查查服务
+wechat.mp.appid=wx27f831675081e293
+wechat.mp.app-secret=a01d7f75f15e1a6b7fa28a317f97ef1b
 
 
 

+ 33 - 1
src/main/resources/application-prd.properties

@@ -63,4 +63,36 @@ spring.servlet.multipart.location=${project.path}/data
 files.path=${project.path}/data
 
 #物联网url
-iot.url=http://39.108.175.9:8090
+iot.url=http://39.108.175.9:8090
+iot.server=114.135.61.188
+iot.port=58088
+
+#########################################Rabbit MQ ����#############################################
+spring.rabbitmq.host=114.135.61.188
+spring.rabbitmq.port=55672
+spring.rabbitmq.username=zoniot
+spring.rabbitmq.password=zcxk100
+spring.rabbitmq.virtual-host=/
+spring.rabbitmq.connection-timeout=1000ms
+
+spring.rabbitmq.publisher-confirm-type=correlated
+
+spring.rabbitmq.publisher-returns=true
+spring.rabbitmq.template.mandatory=true
+
+spring.rabbitmq.listener.direct.acknowledge-mode=manual
+spring.rabbitmq.listener.simple.acknowledge-mode=manual
+
+spring.rabbitmq.exchange=sync-handler-exchange
+spring.rabbitmq.device.queue=sync-device-handler-queue
+spring.rabbitmq.community.queue=sync-community-handler-queue
+spring.rabbitmq.building.queue=sync-building-handler-queue
+
+
+
+
+#中城信科公众号
+wechat.oa.appid=wx6b2ce547cd593413
+#小程序水查查服务
+wechat.mp.appid=wx27f831675081e293
+wechat.mp.app-secret=a01d7f75f15e1a6b7fa28a317f97ef1b

+ 31 - 0
src/main/resources/application-sit.properties

@@ -65,3 +65,34 @@ files.path=${project.path}/data
 
 #物联网url
 iot.url=http://114.135.61.188:58080
+iot.server=114.135.61.188
+iot.port=58088
+
+#########################################Rabbit MQ ����#############################################
+spring.rabbitmq.host=114.135.61.188
+spring.rabbitmq.port=55672
+spring.rabbitmq.username=zoniot
+spring.rabbitmq.password=zcxk100
+spring.rabbitmq.virtual-host=/
+spring.rabbitmq.connection-timeout=1000ms
+
+spring.rabbitmq.publisher-confirm-type=correlated
+
+spring.rabbitmq.publisher-returns=true
+spring.rabbitmq.template.mandatory=true
+
+spring.rabbitmq.listener.direct.acknowledge-mode=manual
+spring.rabbitmq.listener.simple.acknowledge-mode=manual
+
+spring.rabbitmq.exchange=sync-handler-exchange
+spring.rabbitmq.device.queue=sync-device-handler-queue
+spring.rabbitmq.community.queue=sync-community-handler-queue
+spring.rabbitmq.building.queue=sync-building-handler-queue
+
+
+
+#中城信科公众号
+wechat.oa.appid=wx6b2ce547cd593413
+#小程序水查查服务
+wechat.mp.appid=wx27f831675081e293
+wechat.mp.app-secret=a01d7f75f15e1a6b7fa28a317f97ef1b

+ 4 - 0
src/main/resources/mapper/DeviceMapper.xml

@@ -643,4 +643,8 @@
   <select id="getPriceInfo" resultType="com.zoniot.ccrc.dto.PriceInfo">
     select * from prict_info
   </select>
+
+  <select id="findByDeviceId" resultMap="BaseResultMap">
+    select * from sc_device where status = 1 and id = #{deviceId}
+  </select>
 </mapper>

+ 1 - 1
src/main/resources/mapper/GridManagementMapper.xml

@@ -227,6 +227,6 @@
     </select>
 
     <select id="getByDeviceId" resultMap="GridManagementMap">
-        select * from sc_grid_management where device_id = #{deviceId}
+        select * from sc_grid_management where status = 1 and device_id = #{deviceId}
     </select>
 </mapper>