提示:以下是本篇文章正文内容,下面案例可供参考
一、模板消息是什么?
模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。
二、整合步骤:
1.微信小程序后台配置模板消息
订阅消息:
根据实际业务选择你需要的模板,还可以搜索哦。
比如我选择这个商品过期提醒:
点进去
选择模板上的信息,然后提交。
查看自己的模板
2. 发送模板消息
后端逻辑代码
controller
import com.example.wxjava.common.result.R;
import com.example.wxjava.domain.vo.wx.WxKefuMessageBackVo;
import com.example.wxjava.domain.vo.wx.WxKefuMessageVo;
import com.example.wxjava.service.MessageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 成大事
* @since 2022/7/27 23:11
*/
@Slf4j
@RestController
@RequestMapping("/wx/msg")
@AllArgsConstructor
public class WxMessageController {
private final MessageService messageService;
@PostMapping("/sendMessage")
public R sendMsg() {
return messageService.sendMessage();
}
}
service
import com.example.wxjava.common.result.R;
import com.example.wxjava.domain.vo.wx.WxKefuMessageBackVo;
import com.example.wxjava.domain.vo.wx.WxKefuMessageVo;
/**
* @author 成大事
* @since 2022/7/27 23:11
*/
public interface MessageService {
/**
* 发送模板消息
* @return
*/
R sendMessage();
}
impl
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.wxjava.common.result.R;
import com.example.wxjava.domain.vo.wx.WxKefuMessageBackVo;
import com.example.wxjava.domain.vo.wx.WxKefuMessageVo;
import com.example.wxjava.service.MessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
/**
* @author 成大事
* @since 2022/7/27 23:11
*/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class MessageServiceImpl implements MessageService {
private final WxMaService wxMaService;
@Override
public R sendMessage() {
String userId = StpUtil.getLoginIdAsString();
// 测试
List<WxMaSubscribeMessage.MsgData> msgData = Arrays.asList(
new WxMaSubscribeMessage.MsgData("thing1", "嘿嘿"),
new WxMaSubscribeMessage.MsgData("date3", new DateTime(DateUtil.now(), DatePattern.NORM_DATETIME_FORMAT).toString())
);
try {
WxMaSubscribeMessage message = WxMaSubscribeMessage.builder()
// 要给谁发送
.toUser(userId)
// 模板id
.templateId("7oklMCAUD0zNAoTWikBOPSwVH2-XKC2-BJVqsUYGxgg")
// 数据
.data(msgData)
.build();
wxMaService.getMsgService().sendSubscribeMsg(message);
return R.ok("发送成功");
} catch (WxErrorException e) {
log.error(e.toString());
return R.error(e.getError().getErrorMsg());
}
}
前端逻辑代码
<template>
<view style="padding: 15px;">
<button @click="submit" type="primary">发送消息</button>
</view>
</template>
<script>
let that;
export default {
name: "sedMessage",
data() {
return {
}
},
onLoad() {
that = this;
},
methods: {
submit() {
uni.requestSubscribeMessage({
tmplIds: ['7oklMCAUD0zNAoTWikBOPSwVH2-XKC2-BJVqsUYGxgg'],
success(res) {
console.log("res",res)
uni.request({
url: 'http://localhost:8888/wx/msg/sendMessage',
method: 'POST',
success(resc) {
console.log("resc",resc)
}
})
}
});
}
},
}
</script>
<style scoped>
</style>
测试:
在微信开发者工具
可以看到消息发送了
结束