AppleLoginHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. DontDestroyOnLoad(this);
  27. gameObject.name = "AppleLoginHelper";
  28. ins = this;
  29. }
  30. private void Start()
  31. {
  32. // If the current platform is supported
  33. if (AppleAuthManager.IsCurrentPlatformSupported)
  34. {
  35. // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  36. var deserializer = new PayloadDeserializer();
  37. // Creates an Apple Authentication manager with the deserializer
  38. this._appleAuthManager = new AppleAuthManager(deserializer);
  39. }
  40. }
  41. private void Update()
  42. {
  43. // Updates the AppleAuthManager instance to execute
  44. // pending callbacks inside Unity's execution loop
  45. if (this._appleAuthManager != null)
  46. {
  47. this._appleAuthManager.Update();
  48. }
  49. }
  50. //使用苹果正常登录
  51. public void SignInWithAppleButtonPressed()
  52. {
  53. this.SignInWithApple();
  54. }
  55. //删除账号
  56. public void SignInWithAppleButtonPressedDelete(Action success,Action fail)
  57. {
  58. this.DeleteSignInWithApple(success, fail);
  59. }
  60. private void SetupLoginMenuForSignInWithApple()
  61. {
  62. }
  63. private void SignInWithApple()
  64. {
  65. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  66. this._appleAuthManager.LoginWithAppleId(
  67. loginArgs,
  68. credential =>
  69. {
  70. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  71. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  72. //this.onAppleData(credential.User, credential);
  73. var appleIdCredential = credential as IAppleIDCredential;
  74. if (appleIdCredential != null)
  75. {
  76. //if (appleIdCredential.IdentityToken != null)
  77. //{
  78. // var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  79. // Debug.Log("11===identityToken======= " + identityToken);
  80. //}
  81. //if (appleIdCredential.AuthorizationCode != null)
  82. //{
  83. // var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
  84. // Debug.Log("22===authorizationCode======= " + authorizationCode.Substring(0, 45));
  85. //}
  86. string _email = "";
  87. if (appleIdCredential.Email != null)
  88. {
  89. _email = appleIdCredential.Email;
  90. }
  91. Debug.Log("33===Email======= " + _email);
  92. string _fullName = "";
  93. if (appleIdCredential.FullName != null)
  94. {
  95. var fullName = appleIdCredential.FullName;
  96. if (appleIdCredential.FullName.PhoneticRepresentation != null)
  97. {
  98. var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
  99. //phoneticName.ToLocalizedString()
  100. }
  101. _fullName = fullName.ToLocalizedString();
  102. }
  103. Debug.Log("44===FullName======= " + appleIdCredential.FullName);
  104. var _identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  105. Debug.Log("11===identityToken======= " + _identityToken);
  106. LoginView.ins?.OnAppleLoginResp(_identityToken, _email, _fullName);
  107. }
  108. },
  109. error =>
  110. {
  111. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  112. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  113. this.SetupLoginMenuForSignInWithApple();
  114. LoginView.ins?.OnAppleLoginError();
  115. });
  116. }
  117. private void DeleteSignInWithApple(Action success, Action fail)
  118. {
  119. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  120. this._appleAuthManager.LoginWithAppleId(
  121. loginArgs,
  122. credential =>
  123. {
  124. var appleIdCredential = credential as IAppleIDCredential;
  125. if (appleIdCredential.AuthorizationCode != null)
  126. {
  127. var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
  128. Debug.Log("delete===authorizationCode======= " + authorizationCode.Substring(0, 45));
  129. StartCoroutine(LoginController.Instance.DdeleteUserByApple(authorizationCode, (res) =>
  130. {
  131. Debug.Log($"DdeleteUserByApple service rescode {res.code}, msg {res.msg}");
  132. if (res.code == 0)
  133. {
  134. Debug.Log("删除成功");
  135. this.SetupLoginMenuForSignInWithApple();
  136. PlayerPrefs.DeleteKey(AppleUserIdKey);
  137. }
  138. }));
  139. }
  140. success();
  141. },
  142. error =>
  143. {
  144. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  145. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  146. this.SetupLoginMenuForSignInWithApple();
  147. fail();
  148. });
  149. }
  150. public bool HasAppleKey() {
  151. return PlayerPrefs.HasKey(AppleUserIdKey);
  152. }
  153. public void DeleteAppleKey() {
  154. PlayerPrefs.DeleteKey(AppleUserIdKey);
  155. }
  156. }