ThirdCloudHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Module
  4. {
  5. [Serializable]
  6. public class ThirdUserInfoData
  7. {
  8. public string accountId;
  9. public int disabled;
  10. public string email;
  11. public long gmtCreate;
  12. public bool isVoiceAuthed;
  13. public string mobile;
  14. public string regionCode;
  15. public string source;
  16. public int status;
  17. public int type;
  18. public string userId;
  19. public string userName;
  20. public bool voiceAuthed;
  21. public string password;
  22. public string headIcon;
  23. public string userType;
  24. public string idCard;
  25. public string createDateLong;
  26. public string agreement;
  27. public override string ToString()
  28. {
  29. return JsonUtility.ToJson(this);
  30. }
  31. }
  32. public class ThirdCloudHelper : MonoSingleton<ThirdCloudHelper>
  33. {
  34. private Action<ThirdUserInfoData> success;
  35. private Action<string> getTokenSuccess;
  36. private Action<string> error;
  37. protected override void Awake()
  38. {
  39. base.Awake();
  40. this.gameObject.name = "ThirdCloudHelper";
  41. }
  42. private void GetToken(string key, string security)
  43. {
  44. CallBridge.CallAndroid(Request.Build()
  45. .Name("ThirdCloud.getThreeCloudToken")
  46. .Param("key", key)
  47. .Param("security", security)
  48. .AndroidCallback(CallBridge.CreateCallback(this.gameObject.name, "OnGetTokenCallBack")));
  49. }
  50. private void GetUserInfo(string token)
  51. {
  52. CallBridge.CallAndroid(Request.Build()
  53. .Name("ThirdCloud.getUserInfo")
  54. .Param("token", token)
  55. .AndroidCallback(CallBridge.CreateCallback(this.gameObject.name, "OnGetUserInfoCallBack")));
  56. }
  57. private void OnGetTokenCallBack(string data)
  58. {
  59. if (!string.IsNullOrEmpty(data) && data.Contains("success"))
  60. {
  61. int index = data.IndexOf('-') + 1;
  62. string token = data.Substring(index, data.Length - index);
  63. RKLog.Info("====ThirdCloudHelper==== GetToken Success :" + token);
  64. getTokenSuccess?.Invoke(token);
  65. GetUserInfo(token);
  66. }
  67. else
  68. {
  69. RKLog.Info("====ThirdCloudHelper==== GetToken Faild :" + data);
  70. error?.Invoke(data);
  71. }
  72. }
  73. private void OnGetUserInfoCallBack(string data)
  74. {
  75. if (!string.IsNullOrEmpty(data) && data.Contains("success"))
  76. {
  77. int index = data.IndexOf('-') + 1;
  78. string userInfo = data.Substring(index, data.Length - index);
  79. RKLog.Info("====ThirdCloudHelper==== GetUserInfo Success :" + userInfo);
  80. success?.Invoke(JsonUtility.FromJson<ThirdUserInfoData>(userInfo));
  81. }
  82. else
  83. {
  84. RKLog.Info("====ThirdCloudHelper==== GetUserInfo Faild :" + data);
  85. error?.Invoke(data);
  86. }
  87. }
  88. #region 对外接口
  89. public void Init()
  90. {
  91. new AndroidJavaObject("com.rokid.uxr.thirdcloud.ThirdCloud");
  92. CallBridge.CallAndroid(Request.Build()
  93. .Name("ThirdCloud.init"));
  94. }
  95. public void GetToken(string key, string security, Action<string> success, Action<string> error)
  96. {
  97. this.getTokenSuccess = success;
  98. this.error = error;
  99. CallBridge.CallAndroid(Request.Build()
  100. .Name("ThirdCloud.getThreeCloudToken")
  101. .Param("key", key)
  102. .Param("security", security)
  103. .AndroidCallback(CallBridge.CreateCallback(this.gameObject.name, "OnGetTokenCallBack")));
  104. }
  105. public void GetUserInfo(string token, Action<ThirdUserInfoData> success, Action<string> error)
  106. {
  107. this.success = success;
  108. this.error = error;
  109. GetUserInfo(token);
  110. }
  111. /// <summary>
  112. /// 获取用户接口
  113. /// </summary>
  114. /// <param name="key">外部传入需要开发者申请</param>
  115. /// <param name="security">外部传入需要开发者申请</param>
  116. /// <param name="success"></param>
  117. /// <param name="error"></param>
  118. public void GetUserInfo(string key, string security, Action<ThirdUserInfoData> success, Action<string> error)
  119. {
  120. this.success = success;
  121. this.error = error;
  122. GetToken(key, security);
  123. }
  124. public void TestEnv(bool isTest)
  125. {
  126. CallBridge.CallAndroid(Request.Build()
  127. .Name("ThirdCloud.testEnv")
  128. .Param("isTest", isTest));
  129. }
  130. #endregion
  131. }
  132. }