From dcf5f30e61363b957dc972f5ad77d5489b0ef98d Mon Sep 17 00:00:00 2001 From: skqing <569141948@qq.com> Date: Wed, 19 Jun 2019 09:51:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=A5=E4=B8=8B=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=20QQ=E7=99=BB=E5=BD=95=E6=9C=AA=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9C=9F=E6=AD=A3=E7=9A=84uuid=20=E9=83=A8=E5=88=86Re?= =?UTF-8?q?quest=E5=B0=81=E8=A3=85AuthUser=E6=97=B6=E7=BC=BA=E5=B0=91gende?= =?UTF-8?q?r=E5=B1=9E=E6=80=A7=20location=E5=B1=9E=E6=80=A7=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=96=B9=E5=BC=8F=E4=B8=8D=E4=B8=80=E8=87=B4=20?= =?UTF-8?q?=E9=92=89=E9=92=89=E7=99=BB=E5=BD=95=E8=8E=B7=E5=8F=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF=E5=BF=BD=E7=95=A5=E4=BA=86openid=20?= =?UTF-8?q?=E9=92=89=E9=92=89=E7=99=BB=E5=BD=95=E5=9B=9E=E8=B0=83=E6=8A=A5?= =?UTF-8?q?=E9=94=99=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhyd/oauth/request/AuthAlipayRequest.java | 6 +++- .../oauth/request/AuthDingTalkRequest.java | 6 ++-- .../zhyd/oauth/request/AuthDouyinRequest.java | 6 ++-- .../me/zhyd/oauth/request/AuthMiRequest.java | 6 ++-- .../me/zhyd/oauth/request/AuthQqRequest.java | 31 ++++++++++++++----- .../java/me/zhyd/oauth/utils/UrlBuilder.java | 2 +- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java index 4e2e952..a1ed068 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java @@ -69,12 +69,16 @@ public class AuthAlipayRequest extends BaseAuthRequest { } String province = response.getProvince(), city = response.getCity(); + String location = province; + if (!StringUtils.isEmpty(city)) { + location = location + "-"+city; + } return AuthUser.builder() .uuid(response.getUserId()) .username(StringUtils.isEmpty(response.getUserName()) ? response.getNickName() : response.getUserName()) .nickname(response.getNickName()) .avatar(response.getAvatar()) - .location(String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city)) + .location(location) .gender(AuthUserGender.getRealGender(response.getGender())) .token(authToken) .source(AuthSource.ALIPAY) diff --git a/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java index 6470c50..7574a98 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java @@ -6,10 +6,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.exception.AuthException; -import me.zhyd.oauth.model.AuthDingTalkErrorCode; -import me.zhyd.oauth.model.AuthSource; -import me.zhyd.oauth.model.AuthToken; -import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.model.*; import me.zhyd.oauth.utils.GlobalAuthUtil; import me.zhyd.oauth.utils.UrlBuilder; @@ -58,6 +55,7 @@ public class AuthDingTalkRequest extends BaseAuthRequest { .uuid(object.getString("unionid")) .nickname(object.getString("nick")) .username(object.getString("nick")) + .gender(AuthUserGender.UNKNOW) .source(AuthSource.DINGTALK) .token(token) .build(); diff --git a/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java index 06ddaa5..69754e5 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java @@ -5,10 +5,7 @@ import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.exception.AuthException; -import me.zhyd.oauth.model.AuthResponse; -import me.zhyd.oauth.model.AuthSource; -import me.zhyd.oauth.model.AuthToken; -import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.model.*; import me.zhyd.oauth.utils.UrlBuilder; @@ -45,6 +42,7 @@ public class AuthDouyinRequest extends BaseAuthRequest { .username(userInfoObject.getString("nickname")) .nickname(userInfoObject.getString("nickname")) .avatar(userInfoObject.getString("avatar")) + .gender(AuthUserGender.UNKNOW) .token(authToken) .source(AuthSource.DOUYIN) .build(); diff --git a/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java b/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java index e86c2f0..222729a 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java @@ -6,10 +6,7 @@ import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.exception.AuthException; -import me.zhyd.oauth.model.AuthResponse; -import me.zhyd.oauth.model.AuthSource; -import me.zhyd.oauth.model.AuthToken; -import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.model.*; import me.zhyd.oauth.utils.UrlBuilder; import java.text.MessageFormat; @@ -74,6 +71,7 @@ public class AuthMiRequest extends BaseAuthRequest { .nickname(user.getString("miliaoNick")) .avatar(user.getString("miliaoIcon")) .email(user.getString("mail")) + .gender(AuthUserGender.UNKNOW) .token(authToken) .source(AuthSource.MI) .build(); diff --git a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java index a7c0408..d043404 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java @@ -48,8 +48,15 @@ public class AuthQqRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - String openId = this.getOpenId(accessToken); - HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(config.getClientId(), accessToken, openId)) + String openIdStr = this.getOpenId(accessToken); + String[] openIdArr = openIdStr.split("\\|"); + authToken.setOpenId(openIdArr[0]); + String uuid = authToken.getOpenId(); + if (openIdArr.length == 2) { + uuid = openIdArr[1]; + } + + HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(config.getClientId(), accessToken, authToken.getOpenId())) .execute(); JSONObject object = JSONObject.parseObject(response.body()); if (object.getIntValue("ret") != 0) { @@ -64,7 +71,7 @@ public class AuthQqRequest extends BaseAuthRequest { .nickname(object.getString("nickname")) .avatar(avatar) .location(object.getString("province") + "-" + object.getString("city")) - .uuid(openId) + .uuid(uuid) .gender(AuthUserGender.getRealGender(object.getString("gender"))) .token(authToken) .source(AuthSource.QQ) @@ -78,13 +85,21 @@ public class AuthQqRequest extends BaseAuthRequest { String body = response.body(); String removePrefix = StrUtil.replace(body, "callback(", ""); String removeSuffix = StrUtil.replace(removePrefix, ");", ""); - String openId = StrUtil.trim(removeSuffix); - JSONObject object = JSONObject.parseObject(openId); + String jsonStr = StrUtil.trim(removeSuffix); + JSONObject object = JSONObject.parseObject(jsonStr); + StringBuffer sb = new StringBuffer(); if (object.containsKey("openid")) { - return object.getString("openid"); + sb.append(object.getString("openid")); + sb.append("|"); + } else { + throw new AuthException("Invalid openId"); } - throw new AuthException("Invalid openId"); + if (object.containsKey("unionid")) { + sb.append(object.getString("unionid")); + } + return sb.toString(); } - throw new AuthException("Invalid openId"); + + throw new AuthException("request error"); } } diff --git a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java index a6bebde..db66917 100644 --- a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java +++ b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java @@ -58,7 +58,7 @@ public class UrlBuilder { private static final String QQ_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; private static final String QQ_USER_INFO_PATTERN = "{0}?oauth_consumer_key={1}&access_token={2}&openid={3}"; private static final String QQ_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"; - private static final String QQ_OPENID_PATTERN = "{0}?access_token={1}"; + private static final String QQ_OPENID_PATTERN = "{0}?access_token={1}&unionid=1"; private static final String WECHAT_AUTHORIZE_PATTERN = "{0}?appid={1}&redirect_uri={2}&response_type=code&scope=snsapi_login&state={3}#wechat_redirect"; private static final String WECHAT_ACCESS_TOKEN_PATTERN = "{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code"; -- GitLab