微信token和用户工具类封装

master
星期八 5 years ago
parent c433662fe1
commit c77c01e099
  1. 1
      src/main/java/com/lq/entity/WechatUser.java
  2. 2
      src/main/java/com/lq/wechat/mode/AccessToken.java
  3. 42
      src/main/java/com/lq/wechat/util/WechatUserUtil.java
  4. 1
      src/main/java/com/lq/wechat/util/token/AccessTokenUtil.java
  5. 18
      src/test/java/com/lq/wechat/util/token/AccessTokenUtilTest.java

@ -6,7 +6,6 @@ import java.util.Date;
/** /**
* 微信公众号用户 * 微信公众号用户
* Created by qi_liang on 2018/5/24.
* @author qi * @author qi
*/ */
public class WechatUser extends IdEntity{ public class WechatUser extends IdEntity{

@ -2,7 +2,7 @@ package com.lq.wechat.mode;
/** /**
* @Author: qi * @Author: qi
* @Description: * @Description: token对象
* @Date: Create in 9:28 AM 2019/3/15 * @Date: Create in 9:28 AM 2019/3/15
*/ */
public class AccessToken { public class AccessToken {

@ -1,8 +1,10 @@
package com.lq.wechat.util; package com.lq.wechat.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.lq.code.util.HttpKit; import com.lq.code.util.HttpKit;
import com.lq.entity.WechatUser; import com.lq.entity.WechatUser;
import com.lq.wechat.mode.openid.WechatOpenIdList;
/** /**
* @Author: qi * @Author: qi
@ -14,7 +16,7 @@ public class WechatUserUtil {
/** /**
* 获取关注用户列表 * 获取关注用户列表
*/ */
public static final String GET_USER_LIST_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN"; public static final String GET_USER_LIST_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
/** /**
* 获取用户基本信息接口 (get请求) * 获取用户基本信息接口 (get请求)
*/ */
@ -24,9 +26,39 @@ public class WechatUserUtil {
*/ */
public static final String BATCHGET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN"; public static final String BATCHGET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
/**
* 获取关注用户列表
* @param accessToken 接口凭证
* @param nextOpenId 下一个openid
* @return
*/
public static String getWechatUserListStr(String accessToken,String nextOpenId){
String getUrl = GET_USER_LIST_URL.replace("ACCESS_TOKEN",accessToken).replace("NEXT_OPENID",nextOpenId);
String result = HttpKit.get(getUrl);
return result;
}
/**
* 获取关注用户列表
* @param accessToken
* @param nextOpenId
* @return
*/
public static WechatOpenIdList getWechatUserList(String accessToken,String nextOpenId){
String result = getWechatUserListStr(accessToken,nextOpenId);
WechatOpenIdList wechatOpenIdList = JSON.parseObject(result,WechatOpenIdList.class);
return wechatOpenIdList;
}
/**
* 获取微信用户资料
* @param accessToken
* @param openId
* @return
*/
public static String getWechatUserStr(String accessToken,String openId){ public static String getWechatUserStr(String accessToken,String openId){
String url = GET_USER_INFO_URL.replace("ACCESS_TOKEN",accessToken).replace("OPENID","openId"); String url = GET_USER_INFO_URL.replace("ACCESS_TOKEN",accessToken).replace("OPENID",openId);
String result = HttpKit.get(url); String result = HttpKit.get(url);
return result; return result;
}; };
@ -43,10 +75,4 @@ public class WechatUserUtil {
return wechatUser; return wechatUser;
} }
public static void main(String[] args) {
String token = "31_uTuXD_IEbECUYQch9JQxyI1qlLMEh-fRefmbjhVAaM0B6cwnGGBbbwZAYLDhKcQhRhZNekxmzO5YkfXWJQsdNmuetuI0lIAF7b6fcf1K_kf9uOyU_zxa7W3evv2cJD5m9fLyT_mr8Fz7laCeISTaAHAUEF";
String getUrl = GET_USER_LIST_URL.replace("ACCESS_TOKEN",token);
String result = HttpKit.get(getUrl);
System.out.println(result);
}
} }

@ -50,5 +50,4 @@ public class AccessTokenUtil {
} }

@ -1,17 +1,29 @@
package com.lq.wechat.util.token; package com.lq.wechat.util.token;
import com.lq.BaseTest;
import com.lq.entity.WechatInfo;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class AccessTokenUtilTest { public class AccessTokenUtilTest extends BaseTest {
@Autowired
private AccessTokenUtil accessTokenUtil;
@Test @Test
public void getAccessToken() { public void getAccessToken() {
String result = AccessTokenUtil.getAccessToken("wx76ca7130852c4baa","d87125562b8e60618bc7b3120dfe3583");
String result = AccessTokenUtil.getAccessToken("wx759184412abd6599","3197674021ad999f87273ed79a752212");
System.out.println(result); System.out.println(result);
} }
@Test @Test
public void getAccessToken1() { public void getAccessToken1() {
WechatInfo wechatInfo = new WechatInfo();
wechatInfo.setAppId("wx76ca7130852c4baa");
wechatInfo.setAppSecpet("d87125562b8e60618bc7b3120dfe3583");
wechatInfo.setWechatOpenId("gh_50ba8d7a5efc");
System.out.println("=================================");
String token = accessTokenUtil.getAccessToken(wechatInfo);
System.out.println("token:"+token);
} }
} }
Loading…
Cancel
Save