refactor(tysdk): 重构SDK登录和绑定功能

- 重构iOS构建参数,添加调试构建选项
- 实现iOS调试构建菜单项,支持开发选项和调试连接
- 修复Android SDKManager中的snsInfo临时存储逻辑
- 添加清理临时snsInfo的方法实现
- 在iOS接口中添加登录类型存储和sns信息重新登录功能
- 重构账号绑定逻辑,统一处理不同平台的绑定流程
- 添加绑定失败时的登出回调处理
- 更新UnityBridgeFunc中的原生接口绑定
- 在测试脚本中添加初始化时清理临时sns信息
This commit is contained in:
2026-01-07 15:08:04 +08:00
parent cb2ff22a89
commit 4d12bdf0ec
6 changed files with 208 additions and 59 deletions

View File

@@ -9,6 +9,9 @@
#define TOKEN_ERROR @"-8"
extern "C" {
// Store last login type for SNS re-login
static NSInteger g_snsLoginType = -1;
NSString* DicTojsonString(NSDictionary *dict)
{
try {
@@ -33,11 +36,15 @@ extern "C" {
[resultDict setValue:[NSString stringWithFormat:@"%ld", (long)resp.errCode] forKey:@"code"];
[resultDict setValue:resp.errStr ? resp.errStr : @"" forKey:@"errStr"];
if(resp.errCode == XYSuccess){
[resultDict setValue:resp.respObj forKey:@"respObj"];
NSDictionary *respObjDict = (NSDictionary*)resp.respObj;
NSDictionary *resultDictData = (NSDictionary*)[respObjDict objectForKey:@"result"];
NSMutableDictionary *loginData = [resultDictData mutableCopy];
[resultDict setValue:loginData forKey:@"loginData"];
}
NSString *result = DicTojsonString(resultDict);
const char* param = AllocCString(result);
UnitySendMessage("TYSdkFacade","LoginResult", param);
g_snsLoginType = -1;
}
//更新登录支付域名
@@ -85,7 +92,6 @@ extern "C" {
{
XYLoginReq *req = [XYLoginReq initWithLoginType:XYLoginWithGoogle];
[XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) {
NSLog(@"TYSdkInterface UnityLoginByGoogle: %@", resp);
ProcessLoginResult(resp);
}];
}
@@ -113,9 +119,64 @@ extern "C" {
}
char* LoginTypeToStr(XYLoginType loginType)
{
switch (loginType) {
case XYLoginWithFB:
return AllocCString(@"hwFacebook");
case XYLoginWithApple:
return AllocCString(@"Apple");
case XYLoginWithGoogle:
return AllocCString(@"hwGoogle");
default:
return nil;
}
}
NSInteger BindTypeToLoginType(XYBindType bType)
{
switch (bType) {
case XYBindApple:
return XYLoginWithApple;
case XYBindFB:
return XYLoginWithFB;
case XYBindGoodle:
return XYLoginWithGoogle;
default:
return -1;
}
}
const char* UnityLoginBySnsInfo()
{
NSLog(@"TYSdkInterface UnityLoginBySnsInfo");
if(g_snsLoginType == -1)
{
NSLog(@"TYSdkInterface UnityLoginBySnsInfo - No stored login type");
UnitySendMessage("TYSdkFacade","LoginResult", "{\"code\":1}");
// Return empty string
return "";
}
NSLog(@"TYSdkInterface UnityLoginBySnsInfo - Re-login with type: %s", LoginTypeToStr((XYLoginType)g_snsLoginType));
// Re-login using the stored type
XYLoginReq *req = [XYLoginReq initWithLoginType:(XYLoginType)g_snsLoginType];
[XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) {
NSLog(@"TYSdkInterface UnityLoginBySnsInfo result: %@", resp);
ProcessLoginResult(resp);
}];
// Return the login type as C string
return LoginTypeToStr((XYLoginType)g_snsLoginType);
}
void UnityLoginOut()
{
NSLog(@"TYSdkInterface UnityLoginOut");
[XYApi XYLogoutWithCompletion:^(XYResp * _Nonnull resp) {
if(resp.errCode == XYSuccess)
{
@@ -127,7 +188,7 @@ extern "C" {
NSLog(@"TYSdkInterface UnityLoginOut Faild");
NSLog(@"Error Code:%ld msg:%@", resp.errCode, resp.errStr);
}
}];
}
@@ -234,27 +295,85 @@ extern "C" {
[gaSDK track:dic type:GAEventType(type) event:nsEvent];
}
}
void LinkGoogle(const int userId)
void CleanLinkTmpSnsInfo()
{
NSLog(@"TYSdkInterface LinkGoogle");
NSDictionary * userDic = [XYApi XYGetCurrentUserDict];
XYBindReq *bindReq = [XYBindReq initBindWithPlatform:XYBindGoodle UserId: [ [userDic objectForKey:@"userId"] unsignedIntegerValue]];
g_snsLoginType = -1;
}
// Helper function to handle account linking
typedef void(^LogoutCallback)(void);
void LinkAccountWithPlatform(NSString *platformName, XYBindType bindType, const int userId, LogoutCallback logoutCallback)
{
NSLog(@"TYSdkInterface Link%@ userId:%d", platformName, userId);
XYBindReq *bindReq = [XYBindReq initBindWithPlatform:bindType UserId:(NSUInteger)userId];
[XYApi XYBindByBindReq:bindReq isUnbindGuest:@"false" completionHandler:^(XYResp * _Nonnull resp) {
NSMutableDictionary *resultDict = [@{} mutableCopy];
if(resp.errCode == XYSuccess)
{
NSLog(@"TYSdkInterface LinkGoogle Success");
UnitySendMessage("TYSdkFacade","LinkAccoutResult", "1");
NSLog(@"TYSdkInterface Link%@ Success", platformName);
NSDictionary *respObjDict = (NSDictionary*)resp.respObj;
NSDictionary *resultDictData = (NSDictionary*)[respObjDict objectForKey:@"result"];
NSMutableDictionary *loginData = [resultDictData mutableCopy];
[resultDict setValue:[NSString stringWithFormat:@"%ld", (long)resp.errCode] forKey:@"code"];
[resultDict setValue:[NSString stringWithFormat:@"%d", userId] forKey:@"userId"];
[resultDict setValue:loginData forKey:@"loginData"];
NSString *result = DicTojsonString(resultDict);
const char* param = AllocCString(result);
UnitySendMessage("TYSdkFacade","LinkAccoutResult", param);
}
else if(resp.errCode == XYErrCodeHadBind)
{
[XYApi CheckUserExist:bindType completion:^(XYResp * _Nonnull snscheckResp) {
if(snscheckResp.errCode != 0)
{
[resultDict setValue:@"1" forKey:@"code"];
[resultDict setValue:@"0" forKey:@"userId"];
NSString *result = DicTojsonString(resultDict);
const char* param = AllocCString(result);
UnitySendMessage("TYSdkFacade","LinkAccoutResult", param);
}
else
{
g_snsLoginType = BindTypeToLoginType(bindType);
NSDictionary *respObjDict = (NSDictionary*)snscheckResp.respObj;
[resultDict setValue:@"-1" forKey:@"code"];
[resultDict setValue:[respObjDict objectForKey:@"bindInfo"] forKey:@"bindInfo"];
[resultDict setValue:[respObjDict objectForKey:@"userId"] forKey:@"userId"];
NSString *result = DicTojsonString(resultDict);
const char* param = AllocCString(result);
UnitySendMessage("TYSdkFacade","LinkAccoutResult", param);
}
}];
}
else
{
NSLog(@"TYSdkInterface LinkGoogle Faild");
UnityLogoutGoogle();
UnitySendMessage("TYSdkFacade","LinkAccoutResult", "0");
NSLog(@"TYSdkInterface Link%@ Failed errCode:%ld", platformName, (long)resp.errCode);
if (logoutCallback) {
logoutCallback();
}
[resultDict setValue:@"1" forKey:@"code"];
[resultDict setValue:@"0" forKey:@"userId"];
NSString *result = DicTojsonString(resultDict);
const char* param = AllocCString(result);
UnitySendMessage("TYSdkFacade","LinkAccoutResult", param);
}
}];
}
void LinkGoogle(const int userId)
{
LinkAccountWithPlatform(@"Google", XYBindGoodle, userId, ^{
UnityLogoutGoogle();
});
}
void IsLinkedGoogle()
{
[XYApi CheckUserExist:XYBindGoodle completion:^(XYResp * _Nonnull resp) {
@@ -275,23 +394,9 @@ extern "C" {
void LinkFacebook(const int userId)
{
NSLog(@"TYSdkInterface LinkFacebook");
NSDictionary * userDic = [XYApi XYGetCurrentUserDict];
XYBindReq *bindReq = [XYBindReq initBindWithPlatform:XYBindFB UserId: [ [userDic objectForKey:@"userId"] unsignedIntegerValue]];
[XYApi XYBindByBindReq:bindReq isUnbindGuest:@"false" completionHandler:^(XYResp * _Nonnull resp) {
if(resp.errCode == XYSuccess)
{
NSLog(@"TYSdkInterface LinkFacebook Success");
UnitySendMessage("TYSdkFacade","LinkAccoutResult", "1");
}
else
{
NSLog(@"TYSdkInterface LinkFacebook Faild");
UnityLogoutFacebook();
UnitySendMessage("TYSdkFacade","LinkAccoutResult", "0");
}
}];
LinkAccountWithPlatform(@"Facebook", XYBindFB, userId, ^{
UnityLogoutFacebook();
});
}
void IsLinkedFacebook()
@@ -312,26 +417,11 @@ extern "C" {
}
void LinkApple()
void LinkApple(const int userId)
{
NSLog(@"TYSdkInterface LinkApple");
NSDictionary * userDic = [XYApi XYGetCurrentUserDict];
XYBindReq *bindReq = [XYBindReq initBindWithPlatform:XYBindApple UserId: [ [userDic objectForKey:@"userId"] unsignedIntegerValue]];
[XYApi XYBindByBindReq:bindReq isUnbindGuest:@"false" completionHandler:^(XYResp * _Nonnull resp) {
if(resp.errCode == XYSuccess)
{
NSLog(@"TYSdkInterface LinkApple Success");
UnitySendMessage("TYSdkFacade","LinkAccoutResult", "1");
}
else
{
NSLog(@"TYSdkInterface LinkApple Faild");
UnityLogoutApple();
UnitySendMessage("TYSdkFacade","LinkAccoutResult", "0");
}
}];
LinkAccountWithPlatform(@"Apple", XYBindApple, userId, ^{
UnityLogoutApple();
});
}
void IsLinkedApple()