AppleLoginHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using AppleAuth;
  5. using AppleAuth.Enums;
  6. using AppleAuth.Extensions;
  7. using AppleAuth.Interfaces;
  8. using AppleAuth.Native;
  9. using Newtonsoft.Json;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. using UnityEngine.UI;
  13. public class AppleLoginHelper : MonoBehaviour
  14. {
  15. public static AppleLoginHelper ins; //单例记录
  16. private const string AppleUserIdKey = "AppleUserId";
  17. private IAppleAuthManager _appleAuthManager;
  18. bool _bSupportedPlatform ;
  19. public bool bSupportedPlatform
  20. { // 公共属性
  21. get { return this._appleAuthManager == null?false:true; } // getter返回字段的值
  22. set { _bSupportedPlatform = value; } // setter将传入的值赋给字段
  23. }
  24. private void Awake()
  25. {
  26. if (CommonConfig.StandaloneMode) return;
  27. string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  28. Debug.Log("AppleLoginHelper当前场景名称: " + sceneName);
  29. DontDestroyOnLoad(this);
  30. gameObject.name = "AppleLoginHelper";
  31. ins = this;
  32. }
  33. private void Start()
  34. {
  35. // If the current platform is supported
  36. if (AppleAuthManager.IsCurrentPlatformSupported)
  37. {
  38. // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  39. var deserializer = new PayloadDeserializer();
  40. // Creates an Apple Authentication manager with the deserializer
  41. this._appleAuthManager = new AppleAuthManager(deserializer);
  42. }
  43. }
  44. private void Update()
  45. {
  46. // Updates the AppleAuthManager instance to execute
  47. // pending callbacks inside Unity's execution loop
  48. if (this._appleAuthManager != null)
  49. {
  50. this._appleAuthManager.Update();
  51. }
  52. }
  53. //使用苹果正常登录
  54. public void SignInWithAppleButtonPressed()
  55. {
  56. this.SignInWithApple();
  57. }
  58. //删除账号
  59. public void SignInWithAppleButtonPressedDelete(Action success,Action fail)
  60. {
  61. this.DeleteSignInWithApple(success, fail);
  62. }
  63. private void SetupLoginMenuForSignInWithApple()
  64. {
  65. }
  66. private void SignInWithApple()
  67. {
  68. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  69. this._appleAuthManager.LoginWithAppleId(
  70. loginArgs,
  71. credential =>
  72. {
  73. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  74. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  75. //this.onAppleData(credential.User, credential);
  76. var appleIdCredential = credential as IAppleIDCredential;
  77. if (appleIdCredential != null)
  78. {
  79. //if (appleIdCredential.IdentityToken != null)
  80. //{
  81. // var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  82. // Debug.Log("11===identityToken======= " + identityToken);
  83. //}
  84. //if (appleIdCredential.AuthorizationCode != null)
  85. //{
  86. // var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
  87. // Debug.Log("22===authorizationCode======= " + authorizationCode.Substring(0, 45));
  88. //}
  89. string _email = "";
  90. if (appleIdCredential.Email != null)
  91. {
  92. _email = appleIdCredential.Email;
  93. }
  94. Debug.Log("33===Email======= " + _email);
  95. string _fullName = "";
  96. if (appleIdCredential.FullName != null)
  97. {
  98. var fullName = appleIdCredential.FullName;
  99. if (appleIdCredential.FullName.PhoneticRepresentation != null)
  100. {
  101. var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
  102. //phoneticName.ToLocalizedString()
  103. }
  104. _fullName = fullName.ToLocalizedString();
  105. }
  106. Debug.Log("44===FullName======= " + appleIdCredential.FullName);
  107. var _identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  108. Debug.Log("11===identityToken======= " + _identityToken);
  109. LoginView.ins?.OnAppleLoginResp(_identityToken, _email, _fullName);
  110. }
  111. },
  112. error =>
  113. {
  114. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  115. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  116. this.SetupLoginMenuForSignInWithApple();
  117. LoginView.ins?.OnAppleLoginError();
  118. });
  119. }
  120. private void DeleteSignInWithApple(Action success, Action fail)
  121. {
  122. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  123. this._appleAuthManager.LoginWithAppleId(
  124. loginArgs,
  125. credential =>
  126. {
  127. var appleIdCredential = credential as IAppleIDCredential;
  128. if (appleIdCredential.AuthorizationCode != null)
  129. {
  130. var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
  131. Debug.Log("delete===authorizationCode======= " + authorizationCode.Substring(0, 45));
  132. StartCoroutine(LoginController.Instance.DdeleteUserByApple(authorizationCode, (res) =>
  133. {
  134. Debug.Log($"DdeleteUserByApple service rescode {res.code}, msg {res.msg}");
  135. if (res.code == 0)
  136. {
  137. Debug.Log("删除成功");
  138. this.SetupLoginMenuForSignInWithApple();
  139. PlayerPrefs.DeleteKey(AppleUserIdKey);
  140. }
  141. }));
  142. }
  143. success();
  144. },
  145. error =>
  146. {
  147. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  148. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  149. this.SetupLoginMenuForSignInWithApple();
  150. fail();
  151. });
  152. }
  153. public bool HasAppleKey() {
  154. return PlayerPrefs.HasKey(AppleUserIdKey);
  155. }
  156. public void DeleteAppleKey() {
  157. PlayerPrefs.DeleteKey(AppleUserIdKey);
  158. }
  159. }