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

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

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

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

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 微信小程序:模板消息【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息

微信公众号


原文始发于微信公众号(花海里):【SpringBoot学习】41、SpringBoot 集成 wxJava 微信小程序:模板消息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69583.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!