欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

仿京东登入+注册

程序员文章站 2022-07-13 16:54:33
...

一、登入连接

1.
public class Api {
    public static  final  String SHOUYE="https://www.zhaoapi.cn/";
    public static  final  String LOGINAll="http://172.17.8.100/movieApi/";
    public static  final  String REGISTER="http://172.17.8.100/movieApi/";
}
2.
public interface LoginApi {
    @POST("user/v1/login")
    @FormUrlEncoded
    Observable<LoginEntity> Login(@FieldMap HashMap<String,String> map);
}

二、契约层

public interface LoginContract {

    abstract class LoginPresenter extends BasePresenter<LoginModel,LoginView>{

        @Override
        public LoginModel getmModel() {
            return new LoginModel();
        }

        public abstract void login(HashMap<String,String> map);
    }

    interface loginModel extends IBaseModel{
        Observable<LoginEntity> Login(HashMap<String,String> map);
    }
}

三、model

public class LoginModel implements LoginContract.loginModel{

    @Override
    public Observable<LoginEntity> Login(HashMap<String, String> map) {
        return RetrofitUtils.getInstance().createApi(Api.LOGINAll, LoginApi.class).Login(map)
                .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread());
    }
}

四、p层

public class LoginPresenter extends LoginContract.LoginPresenter {
    @Override
    public void login(HashMap<String, String> map) {
        mModel.Login(map).subscribe(new Consumer<LoginEntity>() {
            @Override
            public void accept(LoginEntity loginentity) throws Exception {
                mView.success(loginentity);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.failure("异常");
            }
        });
    }
}

五、fragment

/**
 * EventBus是一个Android事件发布/订阅框架,
 * 通过解耦发布者和订阅者简化Android事件传递,
 * 这里的事件可以理解为消息。
 * 事件传递既可以用于Android四大组件间通讯,
 * 也可以用于异步线程和主线程间通讯等。
 */
public class MyFragment extends BaseMvpFragment<LoginContract.loginModel,LoginContract.LoginPresenter> implements LoginView{

    @BindView(R.id.shezhi)//设置图标
    ImageView shezhi;
    @BindView(R.id.duihua)//对话图标
    ImageView duihua;
    @BindView(R.id.lll)//设置对话布局
    LinearLayout lll;
    @BindView(R.id.touxiang)//头像
    SimpleDraweeView touxiang;
    @BindView(R.id.denglu)//登入注册
    TextView degnluorzhuce;
    private SharedPreferences sp;
    private SharedPreferences.Editor edit;
    private HashMap<String, String> map;
    private LoginEntity.ResultBean result;
    private String status;

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initData() {
        super.initData();
        //获取到shert中的数据
        EventBus.getDefault().register(this);
        sp = getActivity().getSharedPreferences("User", Context.MODE_PRIVATE);
        edit = sp.edit();
        String name = sp.getString("name", "");
        String pwd = sp.getString("pwd", "");
        //登入
        map = new HashMap<>();
        map.put("phone",name);
        map.put("pwd",pwd);
        presenter.login(map);
    }
    @OnClick(R.id.denglu)
    public void loginzhuce(){
        startActivity(new Intent(getActivity(), ZhuceActivity.class));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEven(HeadEventBus headEventBus){
        String id = headEventBus.getId();
        touxiang.setImageURI(id);
        presenter.login(map);
    }

    //登入成功后
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEventBus(LoginEntity loginEntity){
        String id = sp.getString("sessionId", "");
        if (TextUtils.isEmpty(id)){
            return;
        }else {
            //点击设置按钮
            shezhi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), SetActivity.class));
                }
            });
            //点击名字
            degnluorzhuce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), SetActivity.class));
                }
            });
            result = loginEntity.getResult();
            status = loginEntity.getStatus();
            degnluorzhuce.setText(loginEntity.getResult().getUserInfo().getNickName());
            degnluorzhuce.setTextColor(Color.WHITE);
            String headPic = loginEntity.getResult().getUserInfo().getHeadPic();
            touxiang.setImageURI(headPic);
            presenter.login(map);
        }
    }

    //在没有登入的时候
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEvent(String s){

        degnluorzhuce.setText("登录/注册");
        touxiang.setImageResource(R.mipmap.ic_launcher);
        //点击登入注册按钮,首先进入登入页面
        degnluorzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ZhuceActivity.class));
            }
        });
        //点击头像
        touxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ZhuceActivity.class));
            }
        });
        //设置按钮
        shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ZhuceActivity.class));
            }
        });
    }

    @Override
    protected void initView() {

    }




    //成功
    @Override
    public void success(LoginEntity loginentity) {
        final LoginEntity.ResultBean result = loginentity.getResult();
        edit.putString("sessionId",result.getSessionId());
        edit.commit();
        String status = loginentity.getStatus();
        degnluorzhuce.setText(loginentity.getResult().getUserInfo().getNickName());
        degnluorzhuce.setTextColor(Color.WHITE);
        String headPic = loginentity.getResult().getUserInfo().getHeadPic();
        touxiang.setImageURI(headPic);
        //点击名字
        degnluorzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sessionId = sp.getString("sessionId", "");
                Log.i("aaa",sessionId);
                //Toast.makeText(getActivity(),result.getSessionId()+"--->"+sessionId,Toast.LENGTH_LONG).show();
                //startActivity(new Intent(getActivity(), SetActivity.class));
            }
        });
        //点击图像
        touxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SetActivity.class));
            }
        });
        //设置按钮
        shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SetActivity.class));
            }
        });

    }

    @Override
    public void failure(String error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public BasePresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void fail(String msg) {

    }
}

六、登入页面

public class ZhuceActivity extends BaseMvpActivity<LoginContract.loginModel,LoginContract.LoginPresenter> implements LoginView, View.OnClickListener {

    @BindView(R.id.login_name)
    EditText loginName;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.login_login)
    Button loginLogin;
    @BindView(R.id.login_reg)
    TextView loginReg;
    private SharedPreferences sp;
    private SharedPreferences.Editor edit;

    @Override
    protected int bindLayoutId() {

        return R.layout.activity_zhuce;
    }

    @Override
    protected void initData() {
        super.initData();
        loginReg.setOnClickListener(this);//注册
        loginLogin.setOnClickListener(this);//登入
    }
    @Override
    protected void initView() {
        sp = getSharedPreferences("User", MODE_PRIVATE);
        edit = sp.edit();
        edit.putString("name","");
        edit.putString("pwd","");
        edit.putString("Status","");
        edit.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_login://登入
                //获取输入内容
                String name = loginName.getText().toString();
                String pwd = loginPwd.getText().toString();
                //加密
                String pwd2 = EncryptUtil.encrypt(pwd);
                HashMap<String, String> map = new HashMap<>();
                map.put("phone",name);
                map.put("pwd",pwd2);
                //存shert
                edit.putString("name",name);
                edit.putString("pwd",pwd2);
                edit.commit();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){
                    Toast.makeText(ZhuceActivity.this,"密码和账号皆不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    //进行登入
                    presenter.login(map);
                }
                break;
            case R.id.login_reg://注册
                startActivity(new Intent(ZhuceActivity.this,RegisterActivity.class));
                break;
        }
    }
    //成功
    @Override
    public void success(LoginEntity loginentity) {
        edit.putString("Status",loginentity.getStatus());
        edit.putString("sessionId",loginentity.getResult().getSessionId());
        int userId = loginentity.getResult().getUserId();
        edit.putString("userId",userId+"");
        edit.putString("headPic",loginentity.getResult().getUserInfo().getHeadPic());
        edit.putString("nickName",loginentity.getResult().getUserInfo().getNickName());
        edit.putString("phone",loginentity.getResult().getUserInfo().getPhone());
        int sex = loginentity.getResult().getUserInfo().getSex();
        edit.putString("sex",sex+"");
        edit.commit();
        Toast.makeText(ZhuceActivity.this,"登入成功--",Toast.LENGTH_SHORT).show();
         EventBus.getDefault().post(loginentity);
        finish();
    }
    //失败
    @Override
    public void failure(String error) {
        Toast.makeText(ZhuceActivity.this,error,Toast.LENGTH_SHORT).show();
    }

    @Override
    public BasePresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void fail(String msg) {

    }


}

七、注册连接

public interface ZhuceApi {

    @POST("user/v1/registerUser")
    @FormUrlEncoded
    Observable<RegisterEntity> Register(@FieldMap HashMap<String,String> map);
}

八、注册契约

public interface RegisterContract {

    abstract class RegisterPresenter extends BasePresenter<RegisterModel,RegisterView>{
        @Override
        public RegisterModel getmModel() {
            return new com.bwei.MyJD.mvp.model.RegisterModel();
        }
        public abstract void register(HashMap<String,String> map);
    }

    interface RegisterModel extends IBaseModel{
        Observable<RegisterEntity> Register(HashMap<String,String> map);
    }

    interface RegisterView extends IBaseView{
        void success(RegisterEntity registerEntity);
        void failure(String error);
    }
}

九、注册model

public class RegisterModel implements RegisterContract.RegisterModel {
    @Override
    public Observable<RegisterEntity> Register(HashMap<String, String> map) {
        Observable<RegisterEntity> on = RetrofitUtils.getInstance()
                .createApi(Api.REGISTER, ZhuceApi.class)
                .Register(map)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        return on;
    }
}

十、注册p层

public class RegisterPresenter extends RegisterContract.RegisterPresenter {
    @Override
    public void register(HashMap<String, String> map) {
        mModel.Register(map).subscribe(new Consumer<RegisterEntity>() {
            @Override
            public void accept(RegisterEntity registerEntity) throws Exception {
                mView.success(registerEntity);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.failure("异常");
            }
        });
    }
}

七、注册页面

public class RegisterActivity extends BaseMvpActivity<RegisterContract.RegisterModel,RegisterContract.RegisterPresenter> implements RegisterContract.RegisterView {

    @BindView(R.id.reg_name)
    EditText regName;
    @BindView(R.id.reg_pwd)
    EditText regPwd;
    @BindView(R.id.reg_reg)
    Button regReg;

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public BasePresenter initPresenter() {
        return new RegisterPresenter();
    }

    @OnClick(R.id.reg_reg)
    public void Zhuce(){

        //生成随机数
        int suinum = new Random().nextInt(99) % (99 - 10 + 1) + 10;
        //Toast.makeText(this,""+suinum,Toast.LENGTH_SHORT).show();
        //获取到输入内容
        String name = regName.getText().toString();
        String pwd = regPwd.getText().toString();
        //加密
        String pwd2 = EncryptUtil.encrypt(pwd);//Toast.makeText(this,""+pwd2,Toast.LENGTH_SHORT).show();
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",name);
        map.put("pwd",pwd2);
        map.put("pwd2",pwd2);
        map.put("sex",1+"");
        map.put("email","[email protected]");
        map.put("nickName","fang"+suinum);

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){
            Toast.makeText(this,"密码或者账号不能为空",Toast.LENGTH_SHORT).show();
        }else {
            //进行注册
            Toast.makeText(this,""+map,Toast.LENGTH_LONG).show();
            presenter.register(map);
        }
    }

    //成功
    @Override
    public void success(RegisterEntity registerEntity) {
        String status = registerEntity.getStatus();
        //Toast.makeText(this,""+status,Toast.LENGTH_SHORT).show();
        String i="0000";
        //13900540011 123456789
        if (status.equals(i)){
            Toast.makeText(this,"注册成功->"+status,Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,registerEntity.getMessage()+"---",Toast.LENGTH_SHORT).show();
        }
    }
    //失败
    @Override
    public void failure(String error) {
        Toast.makeText(this,error,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void initView() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void fail(String msg) {

    }

}

相关标签: 注册