weixinutil.java_WeixinUtil.java

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

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值