UtilWeixinMsg.java 3.65 KB
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();
		}
	}
}