AccountMergeController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using FrameWork.SDKManager;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. /// <summary>
  8. /// 管理平台账号绑定
  9. /// </summary>
  10. public class AccountMergeController
  11. {
  12. /// <summary>
  13. /// 当要绑定的平台已存在回调
  14. /// </summary>
  15. public static CallBack<AccountMergeInfo2Client> OnMergeAccountExist;
  16. /// <summary>
  17. /// 最终绑定的结果回调
  18. /// </summary>
  19. public static CallBack<ConfirmMergeExistAccount2Client> OnConfirmMergeExistAccountCallback;
  20. /// <summary>
  21. /// 返回当前账户已经绑定的平台(包含当前登录平台)
  22. /// </summary>
  23. public static CallBack<List<LoginPlatform>> OnRequsetAreadyBindPlatformCallBack;
  24. private static List<LoginPlatform> areadyBindPlatform = new List<LoginPlatform>();
  25. [RuntimeInitializeOnLoadMethod]
  26. private static void Init()
  27. {
  28. GlobalEvent.AddTypeEvent<AccountMergeInfo2Client>(OnAccountMergeInfo);
  29. GlobalEvent.AddTypeEvent<ConfirmMergeExistAccount2Client>(OnConfirmMergeExistAccount);
  30. GlobalEvent.AddTypeEvent<RequsetAreadyBindPlatform2Client>(OnRequsetAreadyBindPlatform);
  31. LoginGameController.OnUserLogin += OnUserLogin;
  32. }
  33. private static void OnUserLogin(UserLogin2Client t)
  34. {
  35. RequsetAreadyBindPlatform();
  36. }
  37. #region 消息接收
  38. private static void OnRequsetAreadyBindPlatform(RequsetAreadyBindPlatform2Client e, object[] args)
  39. {
  40. areadyBindPlatform = e.areadyBindPlatforms;
  41. if (OnRequsetAreadyBindPlatformCallBack != null)
  42. {
  43. OnRequsetAreadyBindPlatformCallBack(e.areadyBindPlatforms);
  44. }
  45. }
  46. private static void OnConfirmMergeExistAccount(ConfirmMergeExistAccount2Client e, object[] args)
  47. {
  48. if(e.code==0)
  49. {
  50. if (areadyBindPlatform.Contains(e.loginType))
  51. Debug.LogError("已包含绑定平台:" + e.loginType);
  52. else
  53. areadyBindPlatform.Add(e.loginType);
  54. }
  55. else
  56. {
  57. Debug.LogError("绑定出错");
  58. }
  59. if (OnConfirmMergeExistAccountCallback != null)
  60. {
  61. OnConfirmMergeExistAccountCallback(e);
  62. }
  63. }
  64. private static void OnAccountMergeInfo(AccountMergeInfo2Client e, object[] args)
  65. {
  66. if (!e.alreadyExistAccount)
  67. return;
  68. Debug.Log("要绑定的账户已存在:" + e.mergeAccount.userID);
  69. if (OnMergeAccountExist != null)
  70. {
  71. OnMergeAccountExist(e);
  72. }
  73. }
  74. #endregion
  75. /// <summary>
  76. /// 请求哪些是已绑定的平台信息
  77. /// </summary>
  78. private static void RequsetAreadyBindPlatform()
  79. {
  80. RequsetAreadyBindPlatform2Server msg = new RequsetAreadyBindPlatform2Server();
  81. JsonMessageProcessingController.SendMessage(msg);
  82. }
  83. /// <summary>
  84. /// 返回当前账户已经绑定的平台(包含当前登录平台)
  85. /// </summary>
  86. /// <returns></returns>
  87. public static List<LoginPlatform> GetAreadyBindPlatform()
  88. {
  89. return areadyBindPlatform;
  90. }
  91. /// <summary>
  92. /// 判断是否能使用商店(规则是当前是游客登录,并且未绑定其他登录方式)
  93. /// </summary>
  94. /// <param name="loginType">当前登录方式</param>
  95. /// <returns></returns>
  96. public static bool CheckCanUseStore(LoginPlatform loginType)
  97. {
  98. if (areadyBindPlatform.Contains(loginType) && areadyBindPlatform.Count == 1 && loginType == LoginPlatform.Tourist)
  99. return false;
  100. return true;
  101. }
  102. /// <summary>
  103. /// 当要绑定的账户已存在,确认合并
  104. /// </summary>
  105. public static void ConfirmMerge(bool useCurrentAccount)
  106. {
  107. ConfirmMergeExistAccount2Server msg = new ConfirmMergeExistAccount2Server();
  108. msg.useCurrentAccount = useCurrentAccount;
  109. JsonMessageProcessingController.SendMessage(msg);
  110. }
  111. public static bool isWaiting = false;
  112. /// <summary>
  113. /// 请求绑定账户
  114. /// </summary>
  115. /// <param name="loginPlatform"></param>
  116. /// <param name="accountID"></param>
  117. /// <param name="pw"></param>
  118. public static void MergeLoginPlatform(LoginPlatform loginPlatform, string accountID = "", string pw = "")
  119. {
  120. if (isWaiting)
  121. {
  122. Debug.LogError("AccountMergeController => 等待sdk返回登录信息");
  123. return;
  124. }
  125. isWaiting = true;
  126. SDKManager.LoginCallBack += SDKLoginCallBack;
  127. string tag = "";
  128. if (loginPlatform == LoginPlatform.AccountLogin)
  129. {
  130. accountID = accountID.Trim();
  131. pw = pw.Trim();
  132. string pwMd5 = HDJ.Framework.Utils.MD5Utils.GetObjectMD5(pw);
  133. tag = accountID + "|" + pwMd5;
  134. }
  135. SDKManager.LoginByPlatform(loginPlatform, tag);
  136. }
  137. private static void SDKLoginCallBack(OnLoginInfo info)
  138. {
  139. isWaiting = false;
  140. SDKManager.LoginCallBack -= SDKLoginCallBack;
  141. if (info.isSuccess)
  142. {
  143. AccountMergeInfo2Server msg = AccountMergeInfo2Server.GetMessage(info.loginPlatform, info.accountId, info.password);
  144. JsonMessageProcessingController.SendMessage(msg);
  145. }
  146. else
  147. {
  148. //sdk登录失败
  149. if (OnConfirmMergeExistAccountCallback != null)
  150. {
  151. ConfirmMergeExistAccount2Client msg = new ConfirmMergeExistAccount2Client();
  152. msg.code = -1;
  153. msg.loginType = info.loginPlatform;
  154. OnConfirmMergeExistAccountCallback(msg);
  155. }
  156. }
  157. }
  158. }