Files
tysdk-intergration/sdk-intergration/Assets/Plugins/Android/SnsTest.java
tech 6866b7fca9 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.
2026-03-30 22:11:16 +08:00

137 lines
5.9 KiB
Java

package com.unity3d.player;
import android.util.Log;
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";
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)
{
SDKAPI.getIns().bindBySnsId(snsInfo, userId, (bindcode, bindmsg)->{
if(bindcode == SDKCallBack.CODE_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.w(TAG, bindmsg);
String msg = "{\"code\":1, \"userId\": 0}";
UnityPlayer.UnitySendMessage(GOName, "BindCallback", msg);
}
});
}
else
{
Log.i(TAG, "getSnsInfo failed");
}
});
}
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);
}
}