LoginActivity.java 6.35 KB
package com.hjx.parent;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.prws.common.base.BaseActivity;
import com.prws.common.base.BasePresenter;
import com.prws.common.bean.BaseEntity;
import com.prws.common.net.NetWorks;
import com.prws.common.utils.LogUtil;
import com.prws.common.utils.SharedPreferencesUtil;
import com.prws.common.utils.acmanager.ActivityManager;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;


public class LoginActivity extends BaseActivity {


    EditText et_phone;
    EditText et_pwd;
    ImageView iv_show;
    ImageView iv_delete;
    Button btn_login;
    TextView tv_reg;
    CheckBox cb_1;
    TextView tv_user;
    TextView tv_ys;


    @Override
    protected int layoutResId() {
        return R.layout.activity_login;
    }

    @Override
    public Object getContract() {
        return null;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }


    @Override
    protected void initView() {
        et_phone = findViewById(R.id.et_phone);
        et_pwd = findViewById(R.id.et_pwd);
        iv_show = findViewById(R.id.iv_4);
        iv_delete = findViewById(R.id.iv_2);
        btn_login = findViewById(R.id.btn_1);
        tv_reg = findViewById(R.id.tv_2);
        cb_1 = findViewById(R.id.cb_1);
        tv_user = findViewById(R.id.tv_4);
        tv_ys = findViewById(R.id.tv_6);


        String student = (String) SharedPreferencesUtil.getData("student", "");

        if (!TextUtils.isEmpty(student)) {
            LogUtil.e(TAG, "role 不是空----");
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        } else {
            LogUtil.e(TAG, "role 是空----");
        }


    }

    @Override
    protected void initListener() {
        iv_delete.setOnClickListener(view -> {
            et_phone.setText("");
        });
        iv_show.setOnClickListener(view -> {
            if (iv_show.getBackground().getConstantState().equals(getDrawable(R.mipmap.xs).getConstantState())) {
                iv_show.setBackgroundResource(R.mipmap.yc);
                et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                iv_show.setBackgroundResource(R.mipmap.xs);
                et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });


        btn_login.setOnClickListener(view -> {

            String phone = et_phone.getText().toString().trim();
            String pwd = et_pwd.getText().toString().trim();
            if (phone.isEmpty() || pwd.isEmpty() || phone.length() == 0 || pwd.length() == 0) {
                Toast.makeText(this, "手机号以及密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!PhoneNumberUtils.isGlobalPhoneNumber(et_phone.getText().toString())) {
                Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!cb_1.isChecked()) {
                Toast.makeText(this, "请勾选协议", Toast.LENGTH_SHORT).show();
                return;
            }

            login(phone, pwd);

        });

        tv_reg.setOnClickListener(view -> {
            startActivity(RegisterActivity.class);
            finish();
        });

        tv_ys.setOnClickListener(view -> {
            startActivity(YinsiActivity.class);
        });
        tv_user.setOnClickListener(view -> {
            startActivity(UserAgreementActivity.class);
        });
    }

    public void login(final String phone, String pwd) {

        Map map = new HashMap();
        map.put("username", phone);
        map.put("password", pwd);


        NetWorks.login(NetWorks.getMapRequestBody(map), new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String str = responseBody.string().toString();
                    LogUtil.e(TAG, "----" + str);
                    JSONObject jo = new JSONObject(str);
                    boolean isSucceed = jo.getBoolean("success");
                    if (isSucceed) {
                        JSONObject jo2 = jo.getJSONObject("data");
                        SharedPreferencesUtil.putData("phone", phone);
                        SharedPreferencesUtil.putData("role", jo2.getString("role"));
                        SharedPreferencesUtil.putData("userId", jo2.getString("userId"));
                        SharedPreferencesUtil.putData("token", jo2.getString("token"));
                        JSONObject user = new JSONObject(jo2.getString("user"));
                        SharedPreferencesUtil.putData("photo", user.getString("photo"));
                        startActivity(ChooseActivity.class);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, jo.getString("msg"), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(LoginActivity.this, "服务繁忙,请重试", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    @Override
    protected void initData() {

    }

    @Override
    public void onNetChanged(int netWorkState) {

    }
}