微信企业号获取关注者信息示例
企业微信号开发,企业微信号,企业微信号获取关注者信息2016-08-03
企业如果需要员工在跳转到企业网页时带上员工的身份信息,需构造如下的链接:
| 参数 | 必须 | 说明 |
|---|---|---|
| appid | 是 | 企业的CorpID |
| redirect_uri | 是 | 授权后重定向的回调链接地址,请使用urlencode对链接进行处理 |
| response_type | 是 | 返回类型,此时固定为:code |
| scope | 是 | 应用授权作用域,此时固定为:snsapi_base |
| state | 否 | 重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值,长度不可超过128个字节 |
| #wechat_redirect | 是 | 微信终端使用此参数判断是否需要带上身份信息 |
员工点击后,页面将跳转至 redirect_uri?code=CODE&state=STATE,企业可根据code参数获得员工的userid。
这样,在用户点击后,则可以获取到该用户的code值,此值也需要程序进行记录。$('#index').on('pageshow', function(e) {
code = location.href.split('?')[1].split('&')[0].split('=')[1];
getUserId();
});
function getUserId (){
var url = localURL+'/getUserId.json';
var param = {
code : code
};
var success = function (data) {
userId = data.UserId;
alert(userId);
};
var error = function (data) {
};
ajaxPost(url, param, success, error);
}
private String CorpID = "xxxxxxxxxxxxxxxxxxxxxxx";
private String Secret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
public String getToken() throws Exception{
String strResult = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("corpID", CorpID));
params.add(new BasicNameValuePair("corpsecret", Secret));
strResult = HttpUtil.sendRequestWechat(
"https://qyapi.weixin.qq.com/cgi-bin/gettoken", params);
String token = JSONObject.fromObject(strResult).getString("access_token");
return token;
}
@RequestMapping("/getUserId.{ext}")
public Object getUserId (@RequestBody BaseForm form) throws Exception {
String token = getToken();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("access_token", token));
params.add(new BasicNameValuePair("code", form.getCode()));
String strResult = null;
strResult = HttpUtil.sendRequestWechat(
"https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo", params);
String strOpenId = JSONObject.fromObject(strResult).getString("UserId");
Map<String, String> mapResult = new HashMap<String, String>();
mapResult.put("UserId", strOpenId);
return mapResult;
}