WxMpUtil.java
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.theyeasy.weixin.util;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.w1hd.zzhnc.util.CommonUtil;
import com.w1hd.zzhnc.util.RedisUtil;
import com.w1hd.zzhnc.vo.Vo_TemplateDataItem;
import com.w1hd.zzhnc.vo.Vo_Template_KHZXTZ;
import com.w1hd.zzhnc.vo.Vo_Template_Param;
import me.chanjar.weixin.common.exception.WxErrorException;
import net.sf.json.JSONObject;
public class WxMpUtil {
// 添加消息模板 add by lcc 171027
public static String addTemplate(String template_id_short) {
if (getTemplateLongId(template_id_short)!="") return "error:该模板已经存在,请勿重复添加!";
String url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template";
Map<String, Object> param = new HashMap<String, Object>();
param.put("template_id_short", template_id_short);
try {
String result = sendWxPost(url, param);
System.out.println("addTemplate post.result=:" + result);
JSONObject resultJson = JSONObject.fromObject(result);
if (resultJson.getInt("errcode")==0) {
String template_id = resultJson.getString("template_id");
String key = RedisUtil.getMpAppid() + "_TemplateId_" + template_id_short;
RedisUtil.set(key, template_id); // 将长ID保存到缓存。
return template_id;
} else {
return "error:" + resultJson.getString("errmsg");
}
} catch (Exception e) {
e.printStackTrace();
}
return "error:服务器处理失败";
}
//判断是否添加过模板id
public static String getTemplateLongId(String template_id_short)
{
String redisKey = RedisUtil.getMpAppid() + "_TemplateId_" + template_id_short;
String template_id = RedisUtil.get(redisKey);
System.out.println("缓存里中" + redisKey + "的值是:" + template_id);
if (null == template_id || template_id.equals("")) return "";
return template_id;
}
// 发送模板消息_(模板名称:客户跟进提醒 Template_id_short:?) add by lcc 171027
public static String SendTemplate_Vanker(String openid, String custMsg, String custName, String projectName, String askTime, String directUrl) {
String template_id = "123O4orTZimyb1UY1L2SOiJsEn8-6DswBWTFE9kwnZw";
// 发送模板消息
Vo_Template_Param param = new Vo_Template_Param();
param.setTouser(openid);
param.setTemplate_id(template_id);
param.setUrl(directUrl);
Vo_Template_KHZXTZ data = new Vo_Template_KHZXTZ();
data.setFirst(new Vo_TemplateDataItem(custMsg,"#A00000")); //标题
data.setKeyword1(new Vo_TemplateDataItem(custName,"#173177"));//任务信息
data.setKeyword2(new Vo_TemplateDataItem(projectName,"#B0B0B0"));//所在项目
data.setKeyword3(new Vo_TemplateDataItem(askTime,"#B0B0B0"));//任务时间
data.setRemark(new Vo_TemplateDataItem("请点击打开客服面板进行回复","#B0B0B0"));//remark
param.setData(data);
try {
String result = sendTemplet(param);
return result;
} catch (WxErrorException e) {
e.printStackTrace();
}
return "发送失败,系统处理错误!";
}
// 发送模板消息_基础 add by lcc 171027
public static String sendTemplet(Vo_Template_Param data) throws WxErrorException {
String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + RedisUtil.getMpToken();
String param = CommonUtil.getJson(data);
System.out.println("发送模板消息,参数=" + param);
return CommonUtil.sendPost(requestUrl, param);
}
// 向微信服务器发送Post请求 add by lcc 171027
public static String sendWxPost(String uri, Map<String, Object> param) throws WxErrorException {
String uriWithAccessToken = uri;
String accessToken = RedisUtil.getMpToken();
System.out.print("MpAccessToken is:" + accessToken);
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
RestTemplate rest = new RestTemplate();
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(param, headers);
ResponseEntity<String> entity = rest.exchange(uriWithAccessToken, HttpMethod.POST, requestEntity, String.class);
String result = entity.getBody();
System.out.print("sendWxPost.result=" + result + " param=" + param.toString());
return result;
}
public static void main(String[] arg)
{
//WxMpUtil.SendTemplate_KHZXTZ("odxglt22ULkY7ZgpdgzdMW9zBr5c", "这是一条测试的模板消息", "admin", "无", "2017-10-28 21:33:31", "mini.weiyisz.com/wx/chatListView?salesId=1");
String res = WxMpUtil.SendTemplate_Vanker("oXqmKwSoVLT31zRncvr9Q5pWiLFI", "这是一条测试的模板消息", "admin", "无", "2017-10-28 21:33:31", "mini.weiyisz.com/wx/chatListView?salesId=1");
System.out.print("send:" + res);
}
}