行业知识
小程序发送模板消息给用户 —— 一次性模板实现“长期订阅”
大家好!我是我玩亚索我会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代码:
... //由点击事件调出订阅消息界面 ...
(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&app&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@Setterpublic 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;@Slf4jpublic 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() 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 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 = 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 getDiffDayHourMinuteMap(Date endDate, Date nowDate) { Map 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 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() = 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;@Slf4jpublic 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 params){ Connection.Response response = null; try { Connection connect = Jsoup.connect(url); connect.method(Connection.Method.GET); for(Map.Entry 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
请求参数:
| appid | secret |
|---|---|
| 小程序id | 小程序密钥 |
参数说明:
appid和secret两个的值,可以在微信小程序后台找到。
返回值:
| access_token | expires_in |
|---|---|
| token值 | 有效时间 |
(2)调用发送消息接口:
接口:
POST : https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=TOKEN
请求参数:
| accessToken | touser | template_id | page | data |
|---|---|---|---|---|
| token | 消息接收人 | 消息模板id | 点击消息详情跳转页面 | 消息内容 |
参数说明:
token: 通过步骤1获取
toUser:是微信用户的openId,在微信小程序登陆获取用户信息时可以拿到。
page:是点击消息进入小程序查看后跳转的页面。
data:是消息模板中的内容。需要注意消息模板中的内容都是以key和value的形式进行赋值并且展示的,拿下面的一个工作通知模板做例子:
我们可以看到该消息模板中的消息内容只有两个:温馨提示和时间,分别对应的key是thing6和time2。
切记,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_flag为true时调用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
上一篇:web服务器php怎么用?
下一篇:服务器生存回归怎么设置?