小程序发送模板消息给用户 —— 一次性模板实现“长期订阅”

大家好!我是我玩亚索我会C,在写小程序时,我们会有向用户发送消息的需求,比如发送提醒通知,或者是一些重要通知来形成一个服务的闭环。这就要用到微信的发送消息功能了,下面我使用Java语言来详细演示下如何使用一次性订阅消息发送消息给用户。

1.微信发消息流程

在这里插入图片描述

因为微信算是比较正式的社交软件,主动向用户发信息这个动作很容易被一些商家作为推销的手段,为了避免用户被骚扰所以就由用户直接来决定是否要接受该消息,这样设计很不错!

2.订阅消息

2.1 消息模板

发送消息必须使用微信小程序官方提供的消息模板。消息模板可以在微信公众号平台获取(前提是你得注册过了),如下图:
在这里插入图片描述
选好模板后,点击操作栏的选用按钮即可。如下图:
在这里插入图片描述

在这里插入图片描述

2.1.1 消息内容说明:

在这里插入图片描述

(1)模板id: 模板的唯一标识,微信小程序端服务器端都需使用,来确定使用那个模板。
(2)详细内容: 这里就是消息的内容,上图所示中的 温馨提示 就是标题,{{ting6.DATA}}就是值。

剩余模板中的内容都不重要,在此不作赘述。

2.2 消息类型

(1)一次性订阅:用户自主订阅后,可不限时间地下发一条对应的服务消息;每次订阅只能发送一条消息。

(2)长期订阅:用户订阅一次后,可长期下发多条消息。长期订阅对资质的要求很高,目前长期性订阅消息仅向政务民生、医疗、交通、金融、教育等线下公共服务开放,后期将逐步支持到其他线下公共服务业务。本文选用的时一次性订阅。

2.3 消息发送

(1)方法:wx.requestSubscribeMessage(Object object)(微信官方文档入口)wx.requestSubscribeMessage()用在微信小程序端,用于调起订阅消息界面。如下图:
在这里插入图片描述

2.3.1 注意事项:

(1)触发方式: wx.requestSubscribeMessage(Object object)只能用于点击按钮触发。

(2)一次性消息: 在消息类型中提及到,一次性订阅消息在用户自主订阅后,只能发送一次消息。

3.前端端代码

3.1 前端代码

(1)前端Html代码:

  ...
  //由点击事件调出订阅消息界面
  <button bindtap="subScriptionMessage"></button>
  ...

(2) 前端JS代码:

...
//调出 模板消息界面
 subScriptionMessage(){
	 wx.requestSubscribeMessage({
	  	//模板id,可以写多个
		tmplIds:['sn12-c51f_1PCY52t0u5UZbqKaS7z4G7U1mV-4ZH']
		//成功回调
		success(res){
	        console.log(res);
	    },
	    //失败回调
	    fail(res){
	        console.log(res);
	    }
  	})
 } 
...

3.2 后端代码

(1)代码

import lombok.extern.slf4j.Slf4j;
  	//微信小程序id
    private final String appId = "xxx";
    //微信小程序密钥
    private final String appSecret = "xxx";
    //模板id
    private final String templateId = "xxx";
    //用户openid
    private final String openUserId = "xxxx";
    //消息内容-温馨提示
    private final String warmTip = "hello,tough gay!";
    //跳转页面地址
    private final String pageUrl = "pages/index/index";
    //获取token接口
    private final String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" + appSecret;
    //发送消息接口
    private final String sendWxUserMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
 
    public void sendMessage(){
        //获取token
        String accessTokenResult = HttpClientUtil.doGet(getTokenUrl);
        String accessToken = JSONObject.parseObject(accessTokenResult).getString("access_token");
        SendMsgToWxUserRequestDTO sendMsgToWxUserRequestDTO = new SendMsgToWxUserRequestDTO();
        sendMsgToWxUserRequestDTO.setAccessToken(accessToken);
        sendMsgToWxUserRequestDTO.setTouser(openUserId);
        sendMsgToWxUserRequestDTO.setTemplate_id(templateId);
        JSONObject key = new JSONObject();
        //温馨提醒
        JSONObject thing6OfValue = new JSONObject();
        thing6OfValue.put("value",warmTip);
        key.put("thing6",thing6OfValue);
        //当前时间
        JSONObject time2 = new JSONObject();
        time2.put("value", DateUtil.dateToString(new Date(),DateUtil.DATETIME_FORMATTER));
        key.put("time2",time2);
        sendMsgToWxUserRequestDTO.setData(key);
        sendMsgToWxUserRequestDTO.setPage(pageUrl);
        String sendMessageUrl = sendWxUserMsgUrl + accessToken;
        log.info("发送消息参数:{}",JSONObject.toJSONString(sendMsgToWxUserRequestDTO));
        String s = HttpClientUtil.doPost(sendMessageUrl, JSONObject.toJSONString(sendMsgToWxUserRequestDTO));
        log.info("发送消息参数,返回值:{}",s);
    }

SendMsgToWxUserRequestDTO 类:

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SendMsgToWxUserRequestDTO {
    /**
     * token
     */
    private String accessToken;
    /**
     * 接收方openId
     */
    private String touser;


    private String template_id;

    private String page;

    /**
     * 小程序模板数据
     */
    private Object data;

    private String miniprogram_state;
}

DateUtil类:

package com.hjd.task.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.regex.Pattern;

@Slf4j
public class DateUtil extends DateUtils {
    /**
     * 日期格式
     */
    public static final String DATE_FORMATTER = "yyyy-MM-dd";
    /**
     * 日期格式无分割
     */
    public static final String DATE_FORMATTER_NO_SEGMENTATION = "yyyyMMdd";

    public static final String DATETIME_FORMATTER_NO_SEGMENTATION = "yyyyMMddHHmmss";
    /**
     * 时间格式
     */
    public static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期小时
     */
    public static final String DATE_HOUR_FORMATTER = "yyyy-MM-dd HH:mm";

    /**
     * 小时分钟
     */
    public static final String DATE_HOUR_STR_FORMATTER = "HH点mm分";
    /**
     * 小时分钟
     */
    public static final String HOUR_MINUTE_FORMATTER = "HH:mm";

    public static final String DATE_FORMATTER_NO_HMS = "yyyyMMdd";
    /**
     * 日期月份
     */
    public static final String DATE_MONTH_FORMATER = "yyyy-MM";
    /**
     * 年
     */
    public static final String YEAR_FORMATTER = "yyyy";
    /**
     * 月
     */
    public static final String MONTH_FORMATTER = "MM";
    /**
     * 中国标准时间
     * edg: Thu Feb 02 00:00:00 WET 2012.
     */
    public static final String DATE_EEE_FORMATER = "EEE MMM dd HH:mm:ss zzz yyyy";

    /**
     * 日期带开始时间'T'格式
     */
    public static final String DATE_T_FORMATER = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * 转成中文日期
     */
    public static final String DATE_STR_FORMATER = "yyyy年MM月dd日";

    /**
     * 日期时间字符串格式
     */
    public static final String DATETIME_STR_FORMATTER = "yyyy年MM月dd日 HH时mm分";

    /**
     * 日期字符串格式
     */
    public static final String DATE_STR_FORMATTER = "yyyy年MM月dd日";

    /**
     * 月日字符串格式
     */
    public static final String MONTH_DATE_FORMATTER = "MM月dd日";

    /**
     * 月日字符串格式
     */
    public static final String MONTH_DATE_SHORT_FORMATTER = "M月d日";

    /**
     * 月日时间字符串格式
     */
    public static final String MONTH_DATE_TIME_FORMATTER = "M月d日HH:mm";

    /**
     * 年/月/日 字符串格式
     */
    public static final String YEAR_MONTH_DATE_FORMATTER = "yyyy/MM/dd";
    /**
     * 今天的最后时间
     */
    public static final String NOW_END_TIME = "yyyy-MM-dd 23:59:59";
    /**
     * 今天的开始时间
     */
    public static final String NOW_START_TIME = "yyyy-MM-dd 00:00:00";

    /**
     * 一天的毫秒数
     */
    public static final Long DAY_MILLISECONDS = 86400000L;



    /**
     * 时间进制
     */
    public static final Integer DAY_RADIX = 24;
    public static final Integer TIME_RADIX = 60;
    public static final Integer MILLISECONDS_RADIX = 1000;

    public static final Long TWO_HOURS = 7200000L;
    public static final Long ONE_DAY = 86400000L;

    public static final  long nd = 1000 * 24 * 60 * 60;
    public static final  long nh = 1000 * 60 * 60;
    public static final  long nm = 1000 * 60;


    /**
     * 获取两个日期相差的月数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 如果d2>d1返回 月数差 否则返回0
     */
    public static int getMonthDiff(Date startDate, Date endDate) {
        if (endDate == null || startDate == null) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(endDate);
        c2.setTime(startDate);
        if (c1.getTimeInMillis() < c2.getTimeInMillis()) {
            return 0;
        }
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 假设 d2 = 2015-8-16  d1 = 2011-9-30
        int yearInterval = year1 - year2;
        // 如果 d2的 月-日 小于 d1的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }


    /**
     * 比较两个时间相差的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Long getDaysDiff(LocalDate beginDate, LocalDate endDate) {
        return endDate.toEpochDay() - beginDate.toEpochDay();
    }

    /**
     * 比较两个时间相差的天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long getDaysDiff(Date startDate, Date endDate) {
        if(ObjectUtils.isEmpty(startDate) || ObjectUtils.isEmpty(endDate)){
            return null;
        }
        LocalDate beginTime = DateUtil.getDateToLocalDate(startDate);
        LocalDate endTime = DateUtil.getDateToLocalDate(endDate);
        return getDaysDiff(beginTime, endTime);
    }

    /**
     * 计算两个时间相差的秒数
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static long getSecondDiff(String startTime, String endTime){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long eTime = 0;
        long sTime = 0;
        try {
            eTime = df.parse(endTime).getTime();
            sTime = df.parse(startTime).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = (eTime - sTime) / 1000;
        return diff;
    }

    /**
     * 获取相差月份时的时间
     *
     * @param date  日期
     * @param month 距date 相差月份 正数加 负数减
     * @return
     */
    public static String getMonthByDate(Date date, int month, String formatter) {
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        return dateToString(getDifferInByDate(date, month), formatter);
    }

    /**
     * 获取相差月份时的时间
     *
     * @param date  日期
     * @param month 距date 相差月份 正数加 负数减
     * @return
     */
    public static Date getDifferInByDate(Date date, int month) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        Date m = c.getTime();
        return m;
    }


    /**
     * 获取localDate日期的开始时间 {@link java.time.LocalTime}
     * eg. 2018-06-11 13:31:31 return 2018-06-11 00:00:00
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getDateBeginTime(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取localDate日期的结束时间 {@link java.time.LocalTime}
     * eg. 2018-06-11 13:31:31 return 2018-06-11 23:59:59.999999999
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getDateEndTime(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MAX);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取localDate日期的结束时间 {@link LocalTime}
     * eg. 2018-06-11 13:31:31 return 2018-06-11 23:59:59.000
     *
     * @param localDate
     * @return
     * @author leigang
     */
    public static Date getEndTimeOfDay(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(23, 59, 59, 0));
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取dateTime 的开始时间
     * @return
     */
    public static Date getStartTimeDay(Date dateTime){
        try {
            String format = DateFormatUtils.format(dateTime, NOW_START_TIME);
            Date endTime = DateUtils.parseDate(format, DATETIME_FORMATTER);
            return endTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取dateTime 的结束时间
     * @return
     */
    public static Date getEndTimeDay(Date dateTime){
        try {
            String format = DateFormatUtils.format(dateTime, NOW_END_TIME);
            Date endTime = DateUtils.parseDate(format, DATETIME_FORMATTER);
            return endTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取localDate日期的中午12.00{{@link java.time.LocalTime}
     * eg 2019-03-28 14:32.36 retrun 2019-03-28 12:00:00
     *
     * @param localDate
     * @return
     * @author wugucheng
     */
    public static Date getDateNoon(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.NOON);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * date 转 LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * 获取localDate日期所在月的开始日期
     * eg. 2018-06-11 13:31:31 return 2018-06-01 00:00:00
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getMonthFirstDay(LocalDate localDate) {
        LocalDate firstDate = localDate.with(TemporalAdjusters.firstDayOfMonth());
        return getDateBeginTime(firstDate);
    }

    /**
     * 获取localDate日期所在月的结束日期
     * eg. 2018-06-11 13:31:31 return 2018-06-30 23:59:59.999999999
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getMonthLastDay(LocalDate localDate) {
        LocalDate firstDate = localDate.with(TemporalAdjusters.lastDayOfMonth());
        return getDateEndTime(firstDate);
    }

    /**
     * 获取localDate日期所在年的开始日期
     * eg. 2018-06-11 13:31:31 return 2018-06-01 00:00:00
     *
     * @param localDate
     * @return
     * @author leiGang
     */
    public static Date getYearFirstDay(LocalDate localDate) {
        LocalDate firstDate = localDate.with(TemporalAdjusters.firstDayOfYear());
        return getDateBeginTime(firstDate);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @param formatter
     * @return
     */
    public static String dateToString(Date date, String formatter) {
        if (date == null) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
        return simpleDateFormat.format(date);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @param formatter
     * @return
     */
    public static String dateToStringStr(Date date, String formatter) {
        if (date == null) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATE_STR_FORMATER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
        return simpleDateFormat.format(date);
    }

    /**
     * 毫秒值转日期
     *
     * @param time
     * @param formatter
     * @return
     */
    public static Date timeMillisToDate(long time, String formatter) {
        if (time == 0) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
            Date date = new Date();
            date.setTime(time);
            return date;
        } catch (Exception e) {
            log.error("毫秒值转日期错误!", e);
            return null;
        }

    }

    /**
     * 字符串转日期
     *
     * @param dateString
     * @param formatter
     * @return
     */
    public static Date stringToDate(String dateString, String formatter) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            log.error("字符串转日期错误!", e);
            return null;
        }
    }

    /**
     * CST字符串转日期
     *
     * @param dateString
     * @param formatter
     * @return
     */
    public static Date stringCSTToDate(String dateString, String formatter) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATE_EEE_FORMATER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter, Locale.US);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            log.error("字符串转日期错误!", e);
            return null;
        }
    }

    /**
     * java.util.Date --> java.time.LocalDate
     */
    public static LocalDate getDateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * 比较
     *
     * @param startTime endTime
     * @return
     * @Author wugucheng
     */
    public static Long compare(Date startTime, Date endTime) {
        return endTime.getTime() - startTime.getTime();
    }

    /**
     * 得到两日期之间的毫秒数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long subDate(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        return date1.getTime() - date2.getTime();
    }

    /**
     * 计算已用耗时小时
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long getTwoDateHour(Date date1, Date date2) {
        return getTwoDateTotalMin(date1, date2) / TIME_RADIX;
    }

    /**
     * 计算已用耗时分钟
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long getTwoDateMin(Date date1, Date date2) {
        return getTwoDateTotalMin(date1, date2) % TIME_RADIX;
    }

    /**
     * 获取两个时间之间的分钟数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getTwoDateTotalMin(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0L;
        }
        long timeMils = date1.getTime() - date2.getTime();
        return timeMils / (MILLISECONDS_RADIX * TIME_RADIX);
    }

    /**
     * 根据分钟获取小时和分钟
     *
     * @param min
     * @return
     */
    public static String getHourAndMin(Long min) {
        if (min == null || min <= 0) {
            return "0小时0分";
        }
        return min / TIME_RADIX + "小时" + min % TIME_RADIX + "分钟";
    }

    /**
     * 根据毫秒计算天数(向上取整)
     *
     * @param milliseconds
     * @return
     */
    public static Long getDaysByMillisecondsCeil(Long milliseconds) {
        if (milliseconds == null) {
            return null;
        }
        Long radix = (long) DAY_RADIX * (long) TIME_RADIX * (long) TIME_RADIX * (long) MILLISECONDS_RADIX;
        if (milliseconds % radix != 0) {
            return milliseconds / radix + 1;
        }
        return milliseconds / radix;
    }

    /**
     * 判断是否是同一年
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameYear(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        return isSameYear;
    }

    /**
     * 判断是否是同一月
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMonth(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameMonth = cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        return isSameMonth;
    }

    /**
     * 判断是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameDay = cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
        return isSameDay;
    }

    /**
     * 获取当前时间+1天(第二天凌晨)
     *
     * @param str num 天数
     * @return
     * @Author wugucheng
     */
    public static Date addMidDay(Date str, int num) {
        Calendar rightNow = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            rightNow.setTime(str);
            rightNow.add(Calendar.DATE, num);
            formatStr = rightNow.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 获取当前时间加小时
     *
     * @param str num 小时
     * @return
     * @Author wugucheng
     */
    public static Date addHour(Date str, int num) {
        Calendar rightNow = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            rightNow.setTime(str);
            rightNow.add(Calendar.HOUR, num);
            formatStr = rightNow.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 获取时间文本
     *
     * @param hour
     * @param minute
     * @return
     */
    public static String getTimeString(Integer hour, Integer minute) {
        if (hour == null && minute == null) {
            return null;
        }
        if (hour == null) {
            return minute + "分钟";
        }
        if (minute == null) {
            return hour + "小时";
        }
        return hour + "小时" + minute + "分钟";
    }

    /**
     * 获取下 num 月时间
     *
     * @param date
     * @param num
     * @return
     */
    public static Date addMidMonth(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, num);
            formatStr = calendar.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 日期转星期几
     *
     * @param date
     * @return -1表示无效
     */
    public static int dateToWeek(Date date) {
        if (date == null) {
            return -1;
        }
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0) {
            return -1;
        }
        return weekDays[w];
    }

    /**
     * 返回date的时分
     *
     * @param date
     * @return
     */
    public static String getDateHourAndMinus(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(date);
    }

    /**
     * 返回date的年月日
     *
     * @param date
     * @return
     */
    public static String getDateYearAndMonthAndDay(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    /**
     * 获取单独的日并将date->int
     *
     * @param date
     * @return
     */
    public static int getDayCount(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());
        int day = cal.get(Calendar.DATE);
        return day;
    }

    /**
     * 获取当月总天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当年总天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取当前时间-n月
     *
     * @param str num 月数
     * @return
     */
    public static Date reduceMonth(Date str, int num) {
        Calendar rightNow = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            rightNow.setTime(str);
            rightNow.add(Calendar.MONTH, -num);
            formatStr = rightNow.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 获取两个日期之间的所有日期 (不包含结束日期)
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<Date> getBetweenDates(Date startDate, Date endDate) {
        List<Date> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    /**
     * 获取两个日期之间的日期字符串
     *
     * @param startDate
     * @param endDate
     * @param dateFormatStr
     * @return
     */
    public static List<String> getBetweenDatesStr(String startDate, String endDate, String dateFormatStr) {
        List<String> days = new ArrayList<>();
        DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
        try {
            Date start = dateFormat.parse(startDate);
            Date end = dateFormat.parse(endDate);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * yyyy-mm格式获取相差num月格式
     *
     * @param month
     * @param num
     * @return
     */
    public static String getMonthAddNum(String month, int num) {
        Date date = DateUtil.stringToDate(month, DateUtil.DATE_MONTH_FORMATER);
        return DateUtil.dateToString(DateUtil.addMidMonth(date, num), DateUtil.DATE_MONTH_FORMATER);
    }

    /**
     * 获取当前时间当年最后一天
     *
     * @param date
     * @return Date
     */
    public static Date getLastDayOfYear(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, last);
        return cal.getTime();

    }

    /**
     * 获取传入时间是哪月
     *
     * @param date 日期
     * @return
     */
    public static int getDateOfMonth(Date date) {
        if (date == null) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        return c1.get(Calendar.MONTH) + 1;
    }

    /**
     * 事件+分钟数
     *
     * @param date 日期
     * @return
     */
    public static Date addMinuteToDate(Date date, int num) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_HOUR_FORMATTER);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.MINUTE, num);
        return stringToDate(sdf.format(c1.getTime()), DateUtil.DATE_HOUR_FORMATTER);
    }

    /**
     * 返回指定月份的最后一天的59分59秒
     */
    public static Date getLastMonthOfDate(Date date) {
        Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
        para.setTime(date);
        para.set(Calendar.DATE, para.getActualMaximum(Calendar.DAY_OF_MONTH));
        para.set(Calendar.HOUR_OF_DAY, 23);
        para.set(Calendar.MINUTE, 59);
        para.set(Calendar.SECOND, 59);
        return para.getTime();
    }

    /**
     * 获取本周的开始时间
     *
     * @return
     */
    public static Date getBeginTimeOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获取上月第一天
     */
    public static Date getLastMonthFirstDay() {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.stringToDate(myFormatter.format(cal.getTime()) + " 00:00:00", DateUtil.DATETIME_FORMATTER);
    }

    /**
     * 获取上月最后一天
     */
    public static Date getLastMonthEndDay() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 是分割日期格式
     *
     * @param date
     * @return
     */
    public static boolean isDivDate(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$");
        boolean matches = pattern.matcher(date).matches();
        if (!matches) {
            return false;
        }
        return true;
    }

    /**
     * 是无分割日期格式
     *
     * @param date
     * @return
     */
    public static boolean isNoDivDate(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^([0-9]\\d{3}((0[1-9]|1[012])(0[1-9]|1\\d|2[0-8])|(0[13456789]|1[012])(29|30)|(0[13578]|1[02])31)|(([2-9]\\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00))0229)$");
        boolean matches = pattern.matcher(date).matches();
        if (!matches) {
            return false;
        }
        return true;
    }

    /**
     * 是日期格式字符串
     *
     * @param date
     * @return
     */
    public static boolean isDateStr(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        return isDivDate(date) || isNoDivDate(date);
    }

    /**
     * 两个时间相差是否大于两小时
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean subTimeThanTwoHours(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime() >= TWO_HOURS;
    }

    /**
     * 两个时间相差是否大于一天
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean subTimeThanOneDay(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime() >= ONE_DAY;
    }

    /**
     * 当前时间是否在两个时间段之内(跨日期,小时)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isBetweenTime(LocalTime startTime, LocalTime endTime, LocalTime betweenTime) {
        return betweenTime.isAfter(startTime) || betweenTime.isBefore(endTime);
    }

    /**
     * date转 LocalTime
     *
     * @param date
     * @return
     */
    public static LocalTime dateToLocalTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalTime();
    }

    /**
     * 转有分割日期
     *
     * @param dateStr
     * @return
     */
    public static String toDivDateStr(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        if (isDivDate(dateStr)) {
            return dateStr;
        }
        if (isNoDivDate(dateStr)) {
            return StringUtils.substring(dateStr, 0, 4) + "-" + StringUtils.substring(dateStr, 4, 6) + "-" + StringUtils.substring(dateStr, 6, 8);
        }
        return null;
    }

    /**
     * 获取传入日期所在月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        return cal.getTime();
    }

    /**
     * 获取传入日期所在月的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        return cal.getTime();
    }

    /**
     * 根据时间往前往后推N分钟
     * @param time
     * @param number
     * @return
     */
    public static Date adjustTimeFromNumber(Date time, Integer number){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MINUTE, number);
        return calendar.getTime();
    }

    /**
     * 获取精确到秒的时间戳
     * @param date
     */
    public static String getSecondTimestamp(Date date) {
        if (null == date) {
            return "0";
        }
        return String.valueOf(date.getTime() / 1000);
    }

    /**
     * 计算两个时间相差的天 时 分
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDiffDayHourMinute(Date endDate, Date nowDate) {
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = Math.abs(diff / nd);
        // 计算差多少小时
        long hour = Math.abs(diff % nd / nh);
        // 计算差多少分钟
        long min = Math.abs(diff % nd % nh / nm);
        return day + "天" + hour + "小时" + min + "分钟";
    }
    /**
     * 计算两个时间相差的天 时 分
     * @param endDate
     * @param nowDate
     * @return 返回Map
     */
    public static Map<String,Long> getDiffDayHourMinuteMap(Date endDate, Date nowDate) {
        Map<String,Long> dhmMap = new HashMap();
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = Math.abs(diff / nd);
        // 计算差多少小时
        long hour = Math.abs(diff % nd / nh);
        // 计算差多少分钟
        long min = Math.abs(diff % nd % nh / nm);
        dhmMap.put("day",day);
        dhmMap.put("hour",hour);
        dhmMap.put("min",min);
        return dhmMap;
    }

    /**
     * 获取今天星期几
     * @param nowDate 今日的日期
     * @return
     */
    public static String getDayOfTheWeek(Date nowDate){
        String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(nowDate);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * endTime 在当前时间之前
     * @param nowTime
     * @param endTime
     * @return
     */
    public static Boolean isBeforeNowTime(Date nowTime,Date endTime){
        if(ObjectUtils.isEmpty(nowTime) || ObjectUtils.isEmpty(endTime)){
            return null;
        }
        if(nowTime.getTime() > endTime.getTime()){
            return true;
        }else if(nowTime.getTime() < endTime.getTime()){
            return false;
        }
        return null;
    }

    /**
     * long转date
     * @param dateLong 毫秒
     * @return
     */
    public Date longToDate(long dateLong){
        Date date = new Date(dateLong);
        return date;
    }

    /**
     * date转long
     * @param date
     * @return
     */
    public long dateToLong(Date date){
        long dateLong = date.getTime();
        return  dateLong;
    }

    /**
     * 获取当前日期的前一天
     * @return
     */
    public static String getCurrentDateBeforeDay(){
        Calendar cal =Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        String yesterday = new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
        return yesterday;
    }

    /**
     * 将秒转换为 天,小时,分钟
     * @param second
     * @return
     */
    public static String secondToDayHourMin(Long second){
        long days = second / (60 * 60 * 24);
        long hours = (second % (60 * 60 * 24)) / (60 * 60);
        long minutes = (second % (60 * 60)) / (60);
        long seconds = second % 60;
        if(seconds >= 30){
            minutes++;
        }
        return days + "," + hours + "," + minutes;
    }

    /**
     * 字符串日期转小时
     * @param date 字符串日期
     * @param format 日期格式
     * @return
     */
    public static Integer stringDateToHour(String date,String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        String hhToString = null;
        try {
            hhToString = sdf.format( simpleDateFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer hhToInt = Integer.valueOf(hhToString);
        return hhToInt;
    }

    /**
     * 获取一年的开始时间
     * @param date
     * @return
     */
    public static Date getBeginDayOfYear(String date) {
        LocalDate today;
        if (null==date){
            today = LocalDate.now();
        }else {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            today=LocalDate.parse(date, fmt);
        }
        today = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        LocalDateTime localDateTime = today.atTime(00, 00, 01);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Date dateTime = Date.from(zonedDateTime.toInstant());
        return dateTime;
    }

    /**
     * 获取一年的结束时间
     * @param date
     * @return
     */
    public static Date getEndDayOfYear(String date) {
        LocalDate today;
        if (null==date){
            today = LocalDate.now();
        }else {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            today=LocalDate.parse(date, fmt);
        }
        today = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        LocalDateTime localDateTime = today.atTime(23, 59, 59);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Date dateTime = Date.from(zonedDateTime.toInstant());
        return dateTime;
    }
}

HttpClientUtil类:

package com.hjd.task.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;

@Slf4j
public class HttpClientUtil {

    private static final HttpClient httpClient = new HttpClient();

    static {
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    }

    /**
     * JSoup Post请求
     * @param url 地址
     * @param params JSON参数
     * @return JSON字符串返回值
     */
    public static String doPostJSoup(String url, String params) {
        Connection.Response response = null;
        try {
            response = Jsoup.connect(url)
                    .header("content-type", "application/json;charset=utf-8")
                    .method(Connection.Method.POST)
                    .requestBody(params)
                    .ignoreContentType(true)
                    .execute();
            return response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JSoup Post请求 带有 token
     * @param url 地址
     * @param tokenName token名称
     * @param token token值
     * @param params 参数
     * @return JSON字符串返回值
     */
    public static String doPostJSoup(String url,String tokenName,String token,String params){
        Connection.Response response = null;
        try {
            Connection connect = Jsoup.connect(url);
            connect.method(Connection.Method.POST);
            if(StringUtils.isNotEmpty(tokenName) && StringUtils.isNotEmpty(token)){
                connect.header(tokenName,token);
            }
            response = connect.ignoreContentType(true)
                    .execute();
            return response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JSoup Get请求
     * @param url 地址
     * @param params 参数 类型Map
     * @return JSON字符串返回值
     */
    public static String doGetJSoup(String url, Map<String,String> params){
       Connection.Response response = null;
        try {
            Connection connect = Jsoup.connect(url);
            connect.method(Connection.Method.GET);
            for(Map.Entry<String,String> entry : params.entrySet()){
                connect.data(entry.getKey(),entry.getValue());
            }
            response = connect.ignoreContentType(true)
                    .execute();
            return response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 发送POST请求
     *
     * @param url
     * @param paramJson
     * @return
     */
    public static String doPost(String url, String paramJson) {
        log.info("开始发送POST请求;url:{};参数:", url, paramJson);
        PostMethod postMethod = new PostMethod(url);
        // 设置post请求超时时间
        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        postMethod.addRequestHeader("Content-Type", "application/json");
        String response = null;
        try {
            RequestEntity entity = new StringRequestEntity(paramJson, "application/json", "UTF-8");
            postMethod.setRequestEntity(entity);
            httpClient.executeMethod(postMethod);

            response = postMethod.getResponseBodyAsString();
            postMethod.releaseConnection();
        } catch (IOException e) {
            log.info("POST发出请求异常,异常原因:{}", e);
            e.printStackTrace();
        }
        log.info("发送POST请求结束,返回值为:{}", response);
        return response;
    }

    /**
     * 发送GET请求
     *
     * @param urlParam
     * @return
     */
    public static String doGet(String urlParam) {
        log.info("开始发送GET请求;url:{};", urlParam);
        GetMethod getMethod = new GetMethod(urlParam);
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        getMethod.addRequestHeader("Content-Type", "application/json");
        String response = null;
        try {
            httpClient.executeMethod(getMethod);
            response = getMethod.getResponseBodyAsString();
        } catch (IOException e) {
            log.info("GET发出请求异常,异常原因:{}", e);
            e.printStackTrace();
        }
        log.info("发送GET请求结束,返回值为:{}", response);
        return response;
    }
}

(2)发送消息效果:

在这里插入图片描述

3.2.1 步骤分解:

(1)调用获取token接口:

接口:

GET: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APP_ID&secret=APP_SECRET

请求参数:

appidsecret
小程序id小程序密钥

参数说明:

appidsecret两个的值,可以在微信小程序后台找到。在这里插入图片描述

返回值:

access_tokenexpires_in
token值有效时间

(2)调用发送消息接口:

接口:

POST : https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=TOKEN

请求参数:

accessTokentousertemplate_idpagedata
token消息接收人消息模板id点击消息详情跳转页面消息内容

参数说明:

token: 通过步骤1获取

toUser:是微信用户的openId,在微信小程序登陆获取用户信息时可以拿到。

page:是点击消息进入小程序查看后跳转的页面。

在这里插入图片描述
data:是消息模板中的内容。需要注意消息模板中的内容都是以keyvalue的形式进行赋值并且展示的,拿下面的一个工作通知模板做例子:
在这里插入图片描述
我们可以看到该消息模板中的消息内容只有两个:温馨提示时间,分别对应的keything6time2

切记,key一定要保持和模板中的一致!!!

3.3 可能遇到的问题

3.3.1 参数不合法:

错误内容:

{"errcode":47003,"errmsg":"argument invalid! data.thing6.value invalid rid: 63904a7c-774dbdca-33bce62d"}

原因:

(1)消息模板中的值为空。凡是在消息模板中展示的字段都是必填的。
(2)代码中的key和目标中的不对应。
(3)key对应的value内容过长。

4.实际应用

通过上述介绍,我想大家对微信小程序发送模板消息有两个大概的认识。
我们知道用户同意订阅消息后,服务器端只能发一条消息给用户,后面如果想再次发送,还需要去询问用户是否订阅。那么如果我们想持续的给用户发消息呢?我们该如何应对这样的场景呢?

4.1 “总是保持以上选择”

在询问用户是否同意接收消息时,下方有个总是保持以上选择,如果用户勾选了,并点击确定,那么后面如果再次询问用户是否同意接收消息时,就不会弹出订阅消息界面
在这里插入图片描述

我们该如何判断用户勾选了保持以上选择呢?

可以通过微信小程序的wx.getSetting({})方法。这个方法会在勾选了保持以上选择后,会打印出模板id。所以我们需要在订阅界面成功回调方法中,判断下setting中是否包含模板id。
在这里插入图片描述

4.2 实现思路

  • 在后台数据库中给微信用户添加一个字段:check_flag表示,表示是否勾选 总是保持以上选择true:勾选了false:未勾选
  • wx.requestSubscribeMessage()成功回调方法中调用wx.setting()中判断用户是否勾选了总是保持以上选择
  • 如果是,修改check_flag=true
  • 微信小程序中必点击的地方,添加调用wx.requestSubscribeMessage()方法。
  • 如果check_flagtrue时调用wx.requestSubscribeMessage()

然后就可以实现用户在登陆界面点击一次发送多条消息给用户。如果用户不想再次接收消息。可以在我小程序右上角三个点设置中修改即可。

4.3 代码:


//在用户必点的地方,调用订阅消息方法
clickSubscribeMessage(){
   let flag = wx.getStorageSync('checkFlag');
   console.log(false);
   //订阅消息
   if(flag == 1){
     let workTaskTemplateId = app.globalData.workTaskTemplateId;
     wx.requestSubscribeMessage({
       tmplIds: [workTaskTemplateId],
       success(res){
         console.log(res);
       },
       fail(res){
         console.log(res);
       }
     })
   }
 }
...
//订阅消息
  subScriptionMessage(){
    var that = this;
    //模板id
    let workTaskTemplateId = 'xxx';
    wx.requestSubscribeMessage({
      tmplIds: [workTaskTemplateId],
      success (res) { 
        if(JSON.stringify(res).indexOf("reject") != -1){
          console.log("拒绝")
        } 
        if(JSON.stringify(res).indexOf("accept") != -1){
          console.log("接受")
          wx.getSetting({
            withSubscriptions: true,
            success (res) {
              console.log(res.subscriptionsSetting)
              if(JSON.stringify(res.subscriptionsSetting).indexOf(workTaskTemplateId) != -1){
                console.log("用户选择了“保持以上选择”")
                that.checkFlag();
              }
            }
          })
        } 
      },
      fail(res){
        console.log(res);
      }
    })
  },
 ...
 //请求后台接口,修改check_flag 
 checkFlag(){
 	//请求后台修改check_flag 字段为true
    wx.request({
      url: 'xxxx',
      ...
      success:function(res){
        //如果修改成功将check_status字段放到storage中
        wx.setStorageSync("checkFlag",true)
      },
      fail:function(res){},
    })
 }

如果大家有更好的方法请留言。上面截图例子来自于我做的一个微信小程序TaskPlan。链接如下,欢迎来参观:

☞ ☞ ☞ 人总会改变的,那为什么不往好处变呢?—— TaskPlan

评论 48
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是江迪呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值