微信公众平台——模板消息接口

本文介绍了一种基于Java实现的微信模板消息推送方法,通过自动消息推送服务将定制化的消息内容推送给指定用户。该方法涵盖了消息构造、微信接口调用及权限验证等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于接口文档,请注意:

1、模板消息调用时主要需要模板ID和模板中各参数的赋值内容;
2、模板中参数内容必须以".DATA"结尾,否则视为保留字;
3、模板保留符号"{{ }}"。

1.service

/**
 * 微信自动消息推送
 * @author Administrator
 *
 */
@Service
public class AutomaticMessagePushServiceImpl implements AutomaticMessageServicePush {
	
	@Autowired
	private MyCache cache;

	@Override	
	public String AutomaticMessagePush(JSONObject JsonCustom,String first,String remark,String openId,String templateId,String customUrl,Integer identification) {
		
		JSONObject Json = new JSONObject();
		JSONObject Jsonfirst = new JSONObject();
		JSONObject Jsonremark = new JSONObject();
		//头部
		Jsonfirst.put("value", first);
		Jsonfirst.put("color", "#000000");
		//尾部
		Jsonremark.put("value", remark);
		Jsonremark.put("color", "#000000");
		//自定义内容增加头和尾
		JsonCustom.put("first", Jsonfirst);
		JsonCustom.put("remark", Jsonremark);
		
		//微信openId
		Json.put("touser", openId);
		//模板id
		Json.put("template_id", templateId);
		//详情跳转地址
		Json.put("url", customUrl);
		//json数据
		Json.put("data", JsonCustom);
		
		//根据标识发送对应公众号
		String appid;
		String appSecret;
		String keyName ="";
		if(identification==Constants.TALENT_IDENTIFICATION){
			keyName = "wechatAccessToken";
			appid = ConfigHelper.getInstance("config").getValue("DaRenAppID");
			appSecret = ConfigHelper.getInstance("config").getValue("DaRenAppSecret");
		}else{
			keyName = "wechat_AccessToken";
			appid = ConfigHelper.getInstance("config").getValue("WalletAppID");
			appSecret = ConfigHelper.getInstance("config").getValue("WalletAppSecret");
		}
		
		String url_Template_GetAccessToken ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
		String url_Template_GetAccessTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
		
		String accessToken = cache.getString(keyName);
		if(accessToken == null){
			//获取token
			String url_GetAccessToken = String.format(url_Template_GetAccessToken, appid,appSecret);
			JSONObject accessTokenMap = sendGetRequest(url_GetAccessToken);
			accessToken = accessTokenMap.getString("access_token");
			cache.setString(keyName, 6000, accessToken);
		}
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
		
		String result = HttpRequestSimple.getInstance().postSendHttp(url, Json.toString());
		
		return result;
	}

	public static JSONObject sendGetRequest(String url){
		HttpClient httpClient = CustomHttpClient.GetHttpClient();
		HttpGet get = new HttpGet(url);
		get.setHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=utf-8");
		BufferedReader br = null;
		
		try {
			// 发送请求,接收响应
			HttpResponse resp = httpClient.execute(get);
			int ret = resp.getStatusLine().getStatusCode();
			if(ret == HttpStatus.SC_OK){
				// 响应分析
				HttpEntity entity = resp.getEntity();
				br = new BufferedReader(new InputStreamReader(
						entity.getContent(), "UTF-8"));
				StringBuffer responseString = new StringBuffer();
				String str = br.readLine();
				while (str != null) {
					responseString.append(str);
					str = br.readLine();
				}
				return JSON.parseObject(responseString.toString());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		}
		return new JSONObject();
	}
	

}

2.controller调用

	/**
	 * 绑定消息推送公共方法
	 * @param openId
	 */
	private void bindingPush(String openId) {
		//发送自动消息推送
		com.alibaba.fastjson.JSONObject Json = new com.alibaba.fastjson.JSONObject();
		com.alibaba.fastjson.JSONObject name1 = new com.alibaba.fastjson.JSONObject();
		com.alibaba.fastjson.JSONObject name2 = new com.alibaba.fastjson.JSONObject();
		com.alibaba.fastjson.JSONObject time = new com.alibaba.fastjson.JSONObject();
		
		name1.put("value", "通金达人");
		name1.put("color", "#000000");
		
		name2.put("value", "微信");
		name2.put("color", "#000000");
		//绑定时间
		time.put("value", DateUtil.dateToString1(new Date()));
		time.put("color", "#000000");
		
		Json.put("name1", name1);
		Json.put("name2", name2);
		Json.put("time", time);
		
		//消息头部
		String first = "用户您好,恭喜您账户绑定成功!";
		//消息尾部
		String remark = "您可以使用下方微信菜单进行更多功能体验,如需解除绑定请回复'qxbd'";
		//详情跳转页面
		String customUrl = ConfigHelper.getInstance("config").getValue("DaRenPushUrl");
		//根据json内容微信自动消息推送
		servicePush.AutomaticMessagePush(Json,first,remark,openId,Constants.TALENT_BINDING_SUCCESS,customUrl,Constants.TALENT_IDENTIFICATION);
	}
	



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值