package cn.com.chinautrust.ssoserver.utils;javascript
import java.util.HashMap;
import java.util.Map;java
import sun.org.mozilla.javascript.internal.ObjArray;json
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;api
/**
* 用于微信企业号消息发送
* @author yq
*
*/
public class WxUtil {
static String corpid = "ww90e565617aff08f";
static String Secret = "xVsrITDz0G6iM6_9PouV2hjDCS38G4fllWK5EvjD";
static String AgentSecret = "zXp_DTAEAZ9A1wYJ31xBzzhS9mZmLXBZncKmxUSwahQ";
static String getAccessToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT";
static String getAgentAccessToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT";
static String getUsers = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD";
static String sendMsgUrl = " https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
public static void main(String[] args) {
getAccessToken = getAccessToken.replace("ID", corpid);
System.out.println("==00==getAccessToken:"+getAccessToken+"=================");
getAccessToken = getAccessToken.replace("SECRECT", Secret);
System.out.println("==11==getAccessToken:"+getAccessToken+"=================");
String accessToken = HttpSendUtil.sendGet(getAccessToken);
System.out.println("===accessToken:"+accessToken+"==================");
JSONObject accesstokeJson = JSONObject.parseObject(accessToken);
String accessTokenStr = accesstokeJson.getString("access_token");
String userinfos = getUserIds(accessTokenStr);
System.out.println("=====accessTokenStr:【"+accessTokenStr+"】===========");
getAgentAccessToken = getAgentAccessToken.replace("ID", corpid);
getAgentAccessToken = getAgentAccessToken.replace("SECRECT", AgentSecret);
String agent_accessToken = HttpSendUtil.sendGet(getAgentAccessToken);
JSONObject agentAccesstokeJson = JSONObject.parseObject(agent_accessToken);
String agentAccesstokeJsonStr = agentAccesstokeJson.getString("access_token");
System.out.println("=======agentAccesstokeJsonStr:"+agentAccesstokeJsonStr+"================");
sendMsgUrl = sendMsgUrl.replace("ACCESS_TOKEN", agentAccesstokeJsonStr);
String param = "{"
+ "\"touser\":\""+userinfos+"\","
+ "\"toparty\":\"\","
+ "\"totag\":\"\","
+ "\"msgtype\":\"text\","
+ "\"agentid\": 1000002,"
+ "\"text\" : {"
+ "\"content\":\"恭喜你啊,恭喜你。\""
+ "},"
+ "\"safe\":0"
+ "}";
System.out.println("========param:"+JSON.toJSONString(param)+"===========");
String res = HttpSendUtil.sendPost(sendMsgUrl, param);
System.out.println("=====res:"+res+"======");
}
public static String getUserIds(String accessTokenStr){
getUsers = getUsers.replace("ACCESS_TOKEN", accessTokenStr);
getUsers = getUsers.replace("DEPARTMENT_ID", "1");
getUsers = getUsers.replace("FETCH_CHILD", "0");
System.out.println("===getUsers:"+getUsers+"==================");
String userinfos = HttpSendUtil.sendGet(getUsers);
System.out.println("===userinfos:"+userinfos+"==================");
String userlist = JSONObject.parseObject(userinfos).getString("userlist");
JSONArray users = JSONArray.parseArray(userlist);
JSONObject[] objs = users.toArray(new JSONObject[users.size()]);
StringBuffer buf = new StringBuffer();
String userid = "";
for (int i=0;i
userid = objs[i].getString("userid");
if( i == objs.length-1 ){
buf.append(userid);
}else{
buf.append(userid+"|");
}
}
return buf.toString();
}
}
微信