微信企业号二次开发(推送和获取用户信息)

来源:互联网 发布:欧莱雅玻尿酸系列知乎 编辑:程序博客网 时间:2024/05/20 14:20

微信企业号不同于公众号和服务号,它提供了比较强大的接口,可以自由开发出各种应用和功能


----------------------利用微信企业号推送用户信息-----------------------------

import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

/**微信企业号给关注的用户主动发送推送消息 企业号发消息条数没有限制*/
public class SendMessage {
// 获取配置文件中的值
private final static String CORPID = "ww40fde****";// 登录企业号,在“我的企业”里面的CorpID
private final static String AgentId = "100***";    //企业应用 里面的某一个应用的AgentId
private final static String CORPSECRET = "e-UQWiLTYY***";   //企业应用 里面的某一个应用的Secret
// 获取访问权限码URL
private final static String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+CORPID+"&corpsecret="+CORPSECRET;
// 创建会话请求URL
private final static String CREATE_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";

// 获取接口访问权限码token函数
public String getAccessToken() {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(ACCESS_TOKEN_URL);
post.releaseConnection();
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
String result = "";
try {
client.executeMethod(post);
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
} catch (IOException e) {
e.printStackTrace();
}
// 将数据转换成json
net.sf.json.JSONObject jasonObject = JSONObject.fromObject(result);
result = (String) jasonObject.get("access_token");
post.releaseConnection();
return result;
}


/*消息推送

* @param touser
* 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,
* 则向关注该企业应用的全部成员发送
* @param toparty
* 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
* @param totag
* 标签ID列表,多个接收者用‘|’分隔。当touser为@all时忽略本参数
* @param content
* 消息内容*/


@SuppressWarnings("deprecation")
public String sendWeChatMessage(String ACCESS_TOKEN,String touser, String toparty, String totag, String content) {
HttpClient client = new HttpClient();
StringBuffer sb = new StringBuffer();
sb.append("{");
sb.append("\"touser\":" + "\"" + touser + "\",");//成员ID
sb.append("\"toparty\":" + "\"" + toparty + "\",");// 部门ID
sb.append("\"totag\":" + "\"" + totag + "\",");//标签ID
sb.append("\"msgtype\":" + "\"" + "text" + "\",");//消息类型为text
sb.append("\"agentid\":" + "\"" + AgentId + "\",");//应用的AgentId
sb.append("\"text\":" + "{");
sb.append("\"content\":" + "\"" + content + "\"},");//消息内容
sb.append("\"debug\":" + "\"" + "1" + "\"");
sb.append("}");
// 请求链接
String url = CREATE_SESSION_URL + ACCESS_TOKEN;
PostMethod post = new PostMethod(url);
post.releaseConnection();
post.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
// 给post设置参数
post.setRequestBody(sb.toString());
String result = "";
try {
client.executeMethod(post);
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
} catch (IOException e) {
e.printStackTrace();
}
post.releaseConnection();
return result;
}
        //主函数调用
public static void main(String[] args) {
SendMessage weChat = new SendMessage();
String ACCESS_TOKEN =weChat.getAccessToken();
String  content ="欢迎光临!";
String  toparty="8";       //部门编号
weChat.sendWeChatMessage(ACCESS_TOKEN,"",  toparty, "",  content);//发送给部门编号为8的全部人员一条“欢迎光临”的信息
}
}


---------------------------获取访问用户的信息-----------------------------

插入在企业号应用中的菜单网址设置如下:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww40fde***&redirect_uri=http%3a%2f%2fczxiexm-test.iask.in:11118%2fqyh_app%2fServlet_user&response_type=code&scope=snsapi_base&agentid=1000***&state=1#wechat_redirect

appid=ww40fde***  //在“我的企业”里面的CorpID

agentid=1000*** //企业应用 里面的某一个应用的AgentId

redirect_uri=http%3a%2f%2fczxiexm-test.iask.in:11118%2fqyh_app%2fServlet_user 

//可信域名,需在企业应用里面:跟 网页授权及JS-SDK 设置一样,后面带的文件是后台响应的文件


后台代码:

通过企业号点击上面那条连接,后台Servlet_user可以通过 String code=request.getParameter("code"); 得到code值

import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class qyh_jk {


private final static String CORPID = "ww40fd***";
private final static String CORPSECRET = "e-UQWiLTYYRc5***";
// 获取访问权限码URL
private final static String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+CORPID+"&corpsecret="+CORPSECRET;

        //获取token
public String getAccessToken() {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(ACCESS_TOKEN_URL);
post.releaseConnection();
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
String result = "";
try {
client.executeMethod(post);
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
} catch (IOException e) {
e.printStackTrace();
}
// 将数据转换成json
net.sf.json.JSONObject jasonObject = JSONObject.fromObject(result);
result = (String) jasonObject.get("access_token");
post.releaseConnection();
//System.out.println(result);
return result;
}

       //用token和code获取userid
public String getuserid(String code,String ACCESS_TOKEN) {
String getuserid_URL="https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token="+ACCESS_TOKEN+"&code="+code;
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(getuserid_URL);
post.releaseConnection();
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
String result = "";
try {
client.executeMethod(post);
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println("接口-用户ID串:"+result);
} catch (IOException e) {
e.printStackTrace();
}
// 将数据转换成json
net.sf.json.JSONObject jasonObject = JSONObject.fromObject(result);
result = (String) jasonObject.get("UserId");
post.releaseConnection();
System.out.println("接口-用户id:"+result);
return result;
}

        //利用userid和token或者usemassage
public String[] getusermsg(String UserId,String ACCESS_TOKEN) {
String getuserid_URL="https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+ACCESS_TOKEN+"&userid="+UserId;
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(getuserid_URL);
post.releaseConnection();
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
String[] result = new String[2];
try {
client.executeMethod(post);
result[0] = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println("接口-用户信息串:"+result);
} catch (IOException e) {
e.printStackTrace();
}
// 将数据转换成json
net.sf.json.JSONObject jasonObject = JSONObject.fromObject(result[0]);
result[0] = (String) jasonObject.get("name").toString();
result[1]=  (String) jasonObject.get("department").toString();
post.releaseConnection();
return result;
}
}

               



                Servlet_user响应调用企业号用户信息

                String code=request.getParameter("code");
qyh_jk jk = new qyh_jk();
String ACCESS_TOKEN =jk.getAccessToken();  //获取token
String UserID=jk.getuserid(code,ACCESS_TOKEN); //用户ID
String[] msg=jk.getusermsg(UserID,ACCESS_TOKEN);  //用户信息