Просмотр исходного кода

'开发微信消息提醒接口,创建请求类,根据消息模板填充信息,推送消息'

james 1 год назад
Родитель
Сommit
4a49933ecf

+ 2 - 2
service-iot/service-iot-biz/src/main/java/com/usky/iot/constant/constant.java

@@ -18,9 +18,9 @@ public class constant {
     // 微信公众号消息推送地址
     public static final String WE_CHAT_REQUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
     // 微信公众号推送消息模板id
-    public static final String WE_CHAT_TEMPLATE_ID = "**********";
+    public static final String WE_CHAT_TEMPLATE_ID = "jbgHt8W8RNpRN2KZwvAMty40iiZU2sa9dqnFXOsCvqw";
     // 微信公众号的消息回调地址(这儿可根据业务需求自定义动作,可选)
-    public static final String WE_CHAT_CUSTOMER_CALL_URL = "https://***.***.***.***/***/***/accountInfo?keyword=%s";
+    public static final String WE_CHAT_CUSTOMER_CALL_URL = "http://manager.usky.cn/mobile/#/pages/infoDetails?id=%s";
     // 微信公众号的主题颜色
     public static final String WE_CHAT_TOP_COLOR = "#A349A4";
     // 微信公众号微信用户授权地址

+ 21 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/MceMbuserController.java

@@ -0,0 +1,21 @@
+package com.usky.iot.controller.web;
+
+
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import org.springframework.stereotype.Controller;
+
+/**
+ * <p>
+ * 消息中心_移动端注册表 前端控制器
+ * </p>
+ *
+ * @author han
+ * @since 2024-04-26
+ */
+@Controller
+@RequestMapping("/mceMbuser")
+public class MceMbuserController {
+
+}
+

+ 51 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/WeChatController.java

@@ -1,5 +1,8 @@
 package com.usky.iot.controller.web;
 
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
@@ -13,11 +16,13 @@ import com.usky.iot.domain.SysWxOpuser;
 import com.usky.iot.service.SysWxOpuserService;
 import com.usky.iot.service.vo.SendWeChatMessageRequestVO;
 import com.usky.iot.service.vo.SignUpRequestVO;
+import com.usky.iot.service.vo.TemplateMsgEntityVO;
 import lombok.extern.slf4j.Slf4j;
 import ma.glasnost.orika.impl.util.StringUtil;
 import me.chanjar.weixin.common.api.WxConsts;
 import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
 import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.enums.WxMpApiUrl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.ExitCodeEvent;
 import org.springframework.web.bind.annotation.*;
@@ -307,6 +312,52 @@ public class WeChatController {
 //
 //    }
 
+    /**
+     * 微信消息提醒
+     * @return
+     */
+    @PostMapping("sendWeChatMessage")
+    public JSONObject sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO){
+
+        String infoType = requestVO.getInfoType();
+        String infoTitle = requestVO.getInfoTitle();
+        String infoContent = requestVO.getInfoContent();
+        Integer infoId = requestVO.getInfoId();
+        String openId = requestVO.getOpenId();
+        String token = this.getWeChatAccessToken();
+
+        TemplateMsgEntityVO messageVo=new TemplateMsgEntityVO();
+        messageVo.setTTitle(infoTitle);
+        messageVo.setTKeyword1("测试1");
+        messageVo.setTKeyword2("测试2");
+        messageVo.setTKeyword3("测试3");
+        messageVo.setTKeyword4("测试4");
+        messageVo.setTRemark(infoContent);
+        messageVo.setTUrl(String.format(constant.WE_CHAT_CUSTOMER_CALL_URL,infoId));
+        messageVo.setTemplateId(constant.WE_CHAT_TEMPLATE_ID);
+
+        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="  + token;
+        Map<String,Object> content=new HashMap<>();
+        JSONObject data = new JSONObject();
+        data.put("first",new JSONObject().put("value",messageVo.getTTitle()));
+        data.put("keyword1",new JSONObject().put("value",messageVo.getTKeyword1()));
+        data.put("keyword2",new JSONObject().put("value",messageVo.getTKeyword2()));
+        data.put("keyword3",new JSONObject().put("value",messageVo.getTKeyword3()));
+        data.put("keyword4",new JSONObject().put("value",messageVo.getTKeyword4()));
+        data.put("remark",new JSONObject().put("value",messageVo.getTRemark()));
+
+        content.put("touser",openId);
+        content.put("url",messageVo.getTUrl());
+        content.put("template_id",messageVo.getTemplateId());
+        content.put("data",data);
+        String resp = HttpUtil.post(requestUrl, JSONUtil.parseObj(content).toString());
+        System.out.println(content.toString());
+        System.out.println(JSONUtil.parseObj(content));
+        JSONObject result = JSONObject.parseObject(resp);
+        System.out.println("发送消息:" + resp);
+        return result;
+    }
+
 }
 
 

+ 68 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/domain/MceMbuser.java

@@ -0,0 +1,68 @@
+package com.usky.iot.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.time.LocalDateTime;
+import java.io.Serializable;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * <p>
+ * 消息中心_移动端注册表
+ * </p>
+ *
+ * @author han
+ * @since 2024-04-26
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+public class MceMbuser implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 电话号码
+     */
+    private String phone;
+
+    /**
+     * 微信用户id
+     */
+    private String openid;
+
+    /**
+     * 用户ID
+     */
+    private Long userId;
+
+    /**
+     * 应用ID
+     */
+    private String cids;
+
+    /**
+     * 创建人
+     */
+    private String createBy;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createTime;
+
+    /**
+     * 更新人
+     */
+    private String updateBy;
+
+    /**
+     * 更新时间
+     */
+    private LocalDateTime updateTime;
+
+
+}

+ 16 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/mapper/MceMbuserMapper.java

@@ -0,0 +1,16 @@
+package com.usky.iot.mapper;
+
+import com.usky.iot.domain.MceMbuser;
+import com.usky.common.mybatis.core.CrudMapper;
+
+/**
+ * <p>
+ * 消息中心_移动端注册表 Mapper 接口
+ * </p>
+ *
+ * @author han
+ * @since 2024-04-26
+ */
+public interface MceMbuserMapper extends CrudMapper<MceMbuser> {
+
+}

+ 16 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/MceMbuserService.java

@@ -0,0 +1,16 @@
+package com.usky.iot.service;
+
+import com.usky.iot.domain.MceMbuser;
+import com.usky.common.mybatis.core.CrudService;
+
+/**
+ * <p>
+ * 消息中心_移动端注册表 服务类
+ * </p>
+ *
+ * @author han
+ * @since 2024-04-26
+ */
+public interface MceMbuserService extends CrudService<MceMbuser> {
+
+}

+ 20 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/MceMbuserServiceImpl.java

@@ -0,0 +1,20 @@
+package com.usky.iot.service.impl;
+
+import com.usky.iot.domain.MceMbuser;
+import com.usky.iot.mapper.MceMbuserMapper;
+import com.usky.iot.service.MceMbuserService;
+import com.usky.common.mybatis.core.AbstractCrudService;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ * 消息中心_移动端注册表 服务实现类
+ * </p>
+ *
+ * @author han
+ * @since 2024-04-26
+ */
+@Service
+public class MceMbuserServiceImpl extends AbstractCrudService<MceMbuserMapper, MceMbuser> implements MceMbuserService {
+
+}

+ 10 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/SendWeChatMessageRequestVO.java

@@ -18,4 +18,14 @@ public class SendWeChatMessageRequestVO {
      * 消息内容
      */
     String infoContent;
+
+    /**
+     * 消息Id
+     */
+    Integer infoId;
+
+    /**
+     * openId
+     */
+    String openId;
 }

+ 48 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/TemplateMsgEntityVO.java

@@ -0,0 +1,48 @@
+package com.usky.iot.service.vo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+public class TemplateMsgEntityVO implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 标题
+     */
+    private String tTitle;
+    /**
+     * 第一行
+     */
+    private String tKeyword1;
+    /**
+     * 第二行
+     */
+    private String tKeyword2;
+    /**
+     * 第三行
+     */
+    private String tKeyword3;
+    /**
+     * 第四行
+     */
+    private String tKeyword4;
+    /**
+     * 备注
+     */
+    private String tRemark;
+    /**
+     * 跳转连接
+     */
+    private String tUrl;
+    /**
+     * 模板Id
+     */
+    private String templateId;
+    /**
+     * 状态
+     */
+    private int tStatus;
+
+}

+ 18 - 0
service-iot/service-iot-biz/src/main/resources/mapper/iot/MceMbuserMapper.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.usky.iot.mapper.MceMbuserMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.usky.iot.domain.MceMbuser">
+        <id column="id" property="id" />
+        <result column="phone" property="phone" />
+        <result column="openid" property="openid" />
+        <result column="user_id" property="userId" />
+        <result column="cids" property="cids" />
+        <result column="create_by" property="createBy" />
+        <result column="create_time" property="createTime" />
+        <result column="update_by" property="updateBy" />
+        <result column="update_time" property="updateTime" />
+    </resultMap>
+
+</mapper>