190 lines
7.9 KiB
Kotlin
190 lines
7.9 KiB
Kotlin
package com.yingmao.clash.act
|
||
|
||
import android.annotation.SuppressLint
|
||
import android.content.Intent
|
||
import android.os.Bundle
|
||
import android.os.CountDownTimer
|
||
import android.view.View
|
||
import android.widget.EditText
|
||
import android.widget.TextView
|
||
import android.widget.Toast
|
||
import androidx.appcompat.widget.AppCompatImageView
|
||
import androidx.lifecycle.ViewModelProvider
|
||
import androidx.lifecycle.observe
|
||
import com.yingmao.clash.view.dialog.RxDialogShapeLoading
|
||
|
||
import com.tencent.mmkv.MMKV
|
||
import com.yingmao.clash.R
|
||
import com.yingmao.clash.base.BaseActivity
|
||
import com.yingmao.clash.base.BaseNotTitleActivity
|
||
import com.yingmao.clash.conf.BZYConstants
|
||
import com.yingmao.clash.net.http.HttpReqType
|
||
import com.yingmao.clash.net.http.HttpStatus
|
||
import com.yingmao.clash.net.http.entity.GetSMSCodeRsp
|
||
import com.yingmao.clash.util.CommonUtils
|
||
import com.yingmao.clash.util.Utils
|
||
import com.yingmao.clash.view.RxToast
|
||
|
||
class BandPhoneActivity : BaseActivity() {
|
||
private lateinit var rxDialogShapeLoading: RxDialogShapeLoading
|
||
private lateinit var tvSmsCode: TextView
|
||
private lateinit var tvAccount: TextView
|
||
private lateinit var tvTitle: TextView
|
||
private lateinit var etPhoneNumber: EditText
|
||
private lateinit var etSmsCode: EditText
|
||
private var phoneNumber: String = ""
|
||
var isBandPhone = false
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
super.onCreate(savedInstanceState)
|
||
isBandPhone = intent.getBooleanExtra("isPhone", true);
|
||
setContentView(R.layout.activity_band_phone)
|
||
val viewModel = ViewModelProvider(this)[BandPhoneAtyVM::class.java]
|
||
etPhoneNumber = findViewById(R.id.et_phone_number)
|
||
tvAccount = findViewById(R.id.tv_account)
|
||
tvTitle = findViewById(R.id.tv_title)
|
||
etSmsCode = findViewById(R.id.et_sms_code)
|
||
|
||
tvSmsCode = findViewById(R.id.tv_sms_code)
|
||
tvSmsCode.setOnClickListener {
|
||
phoneNumber = etPhoneNumber.text.trim().toString()
|
||
if (isBandPhone) {
|
||
if (phoneNumber.startsWith("1") && phoneNumber.length == 11) {
|
||
viewModel.getSMSCode(phoneNumber)
|
||
} else {
|
||
RxToast.warning(resources.getString(R.string.correctPhoneNumber))
|
||
}
|
||
} else {
|
||
if (Utils.isEmail(phoneNumber)) {
|
||
viewModel.getSMSCode(phoneNumber)
|
||
} else {
|
||
RxToast.warning(resources.getString(R.string.correctPhoneEmail))
|
||
}
|
||
}
|
||
}
|
||
|
||
findViewById<View>(R.id.btn_band).setOnClickListener {
|
||
phoneNumber = etPhoneNumber.text.trim().toString()
|
||
if (isBandPhone) {
|
||
if (!Utils.isPhoneNumber(phoneNumber)) {
|
||
RxToast.warning(resources.getString(R.string.correctPhoneNumber))
|
||
return@setOnClickListener
|
||
}
|
||
} else {
|
||
if (!Utils.isEmail(phoneNumber)) {
|
||
RxToast.warning(resources.getString(R.string.correctPhoneEmail))
|
||
return@setOnClickListener
|
||
}
|
||
}
|
||
|
||
val smsCode = etSmsCode.text.trim().toString()
|
||
if (smsCode.length < 4) {
|
||
RxToast.warning(resources.getString(R.string.correctCode))
|
||
return@setOnClickListener
|
||
}
|
||
val userName = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME,"")!!
|
||
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN,"")!!
|
||
val params = mutableMapOf(
|
||
"phoneNumber" to phoneNumber,
|
||
"vpnAccount" to userName,
|
||
"SMSCode" to smsCode,
|
||
"phToken" to phToken,
|
||
)
|
||
viewModel.bandPhone(params)
|
||
}
|
||
|
||
if (!isBandPhone){
|
||
tvTitle.text =resources.getString(R.string.bindingEmail)
|
||
tvAccount.text = resources.getString(R.string.email)
|
||
}
|
||
|
||
|
||
initVM(viewModel)
|
||
}
|
||
|
||
private fun initVM(viewModel: BandPhoneAtyVM) {
|
||
viewModel.statusData.observe(this) {
|
||
when (it) {
|
||
is HttpStatus.Loading -> {
|
||
if (it.reqType == HttpReqType.GETSMSCODE) {
|
||
apply {
|
||
rxDialogShapeLoading = RxDialogShapeLoading(this)
|
||
rxDialogShapeLoading.setMessage(resources.getString(R.string.ObtainingVerificationCode))
|
||
rxDialogShapeLoading.show()
|
||
}
|
||
} else {
|
||
apply {
|
||
rxDialogShapeLoading = RxDialogShapeLoading(this)
|
||
rxDialogShapeLoading.setMessage(resources.getString(R.string.nowBinding))
|
||
rxDialogShapeLoading.show()
|
||
}
|
||
}
|
||
}
|
||
|
||
is HttpStatus.Success -> {
|
||
rxDialogShapeLoading.cancel()
|
||
if (it.reqType == HttpReqType.GETSMSCODE) {
|
||
(it.rsp as GetSMSCodeRsp).let { rsp ->
|
||
if (rsp.code == 1000) {
|
||
RxToast.success(resources.getString(R.string.VerificationCodeSent))
|
||
countDown()
|
||
} else {
|
||
RxToast.warning(resources.getString(R.string.VerificationCodeSentFail)+ rsp.message)
|
||
}
|
||
}
|
||
} else {
|
||
(it.rsp as GetSMSCodeRsp).let { rsp ->
|
||
if (rsp.code == 1000) {
|
||
RxToast.success(resources.getString(R.string.BingIngSuccess), Toast.LENGTH_LONG)
|
||
MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, phoneNumber)
|
||
MMKV.defaultMMKV().removeValueForKey(BZYConstants.MMKV_KEY_PH_TOKEN)
|
||
MMKV.defaultMMKV().removeValueForKey(BZYConstants.MMKV_KEY_TOKEN)
|
||
MMKV.defaultMMKV().removeValueForKey(BZYConstants.MMKV_KEY_CURRENT_NODE)
|
||
//百度埋点完成注册
|
||
// BaiduAction.logAction(ActionType.REGISTER)
|
||
val intent=Intent(CommonUtils.getApp(), LoginActivity::class.java)
|
||
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
|
||
startActivity(intent)
|
||
finish()
|
||
} else {
|
||
RxToast.warning(resources.getString(R.string.BingIngFail) + rsp.message)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
is HttpStatus.Failure -> {
|
||
rxDialogShapeLoading.cancel()
|
||
if (it.reqType == HttpReqType.GETSMSCODE) {
|
||
RxToast.warning(resources.getString(R.string.GetSmsFail)+ it.msg)
|
||
} else {
|
||
RxToast.warning(resources.getString(R.string.BingIngFail)+":${it.msg}")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
var countDownTimer: CountDownTimer? = null
|
||
fun countDown() {
|
||
tvSmsCode.isEnabled = false
|
||
countDownTimer = object : CountDownTimer(60000, 1000) {
|
||
//1000ms运行一次onTick里面的方法
|
||
override fun onFinish() {
|
||
tvSmsCode.isEnabled = true
|
||
tvSmsCode.text = resources.getString(R.string.ObtainVerificationCode)
|
||
}
|
||
|
||
@SuppressLint("SetTextI18n")
|
||
override fun onTick(millisUntilFinished: Long) {
|
||
tvSmsCode.text = "${millisUntilFinished / 1000} S"
|
||
|
||
}
|
||
}.start()
|
||
}
|
||
|
||
override fun onDestroy() {
|
||
super.onDestroy()
|
||
countDownTimer?.cancel()
|
||
}
|
||
} |