UtilWeixinMsg.java
3.65 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
package com.taover.util;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.ClientException;
import com.aliyun.mns.common.ServiceException;
import com.aliyun.mns.model.Message;
public class UtilWeixinMsg {
public static Map<String, String> GLOBAL_CONFIG = new HashMap<String, String>(){{
put("appId", "c2MkSTjT8ghZ");
put("secretKey", "7105ec065b8bb30fa6e3f13fccf92d0G");
put("agentId", "47");
put("tenantId", "11");
put("alimnsQueueName", "oms-pc-msg-production");
put("accessId", "H4fIVB56iHjR6zQw");
put("accessKey", "7bA395UltFp16kWPJT7Pfz0XYXCk4Q");
put("mnsEndpoint", "http://1225610490807748.mns.cn-hangzhou.aliyuncs.com");
}};
//发�?�文本消�?
public static void sendTextMessage(String weixinId, String content, Map<String, String> alimnsConfig) throws Exception{
Map<String, Object> data = new HashMap<String, Object>();
data.put("appId", getValue("appId", alimnsConfig));
String timestamp = System.currentTimeMillis()+"";
data.put("timestamp", timestamp);
data.put("signature", UtilEncrypt.MD5Lower32(getValue("appId", alimnsConfig) + timestamp + getValue("secretKey", alimnsConfig)));
data.put("agentId", getValue("agentId", alimnsConfig));
data.put("toWxid", weixinId);
data.put("kind", "TEXT");
data.put("content", content);
data.put("atList", "");
data.put("merchantId", getValue("tenantId", alimnsConfig));
CloudAccount account = new CloudAccount(getValue("accessId", alimnsConfig), getValue("accessKey", alimnsConfig), getValue("mnsEndpoint", alimnsConfig));
MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全
try {
CloudQueue queue = client.getQueueRef(getValue("alimnsQueueName", alimnsConfig));
Message message = new Message();
message.setMessageBody(JSONObject.toJSONString(data));
queue.putMessage(message);
}catch(ClientException ce){
System.out.println("Something wrong with the network connection between client and MNS service."
+ "Please check your network and DNS availablity.");
ce.printStackTrace();
}catch(ServiceException se){
se.printStackTrace();
if (se.getErrorCode() != null){
if (se.getErrorCode().equals("QueueNotExist")){
System.out.println("Queue is not exist.Please create before use");
}else if(se.getErrorCode().equals("TimeExpired")){
System.out.println("The request is time expired. Please check your local machine timeclock");
}
}
}catch(Exception e){
System.out.println("Unknown exception happened!");
e.printStackTrace();
}
client.close(); // 程序�?出时,需主动调用client的close方法进行资源释放
}
public static void sendTextMessage(String weixinId, String content) throws Exception{
sendTextMessage(weixinId, content, null);
}
public static String getValue(String key, Map<String, String> localConfig) {
if(localConfig == null) {
return GLOBAL_CONFIG.get(key);
}else {
String data = localConfig.get(key);
if(data == null) {
return GLOBAL_CONFIG.get(key);
}else {
return data;
}
}
}
public static void main(String args[]) {
try {
sendTextMessage("wxid_kn7w9ctq11ta21", "hello weixin v2");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}