• java 微信公众号消息推送 微信发送消息


    WxMpProperties微信配置类:

    package com.person.modules.wx;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * 微信配置类
     *
     * @author zms
     * @date 2022-03-22
     **/
    @Component
    @Data
    @ConfigurationProperties(prefix = "wx.mp")
    public class WxMpProperties {
        /**
         * 多个公众号配置信息
         */
        private List<MpConfig> configs;
    
        @Data
        public static class MpConfig {
            /**
             * 设置微信公众号的appid
             */
            private String appId;
    
            /**
             * 设置微信公众号的app secret
             */
            private String secret;
    
            /**
             * 设置微信公众号的token
             */
            private String token;
    
            /**
             * 设置微信公众号的EncodingAESKey
             */
            private String aesKey;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    WeChatTemplateYml 微信消息模板配置类

    package com.person.modules.wx.demo;
    
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.io.Serializable;
    
    /**
     * 微信消息模板配置类
     *
     * @author zms
     * @date 2022-06-10
     **/
    @Component
    @Data
    @ConfigurationProperties(prefix = "information.template")
    public class WeChatTemplateYml implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @ApiModelProperty(value = "派车审核通知")
        private String review;
        @ApiModelProperty(value = "派车申请通知")
        private String apply;
        @ApiModelProperty(value = "派车消息提醒司机")
        private String driver;
        @ApiModelProperty(value = "考勤打卡提醒")
        private String attendance;
        @ApiModelProperty(value = "保险提醒")
        private String insurance;
        @ApiModelProperty(value = "车辆年检提醒")
        private String inspection;
        @ApiModelProperty(value = "车辆保养提醒")
        private String maintain;
        @ApiModelProperty(value = "驾驶证到期提醒")
        private String license;
        @ApiModelProperty(value = "域名")
        private String domain;
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    配置类

    wx:
      mp:
        configs:
          - appId: wxe15e8bf14b988f4c    # 第一个公众号的appid       测试环境
            secret: 5e3b8566bdaf1549cf3f3b4bc52d8cb7  # 公众号的appsecret
            token: jyrhpcxt     # 接口配置里的Token值
            aesKey: IOihv439BIS6Xhc8hIRFPcPnPtIOaDIY2guk4nX84rA    # 接口配置里的EncodingAESKey值
    information:
      template:
        review: HUz2gc3ovirQZudcfF5DxhqFcSzD76uUzp-EXn_ysrQ  #派车审核通知
        apply: UZ0kw8Qg0cSsHVXGw3lmbhcACVcYobT-CcWuvVvtn-I  #派车申请通知
        driver: siWN4-XRfjE-jQ59OuRfYyyJWS2vr5JaT-JQtzzPHW0  #派车消息提醒司机
        attendance: Pfmj4ZDRrF_upukDZ8Ej9R5fJ3M-LELHa2I5FvvgCEY  #考勤打卡提醒
        insurance: NUKs3FMZpbzjBPk3HkapOegPOZ9FUkx-uDv8zkkjYXA  #保险提醒
        inspection: oCy66gBaWKb_HtoxYTxg61X0ZU7yW0t2l5Nx11VsHkE  #车辆年检提醒
        maintain: sQgJ-zwY7m3_6EXSncIPXZVT07t76SUGrfAyd_gAU_s  #车辆保养提醒
        license: Ja3KmGyVMvZuORFpi8YnpIaS2KUtml1KdkSw0As47xQ  #驾驶证到期提醒
        domain: http://luttdrbb.dongtaiyuming.net/wechat/dist/index.html?carUrl=  #域名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    WxChatConstant 微信公众号相关操作的链接

    package com.person.modules.wx;
    
    public interface WxChatConstant {
    
        /**
         * 微信公众号相关操作的链接
         */
        interface Url {
    
            /**
             * 1、获取 accessToken 的请求地址
             * 参数1: APPID
             * 参数2: APPSECRET
             */
            String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    
            /**
             * 2、发送模版信息的url
             * 参数1: ACCESS_TOKEN
             */
            String SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    WxChatCache 微信 accessToken缓存

    package com.person.modules.wx;
    
    public class WxChatCache {
    
        /**
         * 微信 accessToken缓存
         */
        public static class AccessToken {
            public static String token = null;   // accessToken
            public static Long expiration = 0L;  // accessToken 过期时间(获取的token 默认有效期2小时)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    WeChetAccessToken 获取微信token工具类

    package com.person.modules.wx;
    
    import cn.hutool.log.Log;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.person.common.utils.Assert;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;
    
    import javax.annotation.Resource;
    
    /**
     * 获取微信token工具类
     */
    @Component
    public class WeChetAccessToken {
        private Logger logger = LoggerFactory.getLogger(getClass());
        /**
         * 微信配置
         */
        @Resource
        private WxMpProperties wxMpProperties;
        /**
         * RPC
         */
    
       @Resource
        private RestTemplate restTemplate;
    
    
        /**
         * 获取 accessToken (token有效期为2小时, 设置缓存, 过期并重新获取 accessToken)
         * 

    * 微信请求正确返回 {\"access_token\":\"37_B7bSN7N0VoqOVhf5rOk7NOHY6aMoxvE15VxNIcnD3f2kXvZkc0HOU-9rhfGZyWAoYkVfLrPzxTMhdcf86kgQeabfWSV-DH0hUYD8YMBF9vcbASzwRlEE3zJbKW2PuHJIl5Nu4BLouY4rUSFwTCBbAHAIRQ\",\"expires_in\":7200} * 微信请求错误返回 {"errcode":40013,"errmsg":"invalid appid"} *

    *

    * 流程: * 1、 判断token是否过期 * 2、 获取 url 并拼接参数 APPID + APPSECRET * 3、 发起请求 * 4、 判断请求是否成功 * 5、 缓存accessToken + accessToken过期时间到jvm 内存中 *

    * @author wangsong * @date 2019年6月19日 下午5:55:11 * @return */
    public String getToken() { if (WxChatCache.AccessToken.expiration <= System.currentTimeMillis()) { String url = WxChatConstant.Url.ACCESS_TOKEN_URL .replace("APPID", wxMpProperties.getConfigs().get(0).getAppId()) .replace("APPSECRET", wxMpProperties.getConfigs().get(0).getSecret()); //http ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class); JSONObject jsonObject = JSON.parseObject(forEntity.getBody()); Object errcode = jsonObject.get("errcode"); if (errcode != null && "40013".equals(errcode.toString())) { System.out.println("===============错误============"); // throw new ErrorException(RType.WX_GET_ACCESS_TOKEN_ERROR); } // 设置为1小时59分钟有效期,防止空指针异常 Object access_token = jsonObject.get("access_token"); Assert.checkNull(access_token,"=======空access_token"); WxChatCache.AccessToken.token = jsonObject.get("access_token").toString(); System.out.println("========token============================"); System.out.println("初始化Token======: "+jsonObject.get("access_token").toString()); WxChatCache.AccessToken.expiration = ((Integer.parseInt(jsonObject.get("expires_in").toString()) - 1) * 1000) + System.currentTimeMillis(); }else { logger.info("未过期Token==:"+WxChatCache.AccessToken.token); logger.info("expiration==:"+WxChatCache.AccessToken.expiration); } System.out.println("=AccessToken=:"+WxChatCache.AccessToken.token); return WxChatCache.AccessToken.token; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    WeChatTemplateMsg推送的微信公众信息的每一个 {{}} 值的内容和颜色封装

    package com.person.modules.wx;
    
    import lombok.Data;
    import lombok.ToString;
    
    /**
     * 推送的微信公众信息的每一个 {{}} 值的内容和颜色封装
     */
    @Data
    @ToString
    public class WeChatTemplateMsg {
        /**
         * 消息
         */
        private String value;
        /**
         * 消息颜色
         */
        private String color;
    
    
        public WeChatTemplateMsg(String value) {
            this.value = value;
            this.color = "#173177";
        }
    
        public WeChatTemplateMsg(String value, String color) {
            this.value = value;
            this.color = color;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    BaseWeChatServiceImpl微信通用service层

    package com.person.modules.wx;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * 微信通用service层
     */
    public class BaseWeChatServiceImpl {
    
        /**
         * rpc
         */
        @Autowired
        protected RestTemplate restTemplate;
    
    
        /**
         * 获取token
         */
    
        @Autowired
        protected WeChetAccessToken weChetAccessToken;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    测试

    WxChatController

    package com.person.modules.wx.demo;
    
    import com.person.common.utils.ResultData;
    import com.person.modules.apply.pojo.vo.CarApplyVo;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import springfox.documentation.annotations.ApiIgnore;
    
    import javax.annotation.Resource;
    
    /**
     * 微信公众号测试接口
     * @author wangsong
     * @mail 1720696548@qq.com
     * @date 2020/9/9 0009 14:41 
     * @version 1.0.0
     */
    @RestController
    @RequestMapping("/wechat/template/msg")
    @Api(value = "WxChatController", tags = "WeChat --> 微信模板消息推送")
    public class WxChatController {
    
        @Resource
        private WxChatService wxChatService;
    
        /**
         * 我的openId:  o8nrg503tfKwepDE4zKeP2g9PujU
         * @return
         */
        @RequestMapping(value = "/sendTest", method = RequestMethod.GET)
        @ApiOperation("推送测试信息")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "openId", value = "微信openId", required = true, paramType = "query", defaultValue = "oQ9Sn5payywZj5IpEaU1UIJvqlfU"),
                @ApiImplicitParam(name = "title", value = "标题", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "carUserName", value = "用车人", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "carUserPhone", value = "用车人电话", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "startingTime", value = "用车时间 yyyy-MM-dd HH:mm:SS", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "endTime", value = "返回时间 yyyy-MM-dd HH:mm:SS", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "passengersNumber", value = "乘车人数", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "startingPoint", value = "出发地", required = true, paramType = "query"),
                @ApiImplicitParam(name = "destination", value = "目的地", required = true, paramType = "query" ),
                @ApiImplicitParam(name = "type", value = "类型:审批VehicleApproval 取消VehicleRecords 司机收车ReturnTheCar 用车人VehicleRecords", required = true, paramType = "query" )
        })
        public ResultData<?> sendTest(@ApiIgnore String openId, @ApiIgnore CarApplyVo carApplyVo,@ApiIgnore String title,@ApiIgnore String type) {
            wxChatService.sendTest(openId, carApplyVo,title,type);
            return ResultData.result();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    openId: 要发送的openId
    carApplyVo: 这是我传入数据的对象
    title: 消息的头标题
    type: 这是我用于区分是那种消息,区分点击消息链接URl 的跳转地址

    WxChatService

    package com.person.modules.wx.demo;
    
    import com.person.modules.apply.pojo.vo.CarApplyVo;
    import com.person.modules.attendance.pojo.Vo.UserAttendanceVo;
    import com.person.modules.vehicle.pojo.form.CarVehicleRemind;
    
    /**
    *  公众号派车消息提醒
    * @author  zms
    * @date   2022/3/24
    **/
    public interface WxChatService {
        /**
         * 审批  VehicleApproval
         * 取消  VehicleRecords
         * 司机收车  ReturnTheCar
         *用车人 VehicleRecords
         *考勤打卡 Attendance
         */
        String TS_SP ="maytask/VehicleApproval";
        String TS_QX_YCR ="maytask/VehicleRecords";
        String TS_SC ="maytask/ReturnTheCar";
        String TS_PC="maytask/SendCar";
        String KQ_DK="maytask/Attendance";
        /**
         * 向一个用户推送消息(测试)
         * @param openId 微信id
         * @param type  类型
         * @param title  类型
         * @param vo  信息
         */
        void sendTest(String openId, CarApplyVo vo, String title, String type);
    
        /**
         * 用车人提示的司机信息
         * @param openId 微信id
         * @param type  类型
         * @param title  类型
         * @param vo  信息
         */
        void sendTestCar(String openId, CarApplyVo vo, String title, String type);
    
        /**
         * 派车审核通知
         * @param openId
         * @param vo
         * @param title
         * @param type
         */
        void sendApproval(String openId, CarApplyVo vo, String title, String type);
    
       /**
         * 考勤打卡提醒
         * @param openId 微信id
         * @param vo 类型
         * @param title 类型
         * @param type 类型
         */
        void attendancePunchReminder(String openId, UserAttendanceVo vo, String title, String type);
        /**
         * 保险提醒
         * @param openId 微信openId
         * @param vehicleRemind 类型
         * @param title 类型
         * @param type 类型
         */
        void insuranceReminder(String openId, CarVehicleRemind vehicleRemind, String title, String type);
    
        /**
         * 车辆年检提醒
         * @param openId 微信openId
         * @param vehicleRemind 提示消息
         * @param title 标题
         * @param type 类型
         */
        void checkReminder(String openId, CarVehicleRemind vehicleRemind, String title, String type);
    
        /**
         * 车辆保养提醒
         * @param openId 微信openId
         * @param vehicleRemind 提示消息
         * @param title 标题
         * @param type 类型
         */
        void maintainReminder(String openId, CarVehicleRemind vehicleRemind, String title, String type);
        /**
         * 驾驶证到期 提醒
         * @param openId 微信openId
         * @param vehicleRemind 提示消息
         * @param title 标题
         * @param type 类型
         */
        void licenseReminder(String openId, CarVehicleRemind vehicleRemind, String title, String type);
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    WxChatServiceImpl

    package com.person.modules.wx.demo;
    
    import com.person.common.utils.Assert;
    import com.person.common.utils.BaseException;
    import com.person.common.utils.DateUtilS;
    import com.person.modules.apply.pojo.vo.CarApplyVo;
    import com.person.modules.attendance.pojo.Vo.UserAttendanceVo;
    import com.person.modules.vehicle.pojo.form.CarVehicleRemind;
    import com.person.modules.wx.BaseWeChatServiceImpl;
    import com.person.modules.wx.WeChatTemplateMsg;
    import com.person.modules.wx.WxChatConstant;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.text.SimpleDateFormat;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
    *  向微信公众号推送消息
    * @author  zms
    * @date   2022/5/11
    **/
    @Service
    public class WxChatServiceImpl extends BaseWeChatServiceImpl implements WxChatService {
    
        @Resource
        private WeChatTemplateYml weChatTemplateYml;
    
        /**
         * 派车审核通知
         * HUz2gc3ovirQZudcfF5DxhqFcSzD76uUzp-EXn_ysrQ 测试
         * r76nyhEIet6Cd58ROWsHh6w-PCaDeIpbox3YOohnIiE
         * {{first.DATA}}
         * 用车人:{{keyword1.DATA}}
         * 用车时间:{{keyword2.DATA}}
         * 行车路线:{{keyword3.DATA}}
         * {{remark.DATA}}
         * @param openId
         * @param vo
         * @param title
         * @param type
         */
        @Override
        public void sendApproval(String openId, CarApplyVo vo, String title, String type) {
            // 模板Id
            String templateId = weChatTemplateYml.getReview();
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(5);
            sendMag.put("first", new WeChatTemplateMsg(title));
            sendMag.put("keyword1", new WeChatTemplateMsg(vo.getCarUserName()));
            sendMag.put("keyword2", new WeChatTemplateMsg(vo.getStartingTime().toString().replaceFirst("T", " ")+"—"+vo.getEndTime().toString().replaceFirst("T", " ")));
            sendMag.put("keyword3", new WeChatTemplateMsg(vo.getStartingPoint()+"—"+vo.getDestination()));
            sendMag.put("remark", new WeChatTemplateMsg("申请部门:"+vo.getDepartmentName()+"\n"+"用车人电话:"+vo.getCarUserPhone()+"\n"+"乘车人数:"+vo.getPassengersNumber()+"(人)"));
            // 发送
            this.send(openId, templateId, sendMag,type);
        }
        /**原来的ueFbjXIngyMaWUqPdDf4E1Vv7_BOVAaLKxpPk0mxFyY
         * UZ0kw8Qg0cSsHVXGw3lmbhcACVcYobT-CcWuvVvtn-I
         * 1、派车申请通知
         {{first.DATA}}
         申请人:{{keyword1.DATA}}
         用车时间:{{keyword2.DATA}}
         行车路线:{{keyword3.DATA}}
         车辆司机:{{keyword4.DATA}}
         用车事由:{{keyword5.DATA}}
         {{remark.DATA}}
         * @param openId 微信用户的openId
         * @param vo 发送的内容
         * @param title 小标题
         * @param type 类别
         */
        @Override
        public void sendTest(String openId, CarApplyVo vo, String title, String type) {
            // 模板Id 测试
            String templateId = weChatTemplateYml.getApply();
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(7);
            sendMag.put("first", new WeChatTemplateMsg(title));
            sendMag.put("keyword1", new WeChatTemplateMsg(vo.getCarUserName()));
            sendMag.put("keyword2", new WeChatTemplateMsg(vo.getStartingTime().toString().replaceFirst("T", " ")+"—"+vo.getEndTime().toString().replaceFirst("T", " ")));
            sendMag.put("keyword3", new WeChatTemplateMsg(vo.getStartingPoint()+"—"+vo.getDestination()));
            sendMag.put("keyword4", new WeChatTemplateMsg(vo.getDriverName()));
            sendMag.put("keyword5", new WeChatTemplateMsg(vo.getReason()));
            sendMag.put("remark", new WeChatTemplateMsg("乘车人数:"+vo.getPassengersNumber()+"(人)"+"\n"+
                    "车牌号:"+vo.getCarNumber()+"\n"+"司机电话:"+vo.getDriverPhone()));
            // 发送
            this.send(openId, templateId, sendMag,type);
        }
    
        /** 派车消息提醒司机
         * b_UFJAg0nGjWRoErBItzwXLYOrNPGOY3595Mk4fPj4o
         * siWN4-XRfjE-jQ59OuRfYyyJWS2vr5JaT-JQtzzPHW0
         * {{first.DATA}}
         * 申请部门:{{keyword1.DATA}}
         * 申请事由:{{keyword2.DATA}}
         * 用车时间:{{keyword3.DATA}}
         * 行程地址:{{keyword4.DATA}}
         * 客户信息:{{keyword5.DATA}}
         * @param openId 微信id
         * @param vo  信息
         * @param title  类型
         * @param type  类型
         */
     @Override
        public void sendTestCar(String openId, CarApplyVo vo, String title, String type) {
         // 模板Id ceshi
         String templateId = weChatTemplateYml.getDriver();
         // 模板参数
         Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(7);
         sendMag.put("first", new WeChatTemplateMsg(title));
         sendMag.put("keyword1", new WeChatTemplateMsg(vo.getDepartmentName()));
         sendMag.put("keyword2", new WeChatTemplateMsg(vo.getReason()));
         sendMag.put("keyword3", new WeChatTemplateMsg(vo.getStartingTime().toString().replaceFirst("T", " ")+"—"+vo.getEndTime().toString().replaceFirst("T", " ")));
         sendMag.put("keyword4", new WeChatTemplateMsg(vo.getStartingPoint()+"—"+vo.getDestination()));
         sendMag.put("keyword5", new WeChatTemplateMsg(vo.getCarUserName()));
         sendMag.put("remark", new WeChatTemplateMsg("用车人电话:"+vo.getCarUserPhone()+"\n"+"乘车人数:"+vo.getPassengersNumber()+"(人)"));
         // 发送
         this.send(openId, templateId, sendMag,type);
        }
    
       /**
         * 考勤打卡提醒
        原来模板id:z5aA7bYarYcYrn3k_DIVlsEle6N2chgL6CrvfomtMgU
        Pfmj4ZDRrF_upukDZ8Ej9R5fJ3M-LELHa2I5FvvgCEY
        {{first.DATA}}  考勤提醒
        姓名:{{keyword1.DATA}}
        时间:{{keyword2.DATA}} yyyy-MM-dd HH:mm:SS
        状态:{{keyword3.DATA}}  上班未打卡、下班未打卡
        {{remark.DATA}}
         * @param openId 微信id
         * @param vo  信息
         * @param title  类型 考勤提醒
         * @param type  类型 KQ_DK
         */
     @Override
        public void attendancePunchReminder(String openId, UserAttendanceVo vo, String title, String type) {
         // 模板Id 测试
         String templateId = weChatTemplateYml.getAttendance();
         // 模板参数
         Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(5);
         sendMag.put("first", new WeChatTemplateMsg(title));
         sendMag.put("keyword1", new WeChatTemplateMsg(vo.getName()));
         sendMag.put("keyword2", new WeChatTemplateMsg(new SimpleDateFormat("HH:mm:ss").format(vo.getDateTime())));
         sendMag.put("keyword3", new WeChatTemplateMsg(vo.getType()));
         sendMag.put("remark", new WeChatTemplateMsg(vo.getRemark()));
         // 发送
         this.send(openId, templateId, sendMag,type);
        }
    
        /**保险提醒
         * NUKs3FMZpbzjBPk3HkapOegPOZ9FUkx-uDv8zkkjYXA  测试
         * Kx-q4JQr5JVay09TlWD7Jw2Upmy6WDbUOPgo8nMYHHo  正式
         * {{first.DATA}}
         * 车型:{{keyword1.DATA}}
         * 保险到期:{{keyword2.DATA}}
         * {{remark.DATA}}
         * @param openId 微信openId
         * @param v 类型
         * @param title 类型
         * @param type 类型
         */
        @Override
        public void insuranceReminder(String openId, CarVehicleRemind v, String title, String type) {
            // 模板Id
            String templateId = weChatTemplateYml.getInsurance();
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(4);
            sendMag.put("first", new WeChatTemplateMsg(title));
            sendMag.put("keyword1", new WeChatTemplateMsg(v.getBrand()));
            sendMag.put("keyword2", new WeChatTemplateMsg(v.getNextInsuranceTime().toString()));
            sendMag.put("remark", new WeChatTemplateMsg("车牌号:"+v.getLicensePlateNumber()+"\n"+"如未办理,请及时办理,以免影响出行。"));
            // 发送
            this.send(openId, templateId, sendMag,type);
        }
    
        /**车辆年检提醒
         * oCy66gBaWKb_HtoxYTxg61X0ZU7yW0t2l5Nx11VsHkE  测试
         * j8OVibPZoHdhr2pNKTuPqYH9dnaUB9-f5p9n2FTwxp8  正式
         * {{first.DATA}}
         * 车牌号码:{{keyword1.DATA}}
         * 有效期止:{{keyword2.DATA}}
         * {{remark.DATA}}
         * @param openId 微信openId
         * @param v 提示消息
         * @param title 标题
         * @param type 类型
         */
        @Override
        public void checkReminder(String openId, CarVehicleRemind v, String title, String type) {
            // 模板Id
            String templateId =weChatTemplateYml.getInspection();
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(4);
            sendMag.put("first", new WeChatTemplateMsg(title));
            sendMag.put("keyword1", new WeChatTemplateMsg(v.getLicensePlateNumber()));
            sendMag.put("keyword2", new WeChatTemplateMsg(v.getNextInspectionTime().toString()));
            sendMag.put("remark", new WeChatTemplateMsg("车型:"+v.getBrand()+"\n"+"如未办理,请及时办理,以免影响出行。"));
            // 发送
            this.send(openId, templateId, sendMag,type);
        }
    
        /**车辆保养提醒
         * sQgJ-zwY7m3_6EXSncIPXZVT07t76SUGrfAyd_gAU_s  测试
         * 1kecOZKza2uyNN7j-wfWxY3GB0U04cUg6lz-BW35b68  正式
         * {{first.DATA}}
         * 车型:{{keyword1.DATA}}
         * 上次保养时间:{{keyword2.DATA}}
         * 上次保养里程:{{keyword3.DATA}}
         * {{remark.DATA}}
         * @param openId 微信openId
         * @param v 提示消息
         * @param title 标题
         * @param type 类型
         */
        @Override
        public void maintainReminder(String openId, CarVehicleRemind v, String title, String type) {
            // 模板Id
            String templateId = weChatTemplateYml.getMaintain();
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(5);
            sendMag.put("first", new WeChatTemplateMsg(title));
            sendMag.put("keyword1", new WeChatTemplateMsg(v.getBrand()));
            sendMag.put("keyword2", new WeChatTemplateMsg(DateUtilS.nowLLocalDate().plusDays(200).toString()));
            sendMag.put("keyword3", new WeChatTemplateMsg(v.getNowMileage().subtract(v.getFixedMileage()).toString()));
            sendMag.put("remark", new WeChatTemplateMsg("车牌号:"+v.getLicensePlateNumber()+"\n"+"如未办理,请及时办理,以免影响出行。"));
            // 发送
            this.send(openId, templateId, sendMag,type);
        }
        /**驾驶证到期提醒
         * 您的证件即将到期
         * Ja3KmGyVMvZuORFpi8YnpIaS2KUtml1KdkSw0As47xQ  测试
         * o1BwRJ_xEVL4BqxDr5rovLvAsXRJZPi6xIGQroWvLi0  正式
         * {{first.DATA}}
         * 证件类型:{{keyword1.DATA}}
         * 人员姓名:{{keyword2.DATA}}
         * 证件有效期至:{{keyword3.DATA}}
         * {{remark.DATA}}
         * @param openId 微信openId
         * @param v 提示消息
         * @param title 标题
         * @param type 类型
         */
        @Override
        public void licenseReminder(String openId, CarVehicleRemind v, String title, String type) {
            // 模板Id
            String templateId = weChatTemplateYml.getLicense();
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>(4);
            sendMag.put("first", new WeChatTemplateMsg(title));
            sendMag.put("keyword1", new WeChatTemplateMsg(v.getDrivingLicense()));
            sendMag.put("keyword2", new WeChatTemplateMsg(v.getDriverName()));
            sendMag.put("keyword3", new WeChatTemplateMsg(v.getNextTime().toString()));
            sendMag.put("remark", new WeChatTemplateMsg("司机电话:"+v.getPhone()+"\n"+"如未办理,请及时办理,以免影响出行。"));
            // 发送
            this.send(openId, templateId, sendMag,type);
        }
    
    
        /**
         * 发送模版消息工具类
         * openId     用户Id
         * templateId 模板Id   测试: "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY"
         * data       模板参数
         * @param data
         */
        private String send(String openId, String templateId, Map<String, WeChatTemplateMsg> data,String type) {
            System.out.println("================消息推送开始===================");
            //微信域名配置  测试
            //String  myUrl="http://15138597996.gnway.cc/wechat/dist/index.html?carUrl=";
            //生产
            //String  myUrl="http://497og49009.qicp.vip/wechat/dist/index.html?carUrl=";
            String  myUrl=weChatTemplateYml.getDomain();
            if (TS_SC.equals(type)){
                myUrl+=TS_SC;
            }else if(TS_SP.equals(type)){
                myUrl+=TS_SP;
            }else if(TS_QX_YCR.equals(type)){
                myUrl+=TS_QX_YCR;
            }else if(TS_PC.equals(type)){
                myUrl+=TS_PC;
            }else if(KQ_DK.equals(type)){
                myUrl+=KQ_DK;
            }else {
                myUrl=null;
            }
            Assert.checkNull(weChetAccessToken,"weChetAccessToken空");
            String accessToken = weChetAccessToken.getToken();
            String url = WxChatConstant.Url.SEND_URL.replace("ACCESS_TOKEN", accessToken);
            //拼接base参数
            Map<String, Object> sendBody = new HashMap<>(5);
            // openId
            sendBody.put("touser", openId);
            // 点击模板信息跳转地址
            sendBody.put("url", myUrl);
            // 顶色
            sendBody.put("topcolor", "#FF0000");
            // 模板参数
            sendBody.put("data", data);
            // 模板Id
            sendBody.put("template_id", templateId);
            ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
            System.out.println("===========消息已推送========");
            System.out.println("openId:"+openId);
            System.out.println("消息:"+data);
            System.out.println("url:"+myUrl);
            System.out.println("================消息推送結束===================");
            try {
                forEntity.getBody();
            } catch (Exception e) {
                e.printStackTrace();
                throw new BaseException("公众号消息推送失败,请查看网络!");
            }
            return forEntity.getBody();
    
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
  • 相关阅读:
    1.vue3脚手架在vscode下面建立
    通过JS生成蜂巢背景图(六边形背景图)
    10 创建型模式-原型模式
    Leetcode—206.反转链表【简单】
    学习ASP.NET Core Blazor编程系列二十五——登录(4)
    密码学系列0-总述
    阿里神作,拥有了这份java面试题,等于拥有了大厂敲门砖
    抛弃真实数据集?生成式AI“踢馆”人工智能下半场
    深入理解强化学习——强化学习的局限性与适用范围
    优秀的ui设计作品(合集)
  • 原文地址:https://blog.csdn.net/qq_41080067/article/details/126528273