1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| @Component public class WeiXinCommonUtil {
@Autowired private HttpRequestUtil httpRequestUtil;
private static String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?" + "grant_type=client_credential&appid=APPID&secret=APPSECRET"; private static String code2SessionUrl= "https://api.weixin.qq.com/sns/jscode2session?" + "appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
public AccessToken getToken(String appid, String appsecret){ AccessToken token=null; String requestUrl=accessTokenUrl.replace("APPID", appid).replace("APPSECRET", appsecret); JSONObject json =httpRequestUtil.httpsRequest(requestUrl,"GET",null); System.out.println("获取到的json格式的Token为:"+json); if (json!=null){ try{ token = new AccessToken(); token.setAccess_token(json.getString("access_token")); token.setExpires_in(json.getInt("expires_in")); } catch (Exception e){ token=null; e.printStackTrace(); System.out.println("系统异常!"); } }else { token=null; System.out.println("Fail:" + json); } return token; }
public UserLoginVO getUserLogin(String appid, String appsecret, String js_code){ UserLoginVO userLoginVO = null; String requestUrl = code2SessionUrl .replace("APPID", appid) .replace("SECRET", appsecret) .replace("JSCODE",js_code); JSONObject json =httpRequestUtil.httpsRequest(requestUrl,"GET",null); System.out.println("获取到的json格式的Token为:"+json); if (json!=null){ try{ userLoginVO = new UserLoginVO(); userLoginVO.setOpen_id(json.getString("openid")); userLoginVO.setSession_key(json.getString("session_key")); }catch (Exception e){ userLoginVO = null; e.printStackTrace(); System.out.println("系统异常!"); } }else { userLoginVO = null; System.out.println("Fail:" + json); } return userLoginVO; } }
|