|
|
|
|
@ -12,8 +12,6 @@ import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
|
|
|
|
import com.google.android.material.textfield.TextInputEditText;
|
|
|
|
|
import com.startsmake.llrisetabbardemo.R;
|
|
|
|
|
import com.startsmake.llrisetabbardemo.manager.SmsManager;
|
|
|
|
|
import com.startsmake.llrisetabbardemo.manager.UserManager;
|
|
|
|
|
|
|
|
|
|
public class ForgotPasswordActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
@ -24,23 +22,14 @@ public class ForgotPasswordActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
private CountDownTimer countDownTimer;
|
|
|
|
|
private boolean isCounting = false;
|
|
|
|
|
private String verificationCode = "";
|
|
|
|
|
private UserManager userManager;
|
|
|
|
|
private SmsManager smsManager;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
setContentView(R.layout.activity_forgot_password);
|
|
|
|
|
|
|
|
|
|
userManager = UserManager.getInstance(this);
|
|
|
|
|
smsManager = SmsManager.getInstance(this);
|
|
|
|
|
|
|
|
|
|
initViews();
|
|
|
|
|
setupClickListeners();
|
|
|
|
|
|
|
|
|
|
// 预填充手机号(如果从登录页传递过来)
|
|
|
|
|
handleIntentExtras();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initViews() {
|
|
|
|
|
@ -64,78 +53,31 @@ public class ForgotPasswordActivity extends AppCompatActivity {
|
|
|
|
|
btnResetPassword.setOnClickListener(v -> attemptResetPassword());
|
|
|
|
|
|
|
|
|
|
// 返回登录
|
|
|
|
|
tvLogin.setOnClickListener(v -> navigateToLogin());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleIntentExtras() {
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
|
if (intent != null) {
|
|
|
|
|
String phone = intent.getStringExtra("phone");
|
|
|
|
|
if (phone != null && !phone.isEmpty()) {
|
|
|
|
|
etPhone.setText(phone);
|
|
|
|
|
etVerificationCode.requestFocus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tvLogin.setOnClickListener(v -> {
|
|
|
|
|
Intent intent = new Intent(ForgotPasswordActivity.this, LoginActivity.class);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
finish();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendVerificationCode() {
|
|
|
|
|
String phone = etPhone.getText().toString().trim();
|
|
|
|
|
|
|
|
|
|
if (!validatePhone(phone)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查用户是否存在
|
|
|
|
|
if (!userManager.isUserExists(phone)) {
|
|
|
|
|
etPhone.setError("该手机号未注册");
|
|
|
|
|
etPhone.requestFocus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isCounting) {
|
|
|
|
|
startCountDown();
|
|
|
|
|
|
|
|
|
|
// 使用短信服务发送验证码
|
|
|
|
|
smsManager.sendVerificationCode(phone, new SmsManager.SmsSendCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess(String code) {
|
|
|
|
|
verificationCode = code;
|
|
|
|
|
Toast.makeText(ForgotPasswordActivity.this,
|
|
|
|
|
"验证码已发送到手机尾号" + phone.substring(7),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onFailure(String error) {
|
|
|
|
|
Toast.makeText(ForgotPasswordActivity.this,
|
|
|
|
|
"验证码发送失败: " + error,
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
resetCountDown();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean validatePhone(String phone) {
|
|
|
|
|
if (phone.isEmpty()) {
|
|
|
|
|
etPhone.setError("请输入手机号");
|
|
|
|
|
etPhone.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (phone.length() != 11) {
|
|
|
|
|
etPhone.setError("手机号格式不正确");
|
|
|
|
|
etPhone.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!phone.startsWith("1")) {
|
|
|
|
|
etPhone.setError("手机号格式不正确");
|
|
|
|
|
etPhone.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
if (!isCounting) {
|
|
|
|
|
startCountDown();
|
|
|
|
|
Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
|
|
|
|
|
// 这里应该调用后端API发送验证码
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startCountDown() {
|
|
|
|
|
@ -157,111 +99,43 @@ public class ForgotPasswordActivity extends AppCompatActivity {
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resetCountDown() {
|
|
|
|
|
if (countDownTimer != null) {
|
|
|
|
|
countDownTimer.cancel();
|
|
|
|
|
}
|
|
|
|
|
isCounting = false;
|
|
|
|
|
btnSendCode.setEnabled(true);
|
|
|
|
|
btnSendCode.setText("发送验证码");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void attemptResetPassword() {
|
|
|
|
|
String phone = etPhone.getText().toString().trim();
|
|
|
|
|
String code = etVerificationCode.getText().toString().trim();
|
|
|
|
|
String newPassword = etNewPassword.getText().toString().trim();
|
|
|
|
|
|
|
|
|
|
if (!validateResetInput(phone, code, newPassword)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证验证码
|
|
|
|
|
if (!code.equals(verificationCode)) {
|
|
|
|
|
etVerificationCode.setError("验证码错误");
|
|
|
|
|
etVerificationCode.requestFocus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重置密码
|
|
|
|
|
if (userManager.updatePassword(phone, newPassword)) {
|
|
|
|
|
performResetSuccess(phone);
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "重置密码失败,请检查手机号是否正确", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean validateResetInput(String phone, String code, String newPassword) {
|
|
|
|
|
if (phone.isEmpty()) {
|
|
|
|
|
etPhone.setError("请输入手机号");
|
|
|
|
|
etPhone.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (code.isEmpty()) {
|
|
|
|
|
etVerificationCode.setError("请输入验证码");
|
|
|
|
|
etVerificationCode.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newPassword.isEmpty()) {
|
|
|
|
|
etNewPassword.setError("请输入新密码");
|
|
|
|
|
etNewPassword.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (phone.length() != 11) {
|
|
|
|
|
etPhone.setError("手机号格式不正确");
|
|
|
|
|
etPhone.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newPassword.length() < 6) {
|
|
|
|
|
etNewPassword.setError("密码至少6位");
|
|
|
|
|
etNewPassword.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查密码强度(可选)
|
|
|
|
|
if (!isPasswordStrong(newPassword)) {
|
|
|
|
|
etNewPassword.setError("密码过于简单,建议包含字母和数字");
|
|
|
|
|
etNewPassword.requestFocus();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isPasswordStrong(String password) {
|
|
|
|
|
// 简单的密码强度检查:至少包含字母和数字
|
|
|
|
|
boolean hasLetter = false;
|
|
|
|
|
boolean hasDigit = false;
|
|
|
|
|
|
|
|
|
|
for (char c : password.toCharArray()) {
|
|
|
|
|
if (Character.isLetter(c)) {
|
|
|
|
|
hasLetter = true;
|
|
|
|
|
} else if (Character.isDigit(c)) {
|
|
|
|
|
hasDigit = true;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hasLetter && hasDigit;
|
|
|
|
|
// 模拟重置密码成功
|
|
|
|
|
performResetPassword();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void performResetSuccess(String phone) {
|
|
|
|
|
private void performResetPassword() {
|
|
|
|
|
Toast.makeText(this, "密码重置成功!", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
|
|
// 重置成功后跳转到登录页面,并传递手机号
|
|
|
|
|
navigateToLoginWithPhone(phone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void navigateToLoginWithPhone(String phone) {
|
|
|
|
|
Intent intent = new Intent(ForgotPasswordActivity.this, LoginActivity.class);
|
|
|
|
|
intent.putExtra("phone", phone);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void navigateToLogin() {
|
|
|
|
|
// 重置成功后跳转到登录页面
|
|
|
|
|
Intent intent = new Intent(ForgotPasswordActivity.this, LoginActivity.class);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
finish();
|
|
|
|
|
@ -274,4 +148,4 @@ public class ForgotPasswordActivity extends AppCompatActivity {
|
|
|
|
|
countDownTimer.cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|