enhance ChangeLinkAccount functionality

- Added LoginTypeToBindType function to map login types to binding types.
- Updated ChangeLinkAccount method to handle account linking and unbinding with appropriate error handling.
- Ensured proper messaging for success and failure cases during account changes.
- Cleaned temporary SNS information after account change attempts.
This commit is contained in:
2026-03-30 21:49:14 +08:00
parent 9f5151c54c
commit 6866b7fca9
9 changed files with 694 additions and 60 deletions

View File

@@ -7,23 +7,94 @@ import com.tuyoo.gamesdk.api.SDKAPI;
import com.tuyoo.gamesdk.api.SDKAPIConstant;
import com.tuyoo.gamesdk.api.SDKCallBack;
import com.tuyoo.gamesdk.api.SDKWrapper;
import com.tuyoo.gamesdk.login.model.SnsInfo;
import org.json.JSONException;
import org.json.JSONObject;
public class SnsTest {
static final String TAG = "SNSTEST";
static final String GOName = "SDKTest";
public static void Bind(String type, String guserId)
static SnsInfo tmpSnsInfo = null;
public static void Bind(String type, String userId)
{
SDKAPI.getIns().getSnsInfo(type, (snscode, snsInfo, snsmsg) ->{
if(snscode == SDKCallBack.CODE_SUCCESS && snsInfo !=null)
if(snscode == SDKCallBack.CODE_SUCCESS && snsInfo != null)
{
SDKAPI.getIns().bindBySnsId(snsInfo, guserId, (bindcode, bindmsg)->{
SDKAPI.getIns().bindBySnsId(snsInfo, userId, (bindcode, bindmsg)->{
if(bindcode == SDKCallBack.CODE_SUCCESS)
{
Log.i(TAG, "LinkSns success");
try {
JSONObject result = new JSONObject();
result.put("code", bindcode);
result.put("userId", userId);
JSONObject serverResponse = new JSONObject(bindmsg);
JSONObject userInfo = serverResponse.getJSONObject("result");
result.put("loginData", userInfo);
UnityPlayer.UnitySendMessage(GOName, "BindCallback", result.toString());
} catch (JSONException e) {
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
}
}
else if(bindcode == SDKCallBack.CODE_FAILED)
{
try
{
JSONObject bindMsgObj = new JSONObject(bindmsg);
JSONObject bindMsgResult = bindMsgObj.getJSONObject("result");
int bindMsgResultCode = bindMsgResult.getInt("code");
if(bindMsgResultCode == 4)
{
SDKAPI.getIns().checkSnsInfo(snsInfo, (checkcode, checkMsg ) -> {
if(checkcode != SDKCallBack.CODE_SUCCESS)
{
Log.w(TAG, checkMsg);
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
return;
}
try {
tmpSnsInfo = snsInfo;
JSONObject checkMsgObj = new JSONObject(checkMsg);
JSONObject checkResult = checkMsgObj.getJSONObject("result");
String bindInfo = checkResult.getString("bindInfo");
int bindUserId = checkResult.getInt("userId");
JSONObject callbackMsg = new JSONObject();
callbackMsg.put("code", -1);
callbackMsg.put("userId", bindUserId);
callbackMsg.put("bindInfo", bindInfo);
UnityPlayer.UnitySendMessage(GOName, "BindCallback", callbackMsg.toString());
} catch (JSONException e) {
Log.i(TAG, e.toString());
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
}
});
}
else
{
Log.w(TAG, bindmsg);
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
}
}
catch (JSONException e)
{
Log.i(TAG, e.toString());
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
}
}
else
{
Log.i(TAG, String.format("LinkSns failed, code: %d, msg: %s", bindcode, bindmsg));
Log.w(TAG, bindmsg);
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
}
});
}
@@ -34,8 +105,32 @@ public class SnsTest {
});
}
public static void Unbind(String type, int userId)
public static void ChangeBind(int unbindUserId, int bindUserId)
{
if(tmpSnsInfo == null)
{
Log.i(TAG, "tmpSnsInfo is null");
return ;
}
SDKAPI.getIns().unbindBySnsId(tmpSnsInfo, String.valueOf(unbindUserId), (unbindCode, unbindMsg) -> {
Log.i(TAG, "unbindBySnsId code: " + unbindCode + " msg: " + unbindMsg);
if(unbindCode == SDKCallBack.CODE_SUCCESS)
{
SDKAPI.getIns().bindBySnsId(tmpSnsInfo, String.valueOf(bindUserId), (bindCode, bindMsg) -> {
Log.i(TAG, "bindBySnsId code: " + bindCode + " msg: " + bindMsg);
});
}
});
}
public static void RmSns()
{
tmpSnsInfo = null;
}
public static void GetSns(String accountType)
{
Log.i(TAG, "GetSns accountType: " + accountType);
}
}