Skip to content

Commit 34bcdaf

Browse files
committed
refactor(WeChat登录): 提取成功登录逻辑到独立方法
将微信登录成功后的处理逻辑提取到独立的Success方法中,提高代码复用性 添加已授权检查,避免重复授权
1 parent a57d6d3 commit 34bcdaf

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

Runtime/WeChatLoginManager.cs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,7 @@ private void OnAuthEventArgs(object sender, GameEventArgs e)
9090
return;
9191
}
9292

93-
var weChatLoginSuccess = new WeChatLoginSuccess();
94-
var authInfo = _shareSDK.GetAuthInfo(PlatformType.WeChat);
95-
Log.Debug(authInfo);
96-
if (eventArgs.Data != null)
97-
{
98-
if (eventArgs.Data.ContainsKey("nickname"))
99-
{
100-
weChatLoginSuccess.NickName = eventArgs.Data["nickname"].ToString();
101-
}
102-
103-
if (eventArgs.Data.ContainsKey("unionid"))
104-
{
105-
weChatLoginSuccess.UnionId = eventArgs.Data["unionid"].ToString();
106-
}
107-
108-
if (eventArgs.Data.ContainsKey("openid"))
109-
{
110-
weChatLoginSuccess.OpenId = eventArgs.Data["openid"].ToString();
111-
}
112-
113-
if (eventArgs.Data.ContainsKey("headimgurl"))
114-
{
115-
weChatLoginSuccess.PhotoUrl = eventArgs.Data["headimgurl"].ToString();
116-
}
117-
}
118-
119-
_loginSuccess.Invoke(weChatLoginSuccess);
93+
Success();
12094
}
12195
else
12296
{
@@ -125,6 +99,37 @@ private void OnAuthEventArgs(object sender, GameEventArgs e)
12599
}
126100
}
127101

102+
private void Success()
103+
{
104+
Hashtable authInfo = _shareSDK.GetAuthInfo(PlatformType.WeChat);
105+
Log.Debug(authInfo);
106+
var weChatLoginSuccess = new WeChatLoginSuccess();
107+
if (authInfo != null)
108+
{
109+
if (authInfo.ContainsKey("nickname"))
110+
{
111+
weChatLoginSuccess.NickName = authInfo["nickname"].ToString();
112+
}
113+
114+
if (authInfo.ContainsKey("unionid"))
115+
{
116+
weChatLoginSuccess.UnionId = authInfo["unionid"].ToString();
117+
}
118+
119+
if (authInfo.ContainsKey("openid"))
120+
{
121+
weChatLoginSuccess.OpenId = authInfo["openid"].ToString();
122+
}
123+
124+
if (authInfo.ContainsKey("headimgurl"))
125+
{
126+
weChatLoginSuccess.PhotoUrl = authInfo["headimgurl"].ToString();
127+
}
128+
}
129+
130+
_loginSuccess?.Invoke(weChatLoginSuccess);
131+
}
132+
128133
private Action<WeChatLoginSuccess> _loginSuccess;
129134
private Action<int> _loginFail;
130135

@@ -137,9 +142,13 @@ public void Login(Action<WeChatLoginSuccess> loginSuccess, Action<int> loginFail
137142
_loginSuccess?.Invoke(new WeChatLoginSuccess() { NickName = "test", OpenId = SystemInfo.deviceUniqueIdentifier, PhotoUrl = "test", UnionId = SystemInfo.deviceUniqueIdentifier });
138143
return;
139144
#endif
145+
if (_shareSDK.IsAuthorized(PlatformType.WeChat))
146+
{
147+
Success();
148+
return;
149+
}
150+
140151
_shareSDK.Authorize(PlatformType.WeChat);
141-
var authInfo = _shareSDK.GetAuthInfo(PlatformType.WeChat);
142-
Log.Debug(authInfo);
143152
}
144153

145154
[UnityEngine.Scripting.Preserve]

0 commit comments

Comments
 (0)