消息推送之微信模板消息

微服务中消息推送模块,一般消息推送分为微信模板消息、阿里云短信、APP消息推送,APP消息推送选择个推,本文主要介绍微信消息模板

  • 引入maven(具体版本具体引入)
    <dependency>
        <groupId>com.github.binarywang</groupId>
        <artifactId>weixin-java-mp</artifactId>
        <version>3.3.0</version>
    </dependency>
  • yml配置文件配置微信账号基本信息
#微信参数设置
wx:
  msg:
    #参数配置
    appid: wx1cXXXXad983
    secret: 4269XXXX9e3df3c6af03833
    AESKey: JykXXXX4+=@
  • 创建模板实体类
    WxTemplate.java
/**
 * 实体类
 *
 * @author myn
 * @since 2021-10-12
 */
@Data
@TableName("message_wx_template")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "WxTemplate对象", description = "WxTemplate对象")
public class WxTemplate extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
    * 主键
    */
        @ApiModelProperty(value = "主键")
        private Long id;
    /**
    * 模板id
    */
        @ApiModelProperty(value = "模板id")
        private String tid;
    /**
    * 关键字列表,最多支持5个,最少2个
    */
        @ApiModelProperty(value = "关键字列表,最多支持5个,最少2个")
        private String kidList;
    /**
    * 服务场景描述,15个字以内
    */
        @ApiModelProperty(value = "服务场景描述,15个字以内")
        private String sceneDesc;
    /**
    * 审核结果。0--未通过,1--通过
    */
        @ApiModelProperty(value = "审核结果。0--未通过,1--通过")
        private String reviewStatus;
    /**
    * 生成的模板id
    */
        @ApiModelProperty(value = "生成的模板id")
        private String templateId;
    /**
    * 模板类型。0--一次性订阅,1--永久性推送
    */
        @ApiModelProperty(value = "模板类型。0--一次性订阅,1--永久性推送")
        private String type;
    /**
    * 模板标题
    */
        @ApiModelProperty(value = "模板标题")
        private String title;
    /**
    * 模板类目id
    */
        @ApiModelProperty(value = "模板类目id")
        private Integer categoryId;
    /**
    * 模板类目名称
    */
        @ApiModelProperty(value = "模板类目名称")
        private String categoryName;
    /**
    * 租户标识
    */
        @ApiModelProperty(value = "租户标识")
        private String tenant;
    /**
    * 排序
    */
        @ApiModelProperty(value = "排序")
        private Integer sort;


}

WxTemplateData.java

/**
 * 实体类
 *
 * @author myn
 * @since 2021-10-13
 */
@Data
@TableName("message_wx_template_data")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "WxTemplateData对象", description = "WxTemplateData对象")
public class WxTemplateData extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
    * id
    */
        @ApiModelProperty(value = "id")
        private Long id;
    /**
    * 生成的模板id,如TbZwosLXXXXXXfTP27E
    */
        @ApiModelProperty(value = "生成的模板id,如TbZwosLXXXXXXfTP27E")
        private String templateId;
    /**
    * 模板id,如“2269”
    */
        @ApiModelProperty(value = "模板id,如“2269”")
        private String tid;
    /**
     * 用户ID
     */
    @ApiModelProperty(value = "用户ID")
    private String openId;
    /**
     * 跳转页面
     */
    @ApiModelProperty(value = "跳转页面")
    private String page;
    /**
     * 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
     */
    @ApiModelProperty(value = "跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版")
    private String miniprogramState;
    /**
     * 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
     返回值
     */
    @ApiModelProperty(value = "进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN 返回值")
    private String lang;
    /**
    * 关键字id,如 1
    */
        @ApiModelProperty(value = "关键字id,如 1 ")
        private String kid1;
    /**
    * 关键字名称,如“商品名称”
    */
        @ApiModelProperty(value = "关键字名称,如“商品名称”")
        private String kidName1;
    /**
    * 关键字的key。如”thing1“,微信后台获取
    */
        @ApiModelProperty(value = "关键字的key。如”thing1“,微信后台获取")
        private String kidDataKey1;
    /**
     * 关键字发送的内容
     */
    @ApiModelProperty(value = "关键字发送的内容")
    private String kidData1;
    /**
    * 关键字id,如 1
    */
        @ApiModelProperty(value = "关键字id,如 1 ")
        private String kid2;
    /**
    * 关键字名称,如“商品名称”
    */
        @ApiModelProperty(value = "关键字名称,如“商品名称”")
        private String kidName2;
    /**
    * 关键字的key。如”thing1“,微信后台获取
    */
        @ApiModelProperty(value = "关键字的key。如”thing1“,微信后台获取")
        private String kidDataKey2;
    /**
     * 关键字发送的内容
     */
    @ApiModelProperty(value = "关键字发送的内容")
    private String kidData2;
    /**
    * 关键字id,如 1
    */
        @ApiModelProperty(value = "关键字id,如 1 ")
        private String kid3;
    /**
    * 关键字名称,如“商品名称”
    */
        @ApiModelProperty(value = "关键字名称,如“商品名称”")
        private String kidName3;
    /**
    * 关键字的key。如”thing1“,微信后台获取
    */
        @ApiModelProperty(value = "关键字的key。如”thing1“,微信后台获取")
        private String kidDataKey3;
    /**
     * 关键字发送的内容
     */
    @ApiModelProperty(value = "关键字发送的内容")
    private String kidData3;
    /**
    * 关键字id,如 1
    */
        @ApiModelProperty(value = "关键字id,如 1 ")
        private String kid4;
    /**
    * 关键字名称,如“商品名称”
    */
        @ApiModelProperty(value = "关键字名称,如“商品名称”")
        private String kidName4;
    /**
    * 关键字的key。如”thing1“,微信后台获取
    */
        @ApiModelProperty(value = "关键字的key。如”thing1“,微信后台获取")
        private String kidDataKey4;
    /**
     * 关键字发送的内容
     */
    @ApiModelProperty(value = "关键字发送的内容")
    private String kidData4;
    /**
    * 关键字id,如 1
    */
        @ApiModelProperty(value = "关键字id,如 1 ")
        private String kid5;
    /**
    * 关键字名称,如“商品名称”
    */
        @ApiModelProperty(value = "关键字名称,如“商品名称”")
        private String kidName5;
    /**
    * 关键字的key。如”thing1“,微信后台获取
    */
        @ApiModelProperty(value = "关键字的key。如”thing1“,微信后台获取")
        private String kidDataKey5;
    /**
     * 关键字发送的内容
     */
    @ApiModelProperty(value = "关键字发送的内容")
    private String kidData5;
    /**
     * 租户标识
     */
    @ApiModelProperty(value = "租户标识")
    private String tenant;


}
  • service实现类,主要实现微信模板的增删改查
/**
 * 服务实现类
 *
 * @author myn
 * @since 2021-10-12
 */
@Service
public class WxTemplateServiceImpl extends BaseServiceImpl<WxTemplateMapper, WxTemplate> implements IWxTemplateService {
    
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;

    @Override
    public IPage<WxTemplateVO> selectWxTemplatePage(IPage<WxTemplateVO> page, WxTemplateVO wxTemplate) {
        return page.setRecords(baseMapper.selectWxTemplatePage(page, wxTemplate));
    }

    @Override
    public JSONObject addWxTemplate(WxTemplate wxTemplate) {

        String accessToken = GetAccessToken.getAccessToken();
        String tid = wxTemplate.getTid();
        char[] kidList = wxTemplate.getKidList().toCharArray();
        String sceneDesc = wxTemplate.getSceneDesc();

        String url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token="
                + accessToken;
        wxTemplate.setReviewStatus("0");
        Map<String, String> map = new HashMap<>();
        map.put("tid", tid);
        map.put("kidList", Arrays.toString(kidList));
        map.put("sceneDesc", sceneDesc);
        //租户标识
        wxTemplate.setTenant("");
        JSONObject object = null;
        R<Object> resp = R.status(this.save(wxTemplate));
        if (resp.getCode() == 200) {
            String result = HttpUtil.sendPost(url, map);
            object = JSON.parseObject(result);
            if (object.get("errcode") == "0") {
                String tempid = object.get("priTmplId").toString();
                wxTemplate.setTemplateId(tempid);
                wxTemplate.setReviewStatus("1");
                R.status(this.updateById(wxTemplate));
            } else {
                return object;
            }
        } else {
            return JSONObject.parseObject(String.valueOf(resp));
        }
        return object;
    }

    @Override
    public JSONObject delWxTemplate(String id, String templateId) {
        String accessToken = GetAccessToken.getAccessToken();
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);

        String url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token="
                + accessToken;
        JSONObject object = null;
        Map<String, String> map = new HashMap<>();
        map.put("priTmplId", templateId);
        try {
            R<Object> resp = R.status(this.deleteLogic(Func.toLongList(id)));
            if (resp.getCode() == 200) {
                String result = HttpUtil.sendPost(url, map);
                object = JSON.parseObject(result);
                if (object.get("errcode") == "0") {
                    platformTransactionManager.commit(transaction);
                    return object;
                } else {
                    return object;
                }
            } else {
                return JSONObject.parseObject(String.valueOf(resp));
            }
        } catch (Exception e) {
            platformTransactionManager.rollback(transaction);
            e.printStackTrace();
        }
        assert object != null;
        return object;
    }

    @Override
    public JSONObject getCategory() {
        String accessToken = GetAccessToken.getAccessToken();

        String url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token="
                + accessToken;
        String result = HttpUtil.sendGet(url);
        return JSON.parseObject(result);
    }

    @Override
    public JSONObject getPubTemplateKeyWordsById(String tid) {
        String accessToken = GetAccessToken.getAccessToken();

        String url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token="
                + accessToken
                + "&tid="
                + tid;
        String result = HttpUtil.sendGet(url);
        return JSON.parseObject(result);
    }

    @Override
    public JSONObject getPubTemplateTitleList(String ids, int start, int limit) {
        String accessToken = GetAccessToken.getAccessToken();

        String url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token="
                + accessToken
                + "&ids="
                + ids
                + "&start="
                + start
                + "&limit="
                + limit;
        String result = HttpUtil.sendGet(url);
        return JSON.parseObject(result);
    }

    @Override
    public JSONObject getTemplateList() {
        String accessToken = GetAccessToken.getAccessToken();

        String url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token="
                + accessToken;
        String result = HttpUtil.sendGet(url);
        return JSON.parseObject(result);
    }

    @Override
    public Map<String,String> getTemplateId(){
        JSONArray js = (JSONArray) getTemplateList().get("data");
        Map<String,String> map = new HashMap<>();
        for (int i=0;i<js.size();i++){
            map.put(js.getJSONObject(i).get("priTmplId").toString(),js.getJSONObject(i).get("title").toString());
        }
        return map;
    }
}

微信消息的服务实现类

/**
 * 服务实现类
 *
 * @author myn
 * @since 2021-10-13
 */
@Service
public class WxTemplateDataServiceImpl extends BaseServiceImpl<WxTemplateDataMapper, WxTemplateData> implements IWxTemplateDataService {

    @Autowired
    private IWxAcceptsingleService wxAcceptsingleService;

    private WxMaMsgService msgService;


    public WxTemplateDataServiceImpl() {
    }

    Log log = LogFactory.get();

    @Override
    public IPage<WxTemplateDataVO> selectWxTemplateDataPage(IPage<WxTemplateDataVO> page, WxTemplateDataVO wxTemplateData) {
        return page.setRecords(baseMapper.selectWxTemplateDataPage(page, wxTemplateData));
    }

    /**
     * 获取需要填写的关键字列表
     *
     * @param templateId
     * @return 关键字的键值对
     * @author huan
     */
    @Override
    public Map<String, String> getDataList(String templateId) {
        WxTemplateData wxTemplateData = baseMapper.selectWxTemplateDataParam(templateId);
        Map<String, String> map = new HashMap<>();
        map.put(wxTemplateData.getKidName1(), wxTemplateData.getKidDataKey1());
        map.put(wxTemplateData.getKidName2(), wxTemplateData.getKidDataKey2());
        map.put(wxTemplateData.getKidName3(), wxTemplateData.getKidDataKey3());
        map.put(wxTemplateData.getKidName4(), wxTemplateData.getKidDataKey4());
        map.put(wxTemplateData.getKidName5(), wxTemplateData.getKidDataKey5());
        //去掉空的KEY和Value,返回有数值的map
        MapRemoveNullUtil.removeNullEntry(map);
        System.out.println(map);
        return map;
    }

    /**
     * 发送消息
     *
     * @param wxTemplateData
     * @return 返回信息的String
     * @throws Exception
     * @author huan
     */
    @Override
    public String sendMsg(WxTemplateData wxTemplateData) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put(wxTemplateData.getKidDataKey1(), wxTemplateData.getKidData1());
        map.put(wxTemplateData.getKidDataKey2(), wxTemplateData.getKidData2());
        map.put(wxTemplateData.getKidDataKey3(), wxTemplateData.getKidData3());
        map.put(wxTemplateData.getKidDataKey4(), wxTemplateData.getKidData4());
        map.put(wxTemplateData.getKidDataKey5(), wxTemplateData.getKidData5());
        //去掉空的键值对
        MapRemoveNullUtil.removeNullEntry(map);
        List<Map<String, String>> data = new ArrayList<>();
        data.add(map);
        //根据,分割传进来的多个openid
        String[] templateId = wxTemplateData.getTemplateId().split(",");
        net.sf.json.JSONObject jsonResult = new JSONObject();
        //foreach发送
        for (String id : templateId) {
            Template template = new Template();
            template.setTemplate_id(id);
            template.setTouser(wxTemplateData.getOpenId());
            template.setPage(wxTemplateData.getPage());
            List<TemplateParam> paras = new ArrayList<>(data.size());
            //转换成{things:{value:"things"}}形式
            for (Map<String, String> m : data) {
                for (String k : m.keySet()) {
                    paras.add(new TemplateParam(k, m.get(k)));
                }
            }
            template.setTemplateParamList(paras);
            //拼接链接
            String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
            requestUrl = requestUrl.replace("ACCESS_TOKEN", GetAccessToken.getAccessToken());
            log.info("链接地址:{}", template.toJSON());
            //获取返回信息
            jsonResult = CommonUtil.httpsRequest(requestUrl, "POST", template.toJSON());
            log.info("返回消息:{}", jsonResult);
            int errorCode = jsonResult.getInt("errcode");
            String errorMessage = jsonResult.getString("errmsg");
            if (errorCode == 0) {
                //发送成功
                log.info("给{}发送的消息成功,返回消息:{}", id, jsonResult.toString());
                //记录存库
                WxAcceptsingle wxAcceptsingle = new WxAcceptsingle();
                wxAcceptsingle.setData(data.toString());
                wxAcceptsingle.setLang(WxMaConstants.MiniprogramLang.ZH_CN);
                wxAcceptsingle.setMiniprogramState(WxMaConstants.MiniprogramState.FORMAL);
                wxAcceptsingle.setAccessToken(GetAccessToken.getAccessToken());
                wxAcceptsingle.setPage(wxTemplateData.getPage());
                wxAcceptsingle.setSendStatus("1");
                wxAcceptsingle.setTouser(wxTemplateData.getOpenId());
                wxAcceptsingle.setTenant(AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId()));
                R.status(this.wxAcceptsingleService.save(wxAcceptsingle));
            } else {
                //发送失败,不进行终止,日志记录后继续发送下一个
                log.warn("订阅消息发送失败" + errorCode + "," + errorMessage);
            }
        }
        return jsonResult.toString();
    }

    /**
     * 利用租户字段进行增删改查测试
     * 查询测试
     *
     * @return
     */
    @Override
    public List<WxTemplateData> selectList() {
        QueryWrapper<WxTemplateData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant", AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId()));
        String a = AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId());
        System.out.println(a);

        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 利用租户字段进行增删改查测试
     * 删除测试
     * @author huan
     * @return 1为删除成功,0为删除失败
     */
    @Override
    public int deleteByEntity(String id) {
        QueryWrapper<WxTemplateData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant", AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId()))
            .eq("id", id);
        String a = AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId());
        System.out.println(a);
        return baseMapper.delete(queryWrapper);
    }
}
  • 控制器
    这个就很简单了,中间建立接口实现调用
    WxTemplateController.java
/**
 * 控制器
 *
 * @author myn
 * @since 2021-10-12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxtemplate")
@Api(value = "", tags = "接口")
public class WxTemplateController extends BladeController {

    private IWxTemplateService wxTemplateService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入wxTemplate")
    public R<WxTemplateVO> detail(WxTemplate wxTemplate) {
        WxTemplate detail = wxTemplateService.getOne(Condition.getQueryWrapper(wxTemplate));
        return R.data(WxTemplateWrapper.build().entityVO(detail));
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入wxTemplate")
    public R<IPage<WxTemplateVO>> list(WxTemplate wxTemplate, Query query) {
        IPage<WxTemplate> pages = wxTemplateService.page(Condition.getPage(query), Condition.getQueryWrapper(wxTemplate));
        return R.data(WxTemplateWrapper.build().pageVO(pages));
    }


    /**
     * 自定义分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入wxTemplate")
    public R<IPage<WxTemplateVO>> page(WxTemplateVO wxTemplate, Query query) {
        IPage<WxTemplateVO> pages = wxTemplateService.selectWxTemplatePage(Condition.getPage(query), wxTemplate);
        return R.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入wxTemplate")
    public R save(@Valid @RequestBody WxTemplate wxTemplate) {
        return R.status(wxTemplateService.save(wxTemplate));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入wxTemplate")
    public R update(@Valid @RequestBody WxTemplate wxTemplate) {
        return R.status(wxTemplateService.updateById(wxTemplate));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入wxTemplate")
    public R submit(@Valid @RequestBody WxTemplate wxTemplate) {
        return R.status(wxTemplateService.saveOrUpdate(wxTemplate));
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(wxTemplateService.deleteLogic(Func.toLongList(ids)));
    }


    /**
     * 添加模板
     *
     * @return
     */
    @PostMapping("/addTem")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "添加", notes = "传入wxTemplate")
    public JSONObject addTem(@Valid @RequestBody WxTemplate wxTemplate) {
        return wxTemplateService.addWxTemplate(wxTemplate);
    }

    /**
     * 删除模板
     *
     * @return
     */
    @PostMapping("/removeTem")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "逻辑删除", notes = "传入id和模板id")
    public JSONObject removeTem(@ApiParam(value = "主键集合", required = true) @RequestParam String id, @RequestParam String templateId) {
        return wxTemplateService.delWxTemplate(id, templateId);
    }

    /**
     * 获取小程序账号的类目
     *
     * @return
     */
    @GetMapping("/getCategory")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "查询类目")
    public JSONObject getCategory() {
        return wxTemplateService.getCategory();
    }

    /**
     * 获取模板标题下的关键词列表
     *
     * @return
     */
    @GetMapping("/getPubTemplateKeyWordsById")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "获取模板标题下的关键词列表")
    public JSONObject getPubTemplateKeyWordsById(@ApiParam(value = "关键词id", required = true) @RequestParam String tid) {
        return wxTemplateService.getPubTemplateKeyWordsById(tid);
    }

    /**
     * 获取帐号所属类目下的公共模板标题
     *
     * @return
     */
    @GetMapping("/getPubTemplateTitleList")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "获取帐号所属类目下的公共模板标题")
    public JSONObject getPubTemplateTitleList(@ApiParam(value = "关键词集合", required = true) @RequestParam String ids, @RequestParam int start, @RequestParam int limit) {
        return wxTemplateService.getPubTemplateTitleList(ids, start, limit);
    }

    /**
     * 获取当前帐号下的个人模板列表
     *
     * @return
     */
    @GetMapping("/getTemplateList")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取帐号所属类目下的公共模板标题")
    public JSONObject getTemplateList() {
        return wxTemplateService.getTemplateList();
    }

    /**
     * 获取当前帐号下的个人模板列表
     *
     * @return
     */
    @GetMapping("/getTemplateListIdAndName")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "获取帐号所属类目下的公共模板标题和ID")
    public Map<String, String> getTemplateListIdAndName() {
        return wxTemplateService.getTemplateId();
    }
}

WxTemplateDataController.java

/**
 *  控制器
 *
 * @author myn
 * @since 2021-10-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxtemplatedata")
@Api(value = "", tags = "接口")
public class WxTemplateDataController extends BladeController {

    private IWxTemplateDataService wxTemplateDataService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入wxTemplateData")
    public R<WxTemplateDataVO> detail(WxTemplateData wxTemplateData) {
        WxTemplateData detail = wxTemplateDataService.getOne(Condition.getQueryWrapper(wxTemplateData));
        return R.data(WxTemplateDataWrapper.build().entityVO(detail));
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入wxTemplateData")
    public R<IPage<WxTemplateDataVO>> list(WxTemplateData wxTemplateData, Query query) {
        QueryWrapper<WxTemplateData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant", AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId()));
        String a=AESUtils.aesEncode(WxAppConfig.getAesKey(), WxAppConfig.getAppId());
        System.out.println(a);
        IPage<WxTemplateData> pages = wxTemplateDataService.page(Condition.getPage(query), queryWrapper);
        return R.data(WxTemplateDataWrapper.build().pageVO(pages));
    }


    /**
     * 自定义分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入wxTemplateData")
    public R<IPage<WxTemplateDataVO>> page(WxTemplateDataVO wxTemplateData, Query query) {
        IPage<WxTemplateDataVO> pages = wxTemplateDataService.selectWxTemplateDataPage(Condition.getPage(query), wxTemplateData);
        return R.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入wxTemplateData")
    public R save(@Valid @RequestBody WxTemplateData wxTemplateData) {
        return R.status(wxTemplateDataService.save(wxTemplateData));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入wxTemplateData")
    public R update(@Valid @RequestBody WxTemplateData wxTemplateData) {
        return R.status(wxTemplateDataService.updateById(wxTemplateData));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入wxTemplateData")
    public R submit(@Valid @RequestBody WxTemplateData wxTemplateData) {
        return R.status(wxTemplateDataService.saveOrUpdate(wxTemplateData));
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(wxTemplateDataService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 发送
     */
    @PostMapping("/send")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "发送", notes = "传入实体类")
    public String send(@Valid @RequestBody WxTemplateData wxTemplateData) throws Exception {
        return wxTemplateDataService.sendMsg(wxTemplateData);
    }

    /**
     * 获取需要填写的关键字名称
     * @return
     */
    @GetMapping("/getKeyWords")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取需要填写的关键字名称", notes = "传入模板id")
    public Map<String, String> getKeyWords(@ApiParam(value = "模板id", required = true) @RequestParam String templateId) throws Exception {
        return wxTemplateDataService.getDataList(templateId);
    }

    /**
     * 租户测试
     * @return
     */
    @GetMapping("/getList")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "租户测试")
    public List<WxTemplateData> getList(){
        return wxTemplateDataService.selectList();
    }

    /**
     * 租户删除测试
     * @return
     */
    @GetMapping("/deleteByEntity")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "租户测试")
    public int deleteByEntity(String id){
        return wxTemplateDataService.deleteByEntity(id);
    }
}

微信模板的建立可以在微信公众后台手工创建,比调用API简单。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容