微信公众号开发(Java) ---- 模板消息接口的使用


我们先看下官方文档给出的模板消息介绍:


微信公众号开发(Java) ---- 模板消息接口的使用_第1张图片

下面直接写我的流程了:

第一步:公众号平台上设置好模板消息形式如下(我拿的是测试号演示):

微信公众号开发(Java) ---- 模板消息接口的使用_第2张图片

第二步:提前写好访问模板消息的接口如下


	/**
	 * 发送模板消息
	 * 
	 * @param accessToken 接口访问凭证
	 * @param jsonMsg json格式的模板消息
	 * @return true | false
	 */
	public static boolean sendModelMessage(String accessToken, String jsonMsg) {
		log.info("消息内容:{}", jsonMsg);
		boolean result = false;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
		// 发送客服消息
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonMsg);

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				result = true;
				log.info("模板消息发送成功 errcode:{} errmsg:{}", errorCode, errorMsg);
			} else {
				log.error("模板消息发送失败 errcode:{} errmsg:{}", errorCode, errorMsg);
			}
		}

		return result;
	}


第三步:构造需要的实体类,主要是方便最后构建json数据,在这里我直接贴出我的业务用到的实体类

模板消息实体类:


public class MoveCarAlarmTemplate {
	private String touser; //用户的openId
	private String template_id; //模板消息id
	private String url;
	private String topcolor; //标题颜色
	private ModelMessage_Data data; //消息具体内容
	
	public String getTouser() {
		return touser;
	}
	public void setTouser(String touser) {
		this.touser = touser;
	}
	public String getTemplate_id() {
		return template_id;
	}
	public void setTemplate_id(String template_id) {
		this.template_id = template_id;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public String getTopcolor() {
		return topcolor;
	}
	public void setTopcolor(String topcolor) {
		this.topcolor = topcolor;
	}
	public ModelMessage_Data getData() {
		return data;
	}
	public void setData(ModelMessage_Data data) {
		this.data = data;
	}
}



 ModelMessage_Data类
public class ModelMessage_Data {
	private Data_Title title;
	private Data_MoveDate moveDate;
	private Data_ToUserOpenNumber toUserOpenNumber;
	private Data_FromUserOpenNumber fromUserOpenNumber;
	private Data_Remark remark;
	public Data_Remark getRemark() {
		return remark;
	}
	public void setRemark(Data_Remark remark) {
		this.remark = remark;
	}
	public Data_Title getTitle() {
		return title;
	}
	public void setTitle(Data_Title title) {
		this.title = title;
	}
	
	public Data_MoveDate getMoveDate() {
		return moveDate;
	}
	public void setMoveDate(Data_MoveDate moveDate) {
		this.moveDate = moveDate;
	}
	public Data_ToUserOpenNumber getToUserOpenNumber() {
		return toUserOpenNumber;
	}
	public void setToUserOpenNumber(Data_ToUserOpenNumber toUserOpenNumber) {
		this.toUserOpenNumber = toUserOpenNumber;
	}
	public Data_FromUserOpenNumber getFromUserOpenNumber() {
		return fromUserOpenNumber;
	}
	public void setFromUserOpenNumber(Data_FromUserOpenNumber fromUserOpenNumber) {
		this.fromUserOpenNumber = fromUserOpenNumber;
	}
	
}

Data_Title title,

Data_MoveDate
Data_ToUserOpenNumber 
Data_FromUserOpenNumber 
Data_Remark 

以上几个实体类就不贴了,每个类里面都只有两个String变量,分别为 value 和 color

第四步:业务处理

/**
	 * 该方法用于在呼叫对方成功后,服务号会分别向双方发送挪车提醒消息
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	
	@RequestMapping("/send")
	public String sendMoveMessage(HttpServletRequest request,Model model) throws IOException{
		
		Properties prop = new Properties();
		FileInputStream fis = (FileInputStream) request.getSession().getServletContext().getResourceAsStream("/WEB-INF/classes/appconfig.properties");
		prop.load(fis);
		// 第三方用户唯一凭证
		String appId = prop.getProperty("appId");
		// 第三方用户唯一凭证密钥
		String appSecret = prop.getProperty("appSecret");
		// 获取接口访问凭证
		String accessToken = CommonUtil.getToken(appId, appSecret).getAccessToken();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		//被呼叫方的专用号码及openid
		String toOpenNum = request.getParameter("toUserOpenNumber");
		String toOpenId = request.getParameter("toUserOpenId");
		
		//呼叫方的专用号码及openid
		String fromOpenNum = request.getParameter("fromUserOpenNumber");
		if(!StringUtils.isNotBlank(fromOpenNum)){
			fromOpenNum =  String.valueOf((int)((Math.random()*9+1)*100000));
		}
		String fromOpenId = request.getParameter("fromUserOpenId");
		
		//被呼叫方微信上收到挪车提醒
		MoveCarAlarmTemplate temp = new MoveCarAlarmTemplate();
		ModelMessage_Data data = new ModelMessage_Data();
		Data_Title title = new Data_Title();
		Data_MoveDate moveDate = new Data_MoveDate();
		Data_FromUserOpenNumber fromUserOpenNumber = new Data_FromUserOpenNumber();
		Data_ToUserOpenNumber toUserOpenNumber = new Data_ToUserOpenNumber();
		Data_Remark remark = new Data_Remark();
		
		remark.setValue("xxxxxx");
		remark.setColor("#2e307b");
		
		title.setValue("尊敬的车主,您好");
		title.setColor("#2e307b");
		
		moveDate.setValue(sdf.format(new Date()));
		moveDate.setColor("#2e307b");
		
		fromUserOpenNumber.setValue("您的挪车请已经发送给车主"+toOpenNum);
		fromUserOpenNumber.setColor("#2e307b");
		
		toUserOpenNumber.setValue("用户"+fromOpenNum+"请您挪车");
		toUserOpenNumber.setColor("#2e307b");
		
		data.setFromUserOpenNumber(fromUserOpenNumber);
		data.setMoveDate(moveDate);
		data.setTitle(title);
		data.setToUserOpenNumber(toUserOpenNumber);
		data.setRemark(remark);
		
		temp.setTouser(toOpenId);
		temp.setData(data);
		temp.setTopcolor("#2e307b");
		temp.setUrl("");
		temp.setTemplate_id(挪车提醒的模板ID);
		
		
		
		String jsonMsg = JSONObject.fromObject(temp).toString();
		AdvancedUtil.sendModelMessage(accessToken, jsonMsg);
		
		
		//如果呼叫方也关注了该服务号,则呼叫方也将收到已发送挪车提醒的消息
		if(StringUtils.isNotBlank(fromOpenId)){
			temp.setTouser(fromOpenId);
			temp.setTemplate_id(请求回执的模板ID);
			String jsonMsg2 = JSONObject.fromObject(temp).toString();
			AdvancedUtil.sendModelMessage(accessToken, jsonMsg2);
		}
		
		
		return "warnSuccess";
	}


最后成果展示:


主叫方收到(前提是主叫方也关注了该服务号):


被叫方收到:


完成~~~

你可能感兴趣的:(微信公众号开发(Java) ---- 模板消息接口的使用)