邮箱+手机 注册,验证码,4中登录方式。

This commit is contained in:
fengjignqi
2026-09-23 09:51:55 +08:00
parent ec2c26e829
commit 60bacffca5
5 changed files with 108 additions and 25 deletions

View File

@@ -29,6 +29,8 @@ class LoginActivity : BaseActivity<ActivityLoginBinding, LoginPresenter>(),
/** 是否使用密码登录;false 为手机号 + 验证码登录 */ /** 是否使用密码登录;false 为手机号 + 验证码登录 */
private var loginByPassword = true private var loginByPassword = true
/** 是否使用手机号登录;false 表示使用邮箱登录。 */
private var loginByPhone = true
private var isPasswordVisible = false private var isPasswordVisible = false
private var codeCountDownTimer: CountDownTimer? = null private var codeCountDownTimer: CountDownTimer? = null
@@ -59,38 +61,57 @@ class LoginActivity : BaseActivity<ActivityLoginBinding, LoginPresenter>(),
).forEach { button -> ).forEach { button ->
button.setOnClickListener(this) button.setOnClickListener(this)
} }
updateLoginModeText()
} }
override fun onClick(view: View) { override fun onClick(view: View) {
when (view.id) { when (view.id) {
R.id.tvChangeCode -> {//更换登录方式 R.id.tvChangeCode -> {//更换登录方式
//Todo 2.目前这里的切换,只是针对的手机 验证码/密码登录,两种方式。需要修改一下,参考【Todo 1】这里不仅要对手机号的 验证码/密码登录 做处理,同时也要处理 邮箱的 验证码/密码登录;所以这里应该是四种登录方式。 手机验证码/密码 + 邮箱验证码/密码。 // 在当前手机号/邮箱类型下,切换密码登录和验证码登录。
if (loginByPassword) { if (loginByPassword) {
binding.linearCode.visibility = View.VISIBLE binding.linearCode.visibility = View.VISIBLE
binding.linearPsd.visibility = View.GONE binding.linearPsd.visibility = View.GONE
loginByPassword = false loginByPassword = false
binding.tvChangeCode.setText(R.string.register_change_psd_login)
} else { } else {
binding.linearCode.visibility = View.GONE binding.linearCode.visibility = View.GONE
binding.linearPsd.visibility = View.VISIBLE binding.linearPsd.visibility = View.VISIBLE
loginByPassword = true loginByPassword = true
binding.tvChangeCode.setText(R.string.register_change_code_login)
} }
updateLoginModeText()
} }
R.id.tv_change_login_type ->{ R.id.tv_change_login_type ->{
//Todo 1.这里是切换手机/邮箱 两种登录方式的按钮,点击切换,同时img_login_type改变,【icon_login_phone】/【icon_login_email】两种。 // 切换手机号和邮箱登录,并同步输入框图标、提示和输入类型。
loginByPhone = !loginByPhone
binding.imgLoginType.setImageResource(
if (loginByPhone) R.mipmap.icon_login_phone else R.mipmap.icon_login_email
)
binding.tvChangeLoginType.text = if (loginByPhone) "邮箱" else "手机"
// 账号类型切换后,同步更新密码/验证码登录切换提示。
updateLoginModeText()
binding.accountEditText.apply {
hint = if (loginByPhone) "请输入手机号" else "请输入邮箱"
inputType = if (loginByPhone) {
InputType.TYPE_CLASS_PHONE
} else {
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
}
text?.clear()
}
} }
R.id.getCodeButton -> {//获取验证码 R.id.getCodeButton -> {//获取验证码
//Todo 3. 这里获取验证码,是2种,发送手机验证码和邮箱验证码。另外我【Apiservice】中已经写好了方法,两种获取验证码的接口都有。 // 根据当前账号类型选择手机或邮箱验证码接口。
val phone = binding.accountEditText.text.toString().trim() val account = binding.accountEditText.text.toString().trim()
if (phone.isBlank()) { if (account.isBlank()) {
ToastUtil.showShort(mContext, "手机号不能为空") ToastUtil.showShort(mContext, if (loginByPhone) "手机号不能为空" else "邮箱不能为空")
return return
} }
presenter?.getPhoneSmsCode(phone, AppConfig.SEND_CODE_SCENE_LOGIN) if (loginByPhone) {
presenter?.getPhoneSmsCode(account, AppConfig.SEND_CODE_SCENE_LOGIN)
} else {
presenter?.getEmailSmsCode(account, AppConfig.SEND_CODE_SCENE_LOGIN)
}
} }
R.id.registerButton -> {//注册 R.id.registerButton -> {//注册
@@ -106,7 +127,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding, LoginPresenter>(),
} }
R.id.loginButton -> {//登录 R.id.loginButton -> {//登录
//Todo 4.同样也是和前面的逻辑一样,4种登录方法。需要你补齐目前缺少的。 // 账号类型和登录方式组合成四种登录请求。
val account = binding.accountEditText.text.toString().trim() val account = binding.accountEditText.text.toString().trim()
if (account.isBlank()) { if (account.isBlank()) {
ToastUtil.showShort(mContext, "账号不能为空") ToastUtil.showShort(mContext, "账号不能为空")
@@ -119,14 +140,22 @@ class LoginActivity : BaseActivity<ActivityLoginBinding, LoginPresenter>(),
ToastUtil.showShort(mContext, "密码不能为空") ToastUtil.showShort(mContext, "密码不能为空")
return return
} }
presenter?.loginByPhonePsd(account, password) if (loginByPhone) {
presenter?.loginByPhonePsd(account, password)
} else {
presenter?.loginByEmailPsd(account, password)
}
} else { } else {
val code = binding.codeEditText.text.toString().trim() val code = binding.codeEditText.text.toString().trim()
if (code.isBlank()) { if (code.isBlank()) {
ToastUtil.showShort(mContext, "验证码不能为空") ToastUtil.showShort(mContext, "验证码不能为空")
return return
} }
presenter?.loginByPhoneSms(account, code) if (loginByPhone) {
presenter?.loginByPhoneSms(account, code)
} else {
presenter?.loginByEmailSms(account, code)
}
} }
} }
} }
@@ -145,6 +174,15 @@ class LoginActivity : BaseActivity<ActivityLoginBinding, LoginPresenter>(),
binding.passwordEditText.typeface = binding.accountEditText.typeface binding.passwordEditText.typeface = binding.accountEditText.typeface
} }
/** 根据账号类型和当前登录方式,更新登录方式切换按钮文案。 */
private fun updateLoginModeText() {
binding.tvChangeCode.text = if (loginByPassword) {
if (loginByPhone) "切换手机号验证码登录" else "切换邮箱验证码登录"
} else {
if (loginByPhone) "切换手机号密码登录" else "切换邮箱密码登录"
}
}
override fun sendCodeSuccess(message: String) { override fun sendCodeSuccess(message: String) {
hideLoading() hideLoading()
ToastUtil.showShort(mContext, message) ToastUtil.showShort(mContext, message)

View File

@@ -2,6 +2,7 @@ package com.juhabi.birds.login
import android.os.Bundle import android.os.Bundle
import android.os.CountDownTimer import android.os.CountDownTimer
import android.text.InputType
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@@ -30,6 +31,7 @@ class RegisterActivity : BaseActivity<RegisterLayoutBinding, LoginPresenter>(),
LoginContract.LoginView, View.OnClickListener { LoginContract.LoginView, View.OnClickListener {
private var codeCountDownTimer: CountDownTimer? = null private var codeCountDownTimer: CountDownTimer? = null
private var registerByPhone = true
override fun createBinding(inflater: LayoutInflater): RegisterLayoutBinding { override fun createBinding(inflater: LayoutInflater): RegisterLayoutBinding {
return RegisterLayoutBinding.inflate(inflater) return RegisterLayoutBinding.inflate(inflater)
@@ -56,6 +58,7 @@ class RegisterActivity : BaseActivity<RegisterLayoutBinding, LoginPresenter>(),
binding.confirmButton, binding.confirmButton,
binding.includedTitle.imgBack, binding.includedTitle.imgBack,
binding.linearCountry, binding.linearCountry,
binding.tvChangeLoginType,
).forEach { button -> ).forEach { button ->
button.setOnClickListener(this) button.setOnClickListener(this)
} }
@@ -68,27 +71,44 @@ class RegisterActivity : BaseActivity<RegisterLayoutBinding, LoginPresenter>(),
finish() finish()
} }
R.id.tv_change_login_type ->{
registerByPhone = !registerByPhone
binding.linearCountry.visibility = if (registerByPhone) View.VISIBLE else View.GONE
binding.tvChangeLoginType.text = if (registerByPhone) "邮箱" else "手机"
binding.phoneEditText.apply {
text?.clear()
hint = if (registerByPhone) "请输入手机号" else "请输入邮箱"
inputType = if (registerByPhone) {
InputType.TYPE_CLASS_PHONE
} else {
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
}
}
}
R.id.linear_country -> showCountryCodeDialog() R.id.linear_country -> showCountryCodeDialog()
R.id.getCodeButton -> {//获取验证码 R.id.getCodeButton -> {//获取验证码
if (binding.phoneEditText.text.toString().isBlank()) { val account = binding.phoneEditText.text.toString().trim()
ToastUtil.showShort(mContext, "手机号不能为空") if (account.isBlank()) {
ToastUtil.showShort(mContext, if (registerByPhone) "手机号不能为空" else "邮箱不能为空")
return return
} }
presenter?.getPhoneSmsCode( if (registerByPhone) {
binding.phoneEditText.text.toString(), presenter?.getPhoneSmsCode(account, AppConfig.SEND_CODE_SCENE_REGISTER)
AppConfig.SEND_CODE_SCENE_REGISTER } else {
) presenter?.getEmailSmsCode(account, AppConfig.SEND_CODE_SCENE_REGISTER)
}
} }
R.id.confirmButton -> {//注册 R.id.confirmButton -> {//注册
val phone = binding.phoneEditText.text.toString().trim() val account = binding.phoneEditText.text.toString().trim()
val code = binding.codeEditText.text.toString().trim() val code = binding.codeEditText.text.toString().trim()
val password = binding.passwordEditText.text.toString() val password = binding.passwordEditText.text.toString()
val confirmPassword = binding.confirmPasswordEditText.text.toString() val confirmPassword = binding.confirmPasswordEditText.text.toString()
if (phone.isBlank()) { if (account.isBlank()) {
ToastUtil.showShort(mContext, "手机号不能为空") ToastUtil.showShort(mContext, if (registerByPhone) "手机号不能为空" else "邮箱不能为空")
return return
} }
if (code.isBlank()) { if (code.isBlank()) {
@@ -115,7 +135,11 @@ class RegisterActivity : BaseActivity<RegisterLayoutBinding, LoginPresenter>(),
return return
} }
presenter?.registerByPhone(phone, code, password) if (registerByPhone) {
presenter?.registerByPhone(account, code, password)
} else {
presenter?.registerByEmail(account, code, password, "")
}
} }
} }
} }

View File

@@ -80,7 +80,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="end" android:layout_gravity="end"
android:gravity="center" android:gravity="center"
android:text="手机" android:text="邮箱"
android:textColor="@color/color_zhu" android:textColor="@color/color_zhu"
android:textSize="13sp" /> android:textSize="13sp" />
</LinearLayout> </LinearLayout>

View File

@@ -80,6 +80,16 @@
android:maxLines="1" android:maxLines="1"
android:textColorHint="@color/color_999999" android:textColorHint="@color/color_999999"
android:textSize="16sp" /> android:textSize="16sp" />
<TextView
android:id="@+id/tv_change_login_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginEnd="20dp"
android:gravity="center"
android:text="邮箱"
android:textColor="@color/color_zhu"
android:textSize="13sp" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout

View File

@@ -73,7 +73,7 @@
android:id="@+id/phoneEditText" android:id="@+id/phoneEditText"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="2"
android:background="@null" android:background="@null"
android:hint="请输入手机号" android:hint="请输入手机号"
android:inputType="phone" android:inputType="phone"
@@ -81,6 +81,17 @@
android:maxLines="1" android:maxLines="1"
android:textColorHint="@color/color_999999" android:textColorHint="@color/color_999999"
android:textSize="16sp" /> android:textSize="16sp" />
<TextView
android:id="@+id/tv_change_login_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginEnd="20dp"
android:gravity="center"
android:text="邮箱"
android:textColor="@color/color_zhu"
android:textSize="13sp" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout