com.zopen.wechat.mp.service.miniprogram.WechatProgramTemplateMessageService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.wechat.mp.service.miniprogram;
import com.google.gson.Gson;
import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.mp.dto.miniprogram.MiniProgramTemplateMessage;
import com.zopen.wechat.mp.dto.miniprogram.MiniProgramTemplateMessageData;
import com.zopen.wechat.mp.dto.miniprogram.MiniProgramTemplateMessageResp;
import com.zopen.wechat.mp.service.WechatHttpUrl;
import com.zopen.wechat.mp.task.WechatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Deprecated
@Component("atoWechatProgramTemplateMessageService")
public class WechatProgramTemplateMessageService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private WechatMpProperties wechatMpProperties;
// 发送模板消息(使用 application 中配置的 appId)
public MiniProgramTemplateMessageResp send(String touser, String template_id, String page, String form_id, Map dataMap, String emphasis_keyword) {
String appId = wechatMpProperties.getAppId();
return send(touser, template_id, page, form_id, dataMap, emphasis_keyword, appId);
}
/**
* 发送模板消息
*
* @param touser * 接收者openid
* @param template_id * 模板ID
* @param page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
* @param form_id * 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
* @param dataMap 模板内容,不填则下发空模板。具体格式请参考示例。例:{"keyword1": "a"}
* @param emphasis_keyword 模板需要放大的关键词,不填则默认无放大。例:keyword1.DATA
* @param appId * appId
* @return com.zopen.wechat.mp.dto.miniprogram.MiniProgramTemplateMessageResp
*/
public MiniProgramTemplateMessageResp send(String touser, String template_id, String page, String form_id, Map dataMap, String emphasis_keyword,
String appId) {
WechatAssert.notNullAndEmpty(touser, "touser 不能为空");
WechatAssert.notNullAndEmpty(template_id, "template_id 不能为空");
WechatAssert.notNullAndEmpty(form_id, "form_id 不能为空");
WechatAssert.notNullAndEmpty(appId, "app_id 不能为空");
// 请求地址
String accessToken = WechatInfo.getAccessToken(appId);
String url = String.format(WechatHttpUrl.MINI_SEND_TEMPLATE_MESSAGE, accessToken);
// 请求参数
Map dataMapObj = new HashMap<>(16);
if (!CollectionUtils.isEmpty(dataMap)) {
for (Map.Entry entry : dataMap.entrySet()) {
dataMapObj.put(entry.getKey(), new MiniProgramTemplateMessageData(entry.getValue()));
}
}
MiniProgramTemplateMessage message = new MiniProgramTemplateMessage(touser, template_id, page, form_id, dataMapObj, emphasis_keyword);
// 发送请求并解析返回内容
String resp = restTemplate.postForObject(url, message, String.class);
WechatAssert.notNullAndEmpty(resp, "发送模板消息失败:接口返回的内容为空");
MiniProgramTemplateMessageResp respObj = new Gson().fromJson(resp, MiniProgramTemplateMessageResp.class);
WechatAssert.notNull(respObj, "发送模板消息失败:解析返回的内容失败");
respObj.valid("发送模板消息失败");
return respObj;
}
}