WeChat mini program uniapp+springboot implements mini program service notification
1. Achieve results
2. Template selection and field type judgment
2.1 Activate subscription messages and select a template
If you click Subscription Message to enable message subscription, the following page will appear. The template I used this time is
月卡到期提醒
a template. Just click to select it.
2.2 View template field types
TemplateId will be used later, copy it, click details to view template information
2.3 Query restrictions on corresponding field types
Subscription message parameter value content restriction description
Our fields are character_string.DATA, time and thing, we just need to follow their rules
Parameter category | Parameter Description | Parameter value restrictions | illustrate |
---|---|---|---|
thing.DATA | thing | Within 20 characters | Can be combined with Chinese characters, numbers, letters or symbols |
number.DATA | number | Number within 32 digits | Only numbers, decimals allowed |
letter.DATA | letter | Within 32 characters | Only letters |
symbol.DATA | symbol | Symbol within 5 digits | Can only sign |
character_string.DATA | string | Numbers, letters or symbols within 32 digits | Can be combined with numbers, letters or symbols |
time.DATA | time | 24-hour time format (supports + year, month and day), supports filling in time periods, and the two time points are connected with the "~" symbol | For example: 15:01, or: 15:01 on October 1, 2019 |
date.DATA | date | Year, month and day format (supports +24-hour time), supports filling in time periods, and the two time points are connected with the "~" symbol | For example: October 1, 2019, or: October 1, 2019 15:01 |
amount.DATA | Amount | 1 currency symbol + pure numbers within 10 digits, with decimals allowed, and "yuan" at the end | Decimals allowed |
phone_number.DATA | Telephone | Within 17 digits, numbers and symbols | Phone number, for example: +86-0766-66888866 |
car_number.DATA | license plate | Within 8 digits, the first and last digits can be Chinese characters, and the rest can be letters or numbers. | License plate number: Guangdong A8Z888 |
name.DATA | Name | Within 10 pure Chinese characters or within 20 pure letters or symbols | The Chinese name must be within 10 Chinese characters; the pure English name must be within 20 letters; a mixture of Chinese and letters will be counted as a Chinese name, within 10 characters |
phrase.DATA | Chinese character | Within 5 Chinese characters | Within 5 pure Chinese characters, for example: in delivery |
enum.DATA | enumeration value | Only field values within the enumeration value range can be uploaded | Call the interface to obtain the reference enumeration value |
3. vue front-end for authorization
This code only needs to be placed in a certain method, and then fill in the corresponding template_id.
wx.requestSubscribeMessage({
tmplIds: ['xxxx'],
success (res) {
console.log('接口调用成功的回调函数', res)
},
fail (err) {
console.log('接口调用失败的回调函数', err)
},
complete () {
console.log('接口调用结束的回调函数(调用成功、失败都会执行)')
}
})
Authorization effect
After clicking Allow, we can send the message to the user (open_id)onceSubscribe to news
4. The backend sends subscription messages to subscribed users
4.1 Interface
POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN
4.2 Request parameters:
Remember to check whether it is a mini program template or a public account template
4.3 Writing test code
The main thing is to encapsulate the template data and send it to the interface.
I won’t write the method of obtaining accessToken here.
To obtain it, just call this interface and replace APPID and APPSECRET with your own. The return value contains accessToken.
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
package com.admin;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.admin.domain.Card;
import com.admin.mapper.CardMapper;
import com.admin.util.WxUtils;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author zr
* @date 2023/6/20 15:23
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExpireNotifyTest {
@Autowired
private CardMapper cardMapper;
@Test
void name() {
Card card = cardMapper.selectById(10);
String today = DateUtil.formatDate(new Date());
Map map = new HashMap();
map.put("template_id","xxxx");
map.put("page","");//点击详细跳转小程序页面,不填就没有查看详情
map.put("touser","xxxx");//发送用户的open_id
//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
map.put("miniprogram_state","developer");
map.put("lang","zh_CN");
Map data = new HashMap();
Map character_string1 = new HashMap();
character_string1.put("value",card.getCardNo());//15487517
Map time2 = new HashMap();
time2.put("value",card.getDoorValidEnd());//2023-06-20
Map thing3 = new HashMap();
thing3.put("value",card.getDoorValidEnd().compareTo(today)>0?"":"月卡已过期,请尽快延期");//月卡已过期,请尽快延期
data.put("character_string1",character_string1);
data.put("time2",time2);
data.put("thing3",thing3);
map.put("data",data);
String accessToken = WxUtils.getXcxAccessToken();//获取小程序的accessToken
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
String sendUrl = url.replace("ACCESS_TOKEN", accessToken);
String post = HttpUtil.post(sendUrl, JSONUtil.toJsonStr(map));
System.out.println(post);
}
}
5. Test
Because in Chapter 3, our backend was allowed to directly call the test method once.