good-luck-cargo/app/src/main/java/com/oneclouds/cargo/ui/LoginActivity.java
2024-08-07 08:35:41 +08:00

281 lines
10 KiB
Java

package com.oneclouds.cargo.ui;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import com.google.gson.Gson;
import com.oneclouds.cargo.R;
import com.oneclouds.cargo.bean.ConfigBean;
import com.oneclouds.cargo.bean.LoginBean;
import com.oneclouds.cargo.constant.SPConstant;
import com.oneclouds.cargo.request.PublicRequest;
import com.oneclouds.cargo.request.bean.LoginRegInputBean;
import com.oneclouds.cargo.ui.parts.ConfigParts;
import com.oneclouds.cargo.util.SPUtil;
import com.oneclouds.cargo.util.cache.CacheGroup;
import com.oneclouds.cargo.util.http.RequsetCodeConstants;
import com.oneclouds.cargo.util.titleutil.StatusBarUtil;
import com.oneclouds.cargo.util.view.BaseActivity;
import java.util.TimerTask;
/**
* @author hlh
* @version 1.0.0
* @date 2021/9/13 10:35
* @description:
*/
public class LoginActivity extends BaseActivity {
public static final String CODE_RESULT = "code_result";
public static final String LOGIN_RESULT = "login_result";
public static final String USER = "user";
public static final String USER_TOKEN = "token";
public static final String USER_UNAME = "uname";
public static final String USER_RNAME = "rname";
public static final String USER_ID = "id";
private Button btn_login;
private TextView auto_login;
private EditText input_phone, input_code;
private TextView getCode, tvForget;
private LoginBean loginBean;
private TimerTask tt;
private int timer_sum = 60;
private ImageButton agree_btn;
private LinearLayout llCheck;
private CheckBox cbLog;
private CardView no_btn;
private PublicRequest pr;
@Override
public void msgMethod(Message m) {
super.msgMethod(m);
switch (m.what) {
case 12:
getCode.setText(timer_sum + "s");
if (timer_sum <= 0) {
getCode.setText("获取验证码");
timer_sum = 60;
code_key = true;
}
break;
case RequsetCodeConstants.SUCCESS:
if (CacheGroup.cacheList.get(PublicRequest.SEND_CODE) != null) {
Log.e("--短信发送res--", "短信发送成功");
}
if (CacheGroup.cacheList.get(PublicRequest.LOGIN) != null) {
//存储登录记录
loginBean = new Gson().fromJson(CacheGroup.cacheList.get(PublicRequest.LOGIN), LoginBean.class);
Log.e("--登录信息--", new Gson().toJson(loginBean));
if (loginBean.getCode() == 200) {
SPUtil.insSP(con, SPConstant.DATA, SPConstant.DATA_TOKEN, loginBean.getData().getToken());
SPUtil.insSP(con, USER, USER_UNAME, loginBean.getData().getUname());
SPUtil.insSP(con, USER, USER_RNAME, loginBean.getData().getRname());
SPUtil.insSP(con, USER, USER_ID, loginBean.getData().getId() + "");
finish();
} else {
Toast.makeText(con, loginBean.getMsg(), Toast.LENGTH_SHORT).show();
}
CacheGroup.cacheList.remove(PublicRequest.LOGIN);
}
break;
default:
break;
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StatusBarUtil.touming(getWindow());
setContentView(R.layout.activity_login);
con = this;
btn_login = findViewById(R.id.btn_login);
auto_login = findViewById(R.id.auto_login);
input_phone = findViewById(R.id.input_phone);
input_code = findViewById(R.id.input_code);
getCode = findViewById(R.id.getCode);
tvForget = findViewById(R.id.tvForget);
agree_btn = findViewById(R.id.agree_btn);
llCheck = findViewById(R.id.ll_check);
cbLog = findViewById(R.id.cb);
no_btn = findViewById(R.id.no_btn);
pr = new PublicRequest(con, hd);
initView(null);
}
boolean key = false;
boolean code_key = true;
private String url1 = "";
private String url2 = "";
@Override
public void initView(Object obj) {
super.initView(obj);
llCheck.setOnClickListener(v -> {
if (cbLog.isChecked()){
cbLog.setChecked(false);
}else{
cbLog.setChecked(true);
}
});
// agree_btn.setOnClickListener(v -> {
// agree_btn.setVisibility(View.GONE);
// no_btn.setVisibility(View.VISIBLE);
// key = false;
// });
// no_btn.setOnClickListener(v -> {
// agree_btn.setVisibility(View.VISIBLE);
// no_btn.setVisibility(View.GONE);
// key = true;
//
// });
ConfigBean cb = ConfigParts.getConfigParts(con);
if (cb != null) {
url1 = cb.getData().getUserUrl();
url2 = cb.getData().getShipperPrivateUrl();
}
TextView tv_content = findViewById(R.id.tv_content);
String str = "我已阅读并同意相关服务条款和隐私政策《用户服务协议》和《隐私政策》";
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(str);
//第一个出现的位置
final int start = str.indexOf("");
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
//用户服务协议点击事件
Log.e("用户服务协议0", url1);
Intent in = new Intent(con, WebActivity.class);
in.putExtra("title", "用户服务协议");
in.putExtra("url", url1);
startActivity(in);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
//设置文件颜色
ds.setColor(getResources().getColor(R.color.color_theme, null));
// 去掉下划线
ds.setUnderlineText(false);
}
}, start, start + 8, 0);
final int end = str.lastIndexOf("");
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
//隐私协议点击事件
Intent in = new Intent(con, WebActivity.class);
in.putExtra("title", "隐私政策");
in.putExtra("url", url2);
startActivity(in);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
//设置文件颜色
ds.setColor(getResources().getColor(R.color.color_theme, null));
// 去掉下划线
ds.setUnderlineText(false);
}
}, end, end + 6, 0);
tv_content.setMovementMethod(LinkMovementMethod.getInstance());
tv_content.setText(ssb, TextView.BufferType.SPANNABLE);
getCode.setOnClickListener(v -> {
String phone = String.valueOf(input_phone.getText());
if (phone.equals("") || phone == null) {
Log.e("点击事件", "请先输入手机号");
Toast.makeText(con, "请先输入手机号", Toast.LENGTH_SHORT).show();
} else {
if (code_key) {
getCd();
pr.getPhoneCode(phone);
code_key = false;
}
}
});
tvForget.setOnClickListener(v1 -> {
Intent intent = new Intent(this, ForgetPwdActivity.class);
startActivity(intent);
});
btn_login.setOnClickListener(v -> {
String phone = String.valueOf(input_phone.getText());
String code = String.valueOf(input_code.getText());
Log.e("--code--", code);
if (judgeFormat(phone, code)) {
if (cbLog.isChecked()) {
LoginRegInputBean lb = new LoginRegInputBean();
lb.setRoleId(2);
lb.setUserName(phone);
lb.setPassword(code);
pr.userLogin2(lb);
// lb.setRoleId(2);
// lb.setUname(phone);
// lb.setSmscode(code);
// lb.setRegisterType(1);
// pr.userLogin(lb);
} else {
Toast.makeText(con, "请先同意用户协议和隐私政策", Toast.LENGTH_SHORT).show();
}
}
});
auto_login.setOnClickListener(v -> {
Toast.makeText(con, "暂未开放", Toast.LENGTH_SHORT).show();
});
}
public void getCd() {
new Thread() {
@Override
public void run() {
while (timer_sum > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer_sum--;
Message msg = new Message();
msg.what = 12;
hd.sendMessage(msg);
}
interrupt();
}
}.start();
}
//验证用户输入的是否符合格式
public boolean judgeFormat(String phone, String code) {
if (phone.equals("") || code.equals("")) {
Toast.makeText(con, "手机号和验证码不得为空", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
}