【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息

文章目录

  • SpringBoot 集成 wxJava 微信小程序:模板消息
    • 1、微信小程序后台配置模板消息
    • 2、发送模板消息
    • 3、前端测试模板消息
  • 微信公众号

SpringBoot 集成 wxJava 微信小程序:模板消息

1、微信小程序后台配置模板消息

订阅消息
【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第1张图片
选一个比较符合业务的模板
【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第2张图片
我这里随便选了一个用来测试
【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第3张图片

2、发送模板消息


    @ApiOperation("发送模板消息")
    @PostMapping("sendMessage")
    public AjaxResult sendMsg(WxMessageVo entity) {
        return wxMiniappService.sedMessage(entity);
    }
    /**
     * 发送模板消息
     *
     * @param entity
     * @return
     */
    AjaxResult sedMessage(WxMessageVo entity);
    @Override
    public AjaxResult sedMessage(WxMessageVo entity) {
        // 测试
        entity.setOpenId(SecurityUtils.getLoginUser().getUser().getOpenId());
        entity.setMsgDataList(Arrays.asList(
                new WxMaSubscribeMessage.MsgData("phrase2", "报修成功"),
                new WxMaSubscribeMessage.MsgData("thing3", "已经接收到了,正在处理"),
                new WxMaSubscribeMessage.MsgData("time1", new DateTime(DateUtil.now(), DatePattern.NORM_DATETIME_FORMAT).toString())));
        String appid = wxMaProperties.getConfigs().get(0).getAppid();
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        try {
            WxMaSubscribeMessage message = WxMaSubscribeMessage.builder()
                    .toUser(entity.getOpenId())
                    .templateId(TemplateIdConst.testTemplateId)
                    .data(entity.getMsgDataList())
                    .build();
            wxService.getMsgService().sendSubscribeMsg(message);
            return AjaxResult.success("发送成功");
        } catch (WxErrorException e) {
            log.error(e.toString());
            return AjaxResult.error(e.getError().getErrorMsg());
        }
    }
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 微信模板消息参数
 *
 * @author Tellsea
 * @date 2022/3/25
 */
@Data
@Accessors(chain = true)
public class WxMessageVo {

    private String openId;

    private List<WxMaSubscribeMessage.MsgData> msgDataList;
}

/**
 * 消息模板ID
 *
 * @author Tellsea
 * @date 2022/3/25
 */
public class TemplateIdConst {

    /**
     * 测试模板ID
     */
    public static final String testTemplateId = "模板消息ID";
}

3、前端测试模板消息

<template>
  <view style="padding: 15px;">
    <u-button @click="submit" type="primary">发送消息u-button>
  view>
template>

<script>
let that;
export default {
  name: "sedMessage",
  data() {
    return {
    }
  },
  onLoad() {
    that = this;
  },
  methods: {
    submit() {
      uni.requestSubscribeMessage({
        tmplIds: ['模板消息ID'],
        success (res) {
          that.$u.post('/au/wxMiniapp/sendMessage', {}).then(res => {
            that.$msg('发送成功');
          });
        }
      });
    }
  }
}
script>

<style scoped>

style>

【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第4张图片
【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第5张图片
【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第6张图片

微信公众号

【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息_第7张图片

你可能感兴趣的:(Spring,Boot,#,微信小程序,微信,spring,boot,微信小程序)