KotlinCode.kt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package uts.sdk.modules.uniOauthGoogle
  2. import io.dcloud.uts.UTSAndroid
  3. import io.dcloud.uts.console
  4. import android.content.Context
  5. import android.os.SystemClock
  6. import android.util.Base64
  7. import android.util.Log
  8. import androidx.credentials.ClearCredentialStateRequest
  9. import androidx.credentials.Credential
  10. import androidx.credentials.CredentialManager
  11. import androidx.credentials.GetCredentialRequest
  12. import androidx.credentials.GetCredentialResponse
  13. import com.google.android.libraries.identity.googleid.GetGoogleIdOption
  14. import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
  15. import kotlinx.coroutines.CoroutineScope
  16. import kotlinx.coroutines.Dispatchers
  17. import kotlinx.coroutines.launch
  18. import org.json.JSONObject
  19. object NativeCode {
  20. /**
  21. * credentialManager 要求必须在协程环境进行调用
  22. */
  23. private val coroutineScope = CoroutineScope(Dispatchers.IO)
  24. fun requestLogout(credentialManager:CredentialManager,options:CallKotlinLogOutOptions){
  25. val request = ClearCredentialStateRequest()
  26. coroutineScope.launch {
  27. try {
  28. credentialManager.clearCredentialState(request)
  29. options.complete(null)
  30. } catch (e: Exception) {
  31. options.complete(e)
  32. }
  33. }
  34. }
  35. fun requestLogin(credentialManager:CredentialManager,request:GetCredentialRequest,options:CallKotlinLoginOptions){
  36. coroutineScope.launch {
  37. try {
  38. val result = credentialManager.getCredential(
  39. context = UTSAndroid.getUniActivity()!!,
  40. request = request
  41. )
  42. val userInfo = handleSignIn(result)
  43. if(userInfo == null){
  44. options.fail(null)
  45. }else{
  46. options.success(userInfo)
  47. }
  48. } catch (e : Exception) {
  49. options.fail(e)
  50. }
  51. }
  52. }
  53. fun handleFailure(e:Exception){
  54. console.log(e)
  55. }
  56. fun handleSignIn(result: GetCredentialResponse): GoogleLoginSuccess? {
  57. when (val credential = result.credential) {
  58. is GoogleIdTokenCredential -> {
  59. val idToken = credential.idToken
  60. // 验证 Token 并获取用户信息
  61. val user = getUserFromToken(idToken)
  62. // 更新 UI 或发送到服务器
  63. return user
  64. }
  65. else -> {
  66. // 处理其他类型的凭证(如密码)
  67. return null
  68. }
  69. }
  70. }
  71. fun getUserFromToken(idToken: String): GoogleLoginSuccess? {
  72. val parts = idToken.split(".")
  73. if (parts.size != 3) return null
  74. return try {
  75. val payload = String(Base64.decode(parts[1], Base64.URL_SAFE), Charsets.UTF_8)
  76. val json = JSONObject(payload)
  77. GoogleLoginSuccess(
  78. idToken = idToken,
  79. openId = json.optString("sub"),
  80. nickname = json.optString("name"),
  81. email = json.optString("email"),
  82. headimgurl = json.optString("picture")
  83. )
  84. } catch (e: Exception) {
  85. null
  86. }
  87. }
  88. }