rabbit-android/app/src/main/java/com/img/rabbit/viewmodel/LoginViewModel.kt

451 lines
16 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.img.rabbit.viewmodel
import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.State
import androidx.lifecycle.viewModelScope
import com.alipay.sdk.app.AuthTask
import com.img.rabbit.pages.LoginScreenType
import com.g.gysdk.GYManager
import com.g.gysdk.GYResponse
import com.g.gysdk.GyCallBack
import com.g.gysdk.GyConfig
import com.github.gzuliyujiang.oaid.DeviceIdentifier
import com.google.gson.JsonObject
import com.img.rabbit.bean.local.ErrorBean
import com.img.rabbit.bean.local.OnekeyPreLogin
import com.img.rabbit.bean.local.toAlipayResult
import com.img.rabbit.bean.response.LoginInfoEntity
import com.img.rabbit.bean.response.UserConfigEntity
import com.img.rabbit.bean.response.UserInfoEntity
import com.img.rabbit.components.CenterToast
import com.img.rabbit.provider.api.ApiManager
import com.img.rabbit.provider.api.ResultVo
import com.img.rabbit.provider.storage.GlobalStateManager
import com.img.rabbit.provider.storage.PreferenceUtil
import com.img.rabbit.provider.utils.HeadParamUtils.applicationContext
import com.img.rabbit.utils.AppEventBus
import com.img.rabbit.utils.AppUpdate
import com.img.rabbit.utils.GlobalEvent
import com.img.rabbit.utils.GlobalEventBus
import com.img.rabbit.utils.LoginBindEvent
import com.img.rabbit.utils.MMKVUtils
import com.img.rabbit.utils.StringUtils
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeoutOrNull
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.resume
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import okhttp3.RequestBody.Companion.toRequestBody
class LoginViewModel : BaseViewModel() {
private val TAG = "LoginViewModel"
private val ONEKEY_TAG = "OneKeyLoginViewModel"
var isLoginWxAuthor = false
val authInfoForAlipay: MutableState<String> = mutableStateOf("")
val loginScreenType = mutableStateOf(LoginScreenType.LOGIN_NORMAL)
// 登录用户名
val userName = mutableStateOf("")
fun setUserName(loginName: String) {
userName.value = loginName
}
// 登录验证码
val captcha = mutableStateOf("")
fun setCaptcha(loginCaptcha: String) {
captcha.value = loginCaptcha
}
// 登录验证码发送时间戳
val captchaTimestamp = mutableStateOf("")
private val isGYUIDValid = mutableStateOf(false)
var oneKeyPreLogin: OnekeyPreLogin? = null
// 是否同意政策协议
private val _policyAgreement = mutableStateOf(false)
val isPolicyAgreement: State<Boolean> = _policyAgreement
fun setIsPolicyAgreement(isAgreement: Boolean) {
_policyAgreement.value = isAgreement
}
// 登录获取结果
val loginResult = mutableStateOf<ResultVo<LoginInfoEntity>?>(null)
// 用户配置获取结果
val userConfigResult = mutableStateOf<ResultVo<UserConfigEntity>?>(null)
// 保留最新配置供首页观察;userConfigResult 作为请求完成事件会被 Activity 清空。
private val _userConfig = mutableStateOf(PreferenceUtil.getUserConfig())
val userConfig: State<UserConfigEntity?> = _userConfig
// 页面直接观察用户数据,缓存只用于首次加载。
private val _userInfo = mutableStateOf(PreferenceUtil.getUserInfo())
val userInfo: State<UserInfoEntity?> = _userInfo
// 错误状态
val errorState = mutableStateOf<ErrorBean?>(null)
fun reset(){
isLoginWxAuthor = false
authInfoForAlipay.value = ""
loginScreenType.value = LoginScreenType.LOGIN_NORMAL
userName.value = ""
captcha.value = ""
captchaTimestamp.value = ""
isGYUIDValid.value = false
oneKeyPreLogin = null
setIsPolicyAgreement(true)
loginResult.value = null
userConfigResult.value = null
errorState.value = null
}
fun requestUserConfig(isInitConfig: Boolean = false){
mLaunch {
//先置空配置
PreferenceUtil.saveUserConfig(null)
val oaid = MMKVUtils.getString("oaid") ?: ""
val response = ApiManager.serviceVo.getUserConfig(oaid, Build.VERSION.SDK_INT, "", DeviceIdentifier.getAndroidID(applicationContext), MMKVUtils.getString("gt_cid") ?: "")
if (response.status) {
PreferenceUtil.saveXToken(response.data.token)
PreferenceUtil.setTimeDiff(response.data.nowtime.toLong() - System.currentTimeMillis() / 1000)
PreferenceUtil.saveUserConfig(response.data)
_userConfig.value = response.data
userConfigResult.value = response
if(isInitConfig){
val versionEntity = response.data.config?.versionEntity
AppUpdate.checkUpdate(versionEntity) { isUpdate, _, _ ->
if(isUpdate){
// 有更新,提示更新
viewModelScope.launch{
versionEntity?.let {
GlobalEventBus.emit(
GlobalEvent.ShowAppUpdateNotify)
}
}
}
}
}
}else{
Log.w("LoginViewModel", "获取配置失败: code=${response.code}, message=${response.message}")
}
}
}
/** 登录页进入时优先预取本机号码;失败、超时或结果无效时交由页面回退。 */
suspend fun prepareOneKeyLogin(activity: Activity): Boolean = withContext(Dispatchers.Main.immediate) {
oneKeyPreLogin = null
isGYUIDValid.value = false
if (!PreferenceUtil.getAgreement()) return@withContext false
try {
withTimeoutOrNull(12_000L) {
val manager = GYManager.getInstance()
awaitGyResponse { callback ->
manager.init(GyConfig.with(activity.applicationContext).callBack(callback).build())
} ?: return@withTimeoutOrNull false
isGYUIDValid.value = true
val response = awaitGyResponse { callback -> manager.ePreLogin(8000, callback) }
?: return@withTimeoutOrNull false
val data = response.msg?.takeIf { it.isNotBlank() }
?: return@withTimeoutOrNull false
val result = Json { ignoreUnknownKeys = true }.decodeFromString<OnekeyPreLogin>(data)
if (result.errorCode != 0 || result.number.isBlank() ||
!manager.isPreLoginResultValid || manager.preLoginResult == null) {
return@withTimeoutOrNull false
}
oneKeyPreLogin = result
true
} ?: false
} catch (e: CancellationException) {
// 页面退出时取消等待,不让迟到的 SDK 回调切换后续页面。
throw e
} catch (e: Exception) {
Log.w(ONEKEY_TAG, "一键登录准备失败,回退验证码登录", e)
false
}
}
private suspend fun awaitGyResponse(start: (GyCallBack) -> Unit): GYResponse? =
suspendCancellableCoroutine { continuation ->
val completed = AtomicBoolean(false)
fun complete(response: GYResponse?) {
if (completed.compareAndSet(false, true) && continuation.isActive) {
continuation.resume(response)
}
}
start(object : GyCallBack {
override fun onSuccess(response: GYResponse?) = complete(response)
override fun onFailed(response: GYResponse?) = complete(null)
})
}
fun requestOneKeyLogin(gyuid: String, token: String) {
// 调用 API 获取数据
val jsonOneKey = JsonObject()
jsonOneKey.addProperty("gyuid", gyuid)
jsonOneKey.addProperty("token", token)
val jsonObject = JsonObject()
jsonObject.addProperty("type", "onekey")
jsonObject.addProperty("bind", "")
jsonObject.add("data", jsonOneKey)
Log.i(ONEKEY_TAG, "--->开始登录")
requestLogin(jsonObject, LoginScreenType.LOGIN_ONE_KEY)
}
/**
* 请求验证码
*/
fun requestCaptcha(phone: String) {
// 发送请求获取验证码
mLaunch {
// 调用 API 获取数据
val jsonObject = JsonObject()
jsonObject.addProperty("phone", phone)
val response = ApiManager.serviceVo.sendCode(jsonObject.toString().toRequestBody())
if (response.status) {
Log.w("LoginViewModel", "请求验证码成功: ${response.data.timestamp}")
captchaTimestamp.value = response.data.timestamp
}else{
errorState.value = ErrorBean(response.code.toString(), response.message.ifEmpty { "获取验证码失败" })
}
}
}
/**
* 请求登录(验证码)
*/
fun requestLoginForCaptcha(phone: String, code: String) {
// 调用 API 获取数据
val jsonPhone = JsonObject()
jsonPhone.addProperty("timestamp", captchaTimestamp.value)
jsonPhone.addProperty("phone", phone)
jsonPhone.addProperty("code", code)
val jsonObject = JsonObject()
jsonObject.addProperty("login_type", "phone")
jsonObject.add("phone", jsonPhone)
requestLogin(jsonObject, LoginScreenType.LOGIN_CAPTCHA)
}
fun authorWechat(context: Context, wxApi:IWXAPI) {
if (isPolicyAgreement.value) {
if (!wxApi.isWXAppInstalled) {
CenterToast.show("您没有安装微信客户端,请先下载安装")
return
}
isLoginWxAuthor = true
val req = SendAuth.Req()
req.scope = "snsapi_userinfo" // 只能填 snsapi_userinfo
req.state = context.packageName + Math.random() * 1000 + "_phone"
wxApi.sendReq(req)
}else{
CenterToast.show("请先同意用户协议和隐私政策")
}
}
fun requestWxLogin(code: String){
val jsonObject = getWxLoginParam(code) ?: return
requestLogin(jsonObject, LoginScreenType.LOGIN_WX)
}
/**
* 拿着微信授权码完成登录(在WXEntryActivity中调用)
* @param code 微信授权码
*/
fun getWxLoginParam(code: String): JsonObject? {
if(code.isEmpty()){
return null
}
PreferenceUtil.saveWxCode(code)
// 调用 API 获取数据
val jsonWx = JsonObject()
jsonWx.addProperty("code", code)
jsonWx.addProperty("code_type", "")
val jsonObject = JsonObject()
jsonObject.addProperty("login_type", "weixin")
jsonObject.add("weixin", jsonWx)
return jsonObject
}
/**
* 请求支付宝登录参数
*/
fun requestAliPayAuthParam(context: Context) {
mLaunch {
val response = ApiManager.serviceVo.getAlipayAuthParam()
val data = response.data
val param = data.param
authorAlipay(context = context,authParam = param)
authInfoForAlipay.value = param
}
}
private fun authorAlipay(context: Context, authParam: String){
if(authParam.isEmpty()){
CenterToast.show("请先获取支付宝登录授权码")
return
}
// 发送请求获取支付宝登录授权码
mLaunch {
val authorResult = doAlipayLogin(context as Activity, authParam)
Log.i("loginWithAliPay", "支付宝登录结果:$authorResult")
val alipayResult = authorResult.toAlipayResult()
// 处理支付宝登录结果
when (alipayResult.resultStatus) {
"9000" -> {
// 登录成功,result 字段中包含 auth_code
val authCode = StringUtils.parseAlipayResult(alipayResult.result ?: "")["auth_code"] ?: ""
val jsonObject = getAlipayLoginParam(authCode) ?: return@mLaunch
requestLogin(jsonObject, LoginScreenType.LOGIN_ALIPAY)
}
"6001" -> {
"用户取消登录"
}
else -> {
alipayResult.memo ?: "登录失败"
}
}
}
}
/**
* 封装支付宝登录逻辑
* @param activity 当前 Activity 上下文
* @param authInfo 后端生成的授权字符串
* @return 支付宝返回的原始 Map 结果
*/
private suspend fun doAlipayLogin(activity: Activity, authInfo: String): Map<String, String> {
return withContext(Dispatchers.IO) {
// 初始化 AuthTask
val authTask = AuthTask(activity)
// 调用 authV2。第二个参数为 true 表示如果未安装支付宝则展示 Loading 界面, 该方法会阻塞当前线程直到用户操作结束
val result = authTask.authV2(authInfo, true)
Log.i(TAG, "doAlipayLogin: $result")
result ?: emptyMap()
}
}
/**
* 拿着支付宝授权码完成登录
* @param authCode 支付宝授权码
*/
private fun getAlipayLoginParam(authCode: String): JsonObject? {
if(authCode.isEmpty()){
return null
}
// 调用 API 获取数据
val jsonWx = JsonObject()
jsonWx.addProperty("auth_code", authCode)//code
val jsonObject = JsonObject()
jsonObject.addProperty("type", "alipay")
jsonObject.addProperty("bind", "")
jsonObject.add("data", jsonWx)
return jsonObject
}
//请求登录
private fun requestLogin(jsonObject: JsonObject, loginType: LoginScreenType){
setLoading(true)
mLaunch {
val response = ApiManager.serviceVo.login(jsonObject.toString().toRequestBody())
if (response.status) {
PreferenceUtil.saveLoginType(loginType.name)
PreferenceUtil.saveLoginInfo(response.data.apply { isLogin = true })//记录登录数据
PreferenceUtil.saveAccessToken(response.data.token)
loginResult.value = response
}else{
errorState.value = ErrorBean(response.code.toString(), response.message.ifEmpty { "登录失败" })
}
setLoading(false)
}
}
fun requestUserInfo(){
val currentUserId = _userConfig.value?.user_id
if (!currentUserId.isNullOrBlank() && _userInfo.value?.user_id != currentUserId) {
PreferenceUtil.saveUserInfo(null)
_userInfo.value = null
}
mLaunch {
val response = ApiManager.serviceVo.userInfo()
// 切换账号后,忽略旧账号尚未完成的请求。
if (currentUserId != _userConfig.value?.user_id) return@mLaunch
if(response.status){
PreferenceUtil.saveUserInfo(response.data)
_userInfo.value = response.data
}
}
}
/**
* 请求退出登录
*/
@OptIn(DelicateCoroutinesApi::class)
fun requestLogout() {
mLaunch {
val response = ApiManager.serviceVo.logout()
if (response.status) {
PreferenceUtil.clearLogin()
_userInfo.value = null
AppEventBus.post( LoginBindEvent.Login(userId = null, loginType = PreferenceUtil.getLoginType(), isLogin = false, data = null) )
} else {
errorState.value = ErrorBean(response.code.toString(), response.message.ifEmpty { "退出登录失败" })
}
}
}
/**
* 检查是否登录
*/
fun isLogin(): Boolean{
val userInfo = PreferenceUtil.getUserInfo()
return userInfo!=null && !userInfo.temp
}
/**
* 检查登录,未登录 则跳转登录页
*/
fun requiredLogin(): Boolean{
val isLogin = isLogin()
if(!isLogin){
viewModelScope.launch{
GlobalEventBus.emit(GlobalEvent.ShowLoginNotify)
}
}
return isLogin
}
enum class JumpLoginType(val type: Int,val desc: String){
NORMAL(0,"正常登录"),
FROM_ADD(1,"添加账号"),
FROM_LOGOUT(2,"退出登录")
}
}