From 6962861132b218f0cb83fcb69279a0436c365ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Wed, 23 Sep 2015 13:19:54 +0800 Subject: [PATCH] --- .../service/weixin/WeiXinMPService.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src-plugin/com/wentch/redkale/service/weixin/WeiXinMPService.java b/src-plugin/com/wentch/redkale/service/weixin/WeiXinMPService.java index a32598b49..1d119a024 100644 --- a/src-plugin/com/wentch/redkale/service/weixin/WeiXinMPService.java +++ b/src-plugin/com/wentch/redkale/service/weixin/WeiXinMPService.java @@ -21,7 +21,7 @@ import javax.annotation.*; * * @author zhangjx */ -public class WeiXinMPService implements Service{ +public class WeiXinMPService implements Service { protected static final Type MAPTYPE = new TypeToken>() { }.getType(); @@ -52,9 +52,9 @@ public class WeiXinMPService implements Service{ } //-----------------------------------微信服务号接口---------------------------------------------------------- - public RetResult getMPWxunionid(String appid, String code) { + public RetResult getMPWxunionidByCode(String appid, String code) { try { - Map wxmap = getMPUserToken(appid, code); + Map wxmap = getMPUserTokenByCode(appid, code); final String unionid = wxmap.get("unionid"); if (unionid != null && !unionid.isEmpty()) return new RetResult<>(unionid); return new RetResult<>(1011002); @@ -63,15 +63,19 @@ public class WeiXinMPService implements Service{ } } - public Map getMPUserToken(String appid, String code) throws IOException { + public Map getMPUserTokenByCode(String appid, String code) throws IOException { String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + mpsecrets.get(appid) + "&code=" + code + "&grant_type=authorization_code"; String json = getHttpContent(url); if (finest) logger.finest(url + "--->" + json); Map jsonmap = convert.convertFrom(MAPTYPE, json); - url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + jsonmap.get("access_token") + "&openid=" + jsonmap.get("openid"); - json = getHttpContent(url); + return getMPUserTokenByOpenid(jsonmap.get("access_token"), jsonmap.get("openid")); + } + + public Map getMPUserTokenByOpenid(String access_token, String openid) throws IOException { + String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid; + String json = getHttpContent(url); if (finest) logger.finest(url + "--->" + json); - jsonmap = convert.convertFrom(MAPTYPE, json.replaceFirst("\\[.*\\]", "null")); + Map jsonmap = convert.convertFrom(MAPTYPE, json.replaceFirst("\\[.*\\]", "null")); return jsonmap; }