From ab8c24ee5cc92cc1ed660680885ed5b110de5024 Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Tue, 24 Dec 2019 18:47:57 +0800 Subject: [PATCH] =?UTF-8?q?:beers:=20=E8=B0=83=E6=95=B4=E8=BF=87=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E6=80=A7=E5=88=AB=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/oauth/wechat_open.md | 2 +- .../java/me/zhyd/oauth/enums/AuthUserGender.java | 16 ++++++---------- .../zhyd/oauth/request/AuthWeChatMpRequest.java | 14 +------------- 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/docs/oauth/wechat_open.md b/docs/oauth/wechat_open.md index bd67ec7..e4a7af4 100644 --- a/docs/oauth/wechat_open.md +++ b/docs/oauth/wechat_open.md @@ -141,7 +141,7 @@ public class RestAuthController { "email": null, "remark": null, "gender": "MALE", - "source": "WECHAT", + "source": "WECHAT_OPEN", "token": { "accessToken": "28_Bzvt4UxxxxFElOxxxxgTqIJmSE5LxB2Az7stFqYVnFoIhL7RbevA0Urc", "expireIn": 7200, diff --git a/src/main/java/me/zhyd/oauth/enums/AuthUserGender.java b/src/main/java/me/zhyd/oauth/enums/AuthUserGender.java index af3bd1a..348d520 100644 --- a/src/main/java/me/zhyd/oauth/enums/AuthUserGender.java +++ b/src/main/java/me/zhyd/oauth/enums/AuthUserGender.java @@ -18,25 +18,21 @@ public enum AuthUserGender { * MALE/FAMALE为正常值,通过{@link AuthUserGender#getRealGender(String)}方法获取真实的性别 * UNKNOWN为容错值,部分平台不会返回用户性别,为了方便统一,使用UNKNOWN标记所有未知或不可测的用户性别信息 */ - MALE(1, "男"), - FEMALE(0, "女"), - UNKNOWN(-1, "未知"); + MALE("1", "男"), + FEMALE("0", "女"), + UNKNOWN("-1", "未知"); - private int code; + private String code; private String desc; public static AuthUserGender getRealGender(String code) { - if (code == null) { + if (null == code || UNKNOWN.getCode().equals(code)) { return UNKNOWN; } String[] males = {"m", "男", "1", "male"}; if (Arrays.asList(males).contains(code.toLowerCase())) { return MALE; } - String[] females = {"f", "女", "0", "female"}; - if (Arrays.asList(females).contains(code.toLowerCase())) { - return FEMALE; - } - return UNKNOWN; + return FEMALE; } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthWeChatMpRequest.java b/src/main/java/me/zhyd/oauth/request/AuthWeChatMpRequest.java index ac67094..35a55e8 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthWeChatMpRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthWeChatMpRequest.java @@ -57,25 +57,13 @@ public class AuthWeChatMpRequest extends AuthDefaultRequest { authToken.setUnionId(object.getString("unionid")); } - AuthUserGender sex; - switch (object.getString("sex")) { - case "1": - sex = AuthUserGender.MALE; - break; - case "2": - sex = AuthUserGender.FEMALE; - break; - default: - sex = AuthUserGender.UNKNOWN; - } - return AuthUser.builder() .username(object.getString("nickname")) .nickname(object.getString("nickname")) .avatar(object.getString("headimgurl")) .location(location) .uuid(openId) - .gender(sex) + .gender(AuthUserGender.getRealGender(object.getString("sex"))) .token(authToken) .source(source.toString()) .build(); -- GitLab