| | |
| | | package com.dayu.pipirrapp.activity; |
| | | |
| | | import static com.dayu.pipirrapp.net.Constants.BASE_URL; |
| | | |
| | | import android.content.Intent; |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.BitmapFactory; |
| | | import android.os.Bundle; |
| | | import android.os.PersistableBundle; |
| | | import android.text.InputType; |
| | | import android.util.Base64; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | | import com.dayu.pipirrapp.R; |
| | | import com.dayu.pipirrapp.bean.db.LoginBean; |
| | | import com.dayu.pipirrapp.bean.net.CodeResult; |
| | | import com.dayu.pipirrapp.bean.net.LoginResult; |
| | | import com.dayu.pipirrapp.dao.DaoSingleton; |
| | | import com.dayu.pipirrapp.databinding.ActivityLoginBinding; |
| | | import com.dayu.pipirrapp.net.ApiManager; |
| | | import com.dayu.pipirrapp.net.BaseResponse; |
| | | import com.dayu.pipirrapp.net.subscribers.SubscriberListener; |
| | | import com.dayu.pipirrapp.observer.LoginObserver; |
| | | import com.dayu.pipirrapp.utils.CommonKeyName; |
| | | import com.dayu.pipirrapp.utils.SharedPreferencesHelper; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | import com.tencent.bugly.crashreport.CrashReport; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | |
| | | public class LoginActivity extends AppCompatActivity { |
| | | private boolean isPasswordVisible = false; |
| | | public class LoginActivity extends BaseActivity { |
| | | private final String TAG = "LoginActivity"; |
| | | |
| | | ActivityLoginBinding binding; |
| | | LoginObserver loginObserver; |
| | | //密码是否可见 |
| | | private boolean isPasswordVisible = false; |
| | | //是否显示验证码 |
| | | private boolean isShowCode; |
| | | |
| | | private int loginError = 0; |
| | | private String codeToken; |
| | | //点击次数到10次后可以重新选择tag |
| | | int clickNumber; |
| | | |
| | | |
| | | @Override |
| | | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| | | Log.i(TAG, "onCreate"); |
| | | super.onCreate(savedInstanceState); |
| | | binding = ActivityLoginBinding.inflate(LayoutInflater.from(this)); |
| | | setContentView(binding.getRoot()); |
| | | initView(); |
| | | initData(); |
| | | } |
| | | |
| | | |
| | | private void initData() { |
| | | |
| | | } |
| | | |
| | | |
| | | private void initView() { |
| | | loginObserver = new LoginObserver(this); |
| | | getLifecycle().addObserver(loginObserver); |
| | | binding.passwordBtn.setOnClickListener((view) -> { |
| | | togglePasswordVisibility(); |
| | | }); |
| | | binding.loginBtn.setOnClickListener(v -> { |
| | | Intent intent = new Intent(LoginActivity.this, MainActivity.class); |
| | | startActivity(intent); |
| | | String phone = binding.name.getText().toString().trim(); |
| | | String pw = binding.password.getText().toString().trim(); |
| | | String code = binding.code.getText().toString().trim(); |
| | | if (validatePhone(phone) && validatePassword(pw)) { |
| | | if (isShowCode) { |
| | | if (!validateCode(code)) { |
| | | return; |
| | | } |
| | | } |
| | | login(phone, pw, codeToken, code); |
| | | } else { |
| | | ToastUtil.showToast(LoginActivity.this, "请输入正确信息"); |
| | | } |
| | | |
| | | }); |
| | | binding.codeImg.setOnClickListener(v -> { |
| | | getCode(); |
| | | }); |
| | | binding.codeError.setOnClickListener(v -> { |
| | | getCode(); |
| | | }); |
| | | binding.version.setOnClickListener(v -> { |
| | | clickNumber++; |
| | | if (clickNumber == 10) { |
| | | //点击次数到10次后可以重新选择tag |
| | | loginObserver.choesLibrary(); |
| | | clickNumber = 0; |
| | | } |
| | | }); |
| | | if (SharedPreferencesHelper.getInstance(LoginActivity.this).get(CommonKeyName.isShowCode, false)) { |
| | | isShowCode = true; |
| | | binding.codeLL.setVisibility(View.VISIBLE); |
| | | getCode(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 显示隐藏密码 |
| | | */ |
| | | private void togglePasswordVisibility() { |
| | | if (isPasswordVisible) { |
| | | // 如果密码可见,隐藏密码 |
| | | binding.password.setInputType( |
| | | InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); |
| | | binding.passwordBtn.setImageResource(R.drawable.ic_visibility_off); |
| | | binding.passwordBtn.setImageResource(R.drawable.vt_ps_close); |
| | | } else { |
| | | // 如果密码隐藏,显示密码 |
| | | binding.password.setInputType(InputType.TYPE_CLASS_TEXT); |
| | | binding.passwordBtn.setImageResource(R.drawable.ic_visibility); |
| | | binding.passwordBtn.setImageResource(R.drawable.vt_ps_open); |
| | | } |
| | | |
| | | // 移动光标到末尾 |
| | | binding.password.setSelection(binding.password.getText().length()); |
| | | |
| | | // 切换标志 |
| | | isPasswordVisible = !isPasswordVisible; |
| | | } |
| | | |
| | | /** |
| | | * 获取验证码 |
| | | */ |
| | | private void getCode() { |
| | | Map<String, Object> data = new HashMap<>(); |
| | | data.put("type", "math");//图片类型:char-文本,math-一位数算式,math2-两位数算式 |
| | | |
| | | |
| | | ApiManager.getInstance().requestGetHideLoading(LoginActivity.this, BASE_URL + "/app/captcha/get", CodeResult.class, data, new SubscriberListener<BaseResponse<CodeResult>>() { |
| | | @Override |
| | | public void onNext(BaseResponse<CodeResult> t) { |
| | | try { |
| | | if (t.isSuccess()) { |
| | | String encodedString = t.getContent().getBase64(); |
| | | codeToken = t.getContent().getToken(); |
| | | // 移除Base64字符串的头部信息 |
| | | encodedString = encodedString.substring("data:image/png;base64,".length()); |
| | | // 对Base64字符串进行解码 |
| | | byte[] imageAsBytes = Base64.decode(encodedString, Base64.DEFAULT); |
| | | // 使用BitmapFactory将字节数组解码成Bitmap对象 |
| | | Bitmap image = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); |
| | | binding.codeImg.setImageBitmap(image); |
| | | binding.codeImg.setVisibility(View.VISIBLE); |
| | | binding.codeError.setVisibility(View.GONE); |
| | | } else { |
| | | binding.codeImg.setVisibility(View.INVISIBLE); |
| | | binding.codeError.setVisibility(View.VISIBLE); |
| | | ToastUtil.showToast(LoginActivity.this, "验证码获取失败"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | CrashReport.postCatchedException(e); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | |
| | | |
| | | // ApiManager.getInstance().getCode(data, new CodeListener() { |
| | | // @Override |
| | | // public void onNext(CodeResult body) { |
| | | // try { |
| | | // if (body != null && !TextUtils.isEmpty(body.getBase64())) { |
| | | // String encodedString = body.getBase64(); |
| | | // codeToken = body.getToken(); |
| | | // // 移除Base64字符串的头部信息 |
| | | // encodedString = encodedString.substring("data:image/png;base64,".length()); |
| | | // // 对Base64字符串进行解码 |
| | | // byte[] imageAsBytes = Base64.decode(encodedString, Base64.DEFAULT); |
| | | // // 使用BitmapFactory将字节数组解码成Bitmap对象 |
| | | // Bitmap image = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); |
| | | // binding.codeImg.setImageBitmap(image); |
| | | // binding.codeImg.setVisibility(View.VISIBLE); |
| | | // binding.codeError.setVisibility(View.GONE); |
| | | // } else { |
| | | // binding.codeImg.setVisibility(View.INVISIBLE); |
| | | // binding.codeError.setVisibility(View.VISIBLE); |
| | | // ToastUtil.showToast(LoginActivity.this, "验证码获取失败"); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // CrashReport.postCatchedException(e); |
| | | // } |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void error(Throwable throwable) { |
| | | // |
| | | // } |
| | | // }); |
| | | } |
| | | |
| | | private void login(String phone, String pw, String token, String code) { |
| | | |
| | | Map<String, Object> data = new HashMap<>(); |
| | | data.put("phone", phone); |
| | | data.put("password", pw); |
| | | data.put("orgTag", MyApplication.myApplication.myTag); |
| | | if (isShowCode) { |
| | | data.put("token", token); |
| | | data.put("code", code); |
| | | } |
| | | ApiManager.getInstance().requestPostLoading(LoginActivity.this, BASE_URL + "/sso/sso/loginJson", LoginResult.class, data, new SubscriberListener<BaseResponse<LoginResult>>() { |
| | | @Override |
| | | public void onNext(BaseResponse<LoginResult> t) { |
| | | try { |
| | | if (t.isSuccess()) { |
| | | SharedPreferencesHelper.getInstance(LoginActivity.this).put(CommonKeyName.Token, t.getContent().getToken()); |
| | | MyApplication.myApplication.token = t.getContent().getToken(); |
| | | MyApplication.myApplication.userId = t.getContent().getId(); |
| | | LoginBean loginBean = DaoSingleton.getInstance(LoginActivity.this).loginDao().findFirst(); |
| | | if (loginBean == null) { |
| | | loginBean = new LoginBean(); |
| | | } |
| | | loginBean.setName(t.getContent().getName()); |
| | | loginBean.setPhone(t.getContent().getPhone()); |
| | | loginBean.setUserID(t.getContent().getId()); |
| | | DaoSingleton.getInstance(LoginActivity.this).loginDao().insert(loginBean); |
| | | SharedPreferencesHelper.getInstance(LoginActivity.this).put(CommonKeyName.isShowCode, false); |
| | | ToastUtil.showToastLong(LoginActivity.this, "登录成功!"); |
| | | startMainActivity(); |
| | | |
| | | } else { |
| | | ToastUtil.showToast(LoginActivity.this, t.getMsg()); |
| | | loginError++; |
| | | if (loginError >= 3) { |
| | | isShowCode = true; |
| | | SharedPreferencesHelper.getInstance(LoginActivity.this).put(CommonKeyName.isShowCode, true); |
| | | binding.codeLL.setVisibility(View.VISIBLE); |
| | | getCode(); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | CrashReport.postCatchedException(e); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 启动MainActivity |
| | | */ |
| | | private void startMainActivity() { |
| | | Intent intent = new Intent(LoginActivity.this, MainActivity.class); |
| | | startActivity(intent); |
| | | LoginActivity.this.finish(); |
| | | } |
| | | |
| | | @Override |
| | | protected void onStart() { |
| | | super.onStart(); |
| | | Log.i(TAG, "onStart"); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onResume() { |
| | | super.onResume(); |
| | | Log.i(TAG, "onResume"); |
| | | } |
| | | |
| | | @Override |
| | | protected void onRestart() { |
| | | super.onRestart(); |
| | | Log.i(TAG, "onRestart"); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onPause() { |
| | | super.onPause(); |
| | | Log.i(TAG, "onPause"); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onStop() { |
| | | super.onStop(); |
| | | Log.i(TAG, "onStop"); |
| | | } |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | getLifecycle().removeObserver(loginObserver); |
| | | Log.i(TAG, "onDestroy"); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onSaveInstanceState(@NonNull Bundle outState) { |
| | | super.onSaveInstanceState(outState); |
| | | Log.i(TAG, "onSaveInstanceState"); |
| | | } |
| | | |
| | | @Override |
| | | public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) { |
| | | super.onSaveInstanceState(outState, outPersistentState); |
| | | Log.i(TAG, "onSaveInstanceState---outPersistentState"); |
| | | } |
| | | |
| | | @Override |
| | | protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { |
| | | super.onRestoreInstanceState(savedInstanceState); |
| | | } |
| | | |
| | | // 手机号校验:确保是一个有效的手机号 |
| | | private boolean validatePhone(String phone) { |
| | | // 使用正则表达式判断是否为有效的手机号码(中国大陆) |
| | | String phonePattern = "^1[3-9]\\d{9}$"; // 简单的手机号码正则 |
| | | if (phone.isEmpty()) { |
| | | ToastUtil.showToast(LoginActivity.this, "手机号不能为空!"); |
| | | return false; |
| | | } |
| | | // else if (!phone.matches(phonePattern)) { |
| | | // ToastUtil.showToast(LoginActivity.this, "请输入正确手机号!"); |
| | | // return false; |
| | | // } |
| | | else { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | // 密码校验:确保密码长度符合要求 |
| | | private boolean validatePassword(String password) { |
| | | if (password.isEmpty()) { |
| | | ToastUtil.showToast(LoginActivity.this, "密码不能为空!"); |
| | | return false; |
| | | } |
| | | // else if (password.length() < 6) { |
| | | // ToastUtil.showToast(LoginActivity.this, "密码不能少于6位!"); |
| | | // return false; |
| | | // } |
| | | else { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | // 验证码校验:确保验证码符合长度要求 |
| | | private boolean validateCode(String code) { |
| | | if (code.isEmpty()) { |
| | | ToastUtil.showToast(LoginActivity.this, "验证码不能为空"); |
| | | return false; |
| | | } else { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |