package com.xsili.context.util.publishMsg;
import com.xsili.context.util.httpclient.HttpClientUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
public class PublishWeChatMsg {
Logger logger = LoggerFactory.getLogger(PublishWeChatMsg.class);
private String touser;
private String templateId;
private String page;
private Map<String, Object> datas;
private String url;
public PublishWeChatMsg(String touser, String templateId, String page, Map<String, Object> datas, String accessToken) {
this.touser = touser;
this.templateId = templateId;
this.page = page;
this.datas = datas;
this.url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
}
public JSONObject pushWeChatMsg() {
JSONObject result =null;
try {
Map<String, Object> params = new HashMap<>();
params.put("touser", this.touser);
params.put("template_id", this.templateId);
params.put("page", this.page);
params.put("data", this.datas);
JSONObject jsonObject = JSONObject.fromObject(params);
System.out.println(jsonObject.toString());
result = HttpClientUtil.Post(url, JSONObject.fromObject(params));
System.out.println("result:" + result);
logger.info("下发消息通知的返回结果:" + result);
} catch (Exception e) {
logger.info("下发消息通知抛出异常");
e.printStackTrace();
}
return result;
}
}
String templateId = "Qvzw2Zcoxmw2WbMagw4u_5wn1qyfxzJ_bVl9q0AKC6Y";
String title = message.getTitle();
String orderNo = message.getOrderNo();
String tip = message.getTip();
Map<String, Object> datas = new HashMap<>();
Map<String, Object> value1 = new HashMap<>();
if (title.length()>20)title = title.substring(0,15)+"...";
value1.put("value",title);
datas.put("thing6",value1);
Map<String, Object> value2 = new HashMap<>();
if (orderNo.length()>29)orderNo = orderNo.substring(0,25)+"...";
value2.put("value",orderNo);
datas.put("character_string4",value2);
Map<String, Object> value3 = new HashMap<>();
if (tip.length()>20)tip = tip.substring(0,15)+"...";
value3.put("value",tip);
datas.put("thing2",value3);
PublishWeChatMsg publishWeChatMsg = new PublishWeChatMsg(message.getOpendId(), templateId, "pages/user/message/messages/messages?type=MESS", datas, jsonObject.getString("access_token"));
net.sf.json.JSONObject jsonObject1 = publishWeChatMsg.pushWeChatMsg();
String s = jsonObject1.toString();
message.setUpdateTime(new Date());
message.setState(1);
message.setError(s);