package com.smart.vote.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.support.odps.udf.CodecCheck.A;
public class WeixinUtil {
public static String getAccessToken() {
String appId = PropertiesUtils.get("weixin.appid");
String secret = PropertiesUtils.get("weixin.secret");
Map map = new HashMap();
map.put("grant_type", "client_credential");
map.put("appid", appId);
map.put("secret", secret);
String token = (String) CacheUtil.getInstance().getCacheObj(
CacheConfig.ACCESS_TOKEN);
if (StringUtils.isBlank(token)) {
token = HttpUtil.httpsDoGet(
"https://api.weixin.qq.com/cgi-bin/token?"
+ "grant_type=client_credential&"
+ "appid=" + PropertiesUtils.get("weixin.appid") + "&"
+ "secret=" + PropertiesUtils.get("weixin.secret"),
"utf-8");
CacheUtil.getInstance().addCacheObj(CacheConfig.ACCESS_TOKEN, token);
}
JSONObject json = JSONObject.fromObject(token);
if(StringUtils.isBlank((String) json.get("access_token"))){
return token;
} else
return (String) json.get("access_token");
}
public static String getJSApiTicket() {
String accessToken = getAccessToken();
System.out.println("accessToken: "+accessToken);
Map map = new HashMap();
map.put("access_token", accessToken);
map.put("type", "jsapi");
String ticket = (String) CacheUtil.getInstance().getCacheObj(
CacheConfig.API_TICKET);
if (StringUtils.isBlank(ticket)) {
ticket = HttpUtil.httpsDoPost(
"https://api.weixin.qq.com/cgi-bin/ticket/getticket", map,
"UTF-8");
//ticket = HttpUtil.httpsDoGet(
//"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="
//+ accessToken + "&type=jsapi", "utf-8");
CacheUtil.getInstance().addCacheObj(CacheConfig.API_TICKET, ticket);
}
System.out.println("ticket: " + ticket);
JSONObject json = JSONObject.fromObject(ticket);
if(StringUtils.isBlank((String) json.get("ticket"))){
return ticket;
} else
return (String) json.get("ticket");
}
public static Map getUserList(String accessToken, String nextOpenID){
Map paramsMap = new HashMap();
paramsMap.put("access_token", accessToken);
paramsMap.put("next_openid", nextOpenID);
String openIDList = HttpUtil.httpsDoPost("https://api.weixin.qq.com/cgi-bin/user/get", paramsMap, "utf-8");
System.out.println(openIDList);
JSONObject jsonObj = JSONObject.fromObject(openIDList);
Map retMap = new HashMap();
List idList = new ArrayList();
retMap.put("total", jsonObj.get("total"));
retMap.put("count", jsonObj.get("count"));
Object openId = jsonObj.get("data");
Object obj = (JSONObject.fromObject(openId)).get("openid");
JSONArray jsonArr = JSONArray.fromObject(obj);
for(int i = 0 ; i < jsonArr.size() ; i++){
idList.add((String) jsonArr.get(i));
}
retMap.put("openIDList", idList);
return retMap;
}
//public static Map getUserInfo(String accessToken, String openId){
//Map paramsMap = new HashMap();
//paramsMap.put("access_token", accessToken);
//paramsMap.put("openid", "otYOUweAts_XQjUyBmswG-WK5hLU");
//paramsMap.put("lang", "zh_CN");
String userInfo = HttpUtil.httpsDoPost("https://api.weixin.qq.com/cgi-bin/user/info", paramsMap, "utf-8");
//String userInfo = HttpUtil.httpsDoGet(
//"https://api.weixin.qq.com/cgi-bin/user/info?"
//+ "access_token=" + accessToken + "&"
//+ "openid=otYOUwVKJ8ykGewank4cSvae2Y1Q&"
//+ "lang=zh_CN",
//"utf-8");
//System.out.println(userInfo);
//}
public static String getNonceStr(){
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String res = "";
Random rd = new Random();
for(int i = 0;i < 16; i++){
res += chars.charAt(rd.nextInt(chars.length() - 1));
}
return res;
}
public static void main(String[] args) {
String accessToken = getAccessToken();
System.out.println("accessToken: " + accessToken);
System.out.println(getUserList(accessToken, null));
//getUserInfo(accessToken, null);
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史