InputPhoneActivity.java 12.8 KB
Newer Older
1
package com.yidianling.user.ui;
konghaorui committed
2 3 4 5 6 7 8 9 10 11

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;
12 13 14 15
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
import com.ydl.ydlcommon.base.BaseActivity;
import com.ydl.ydlcommon.base.config.YDLConstants;
16
import com.ydl.ydlcommon.bean.StatusBarOptions;
17 18 19 20 21 22 23 24 25
import com.ydl.ydlcommon.data.http.RxUtils;
import com.ydl.ydlcommon.data.http.ThrowableConsumer;
import com.ydl.ydlcommon.router.YdlCommonOut;
import com.ydl.ydlcommon.utils.log.LogHelper;
import com.ydl.ydlcommon.utils.remind.ToastHelper;
import com.ydl.ydlcommon.view.DeleteEditTextView;
import com.ydl.ydlcommon.view.JumpTextView;
import com.ydl.ydlcommon.view.RoundCornerButton;
import com.ydl.ydlcommon.view.dialog.CommonDialog;
konghaorui committed
26
import com.yidianling.common.tools.ToastUtil;
27 28 29 30 31 32 33 34 35 36
import com.yidianling.user.R;
import com.yidianling.user.UserConstants;
import com.yidianling.user.http.UserHttp;
import com.yidianling.user.http.UserHttpImpl;
import com.yidianling.user.http.request.CodeParam;
import com.yidianling.user.http.request.ExistParam;
import com.yidianling.user.http.response.ExistResponse;
import com.yidianling.user.ui.login.RegisterAndLoginActivity;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
konghaorui committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;


/**
 * 注册手机号或绑定手机号
 * Created by softrice on 15/11/23.
 */
public class InputPhoneActivity extends BaseActivity implements View.OnClickListener {

    JumpTextView jtv_country;
    DeleteEditTextView detv_phone;
    TextView tv_protocol;
    LinearLayout ll_protocol;
    RoundCornerButton rcb_next;

    TextView tvFreePacketHint;

    private String country_code = "0086";
    private String country_name = "中国";
    private String FORMAT = "%s   +%s";

    String phoneNum;

    String smsAction = UserConstants.REGISTER_ACTION;//验证码类型,注册:register找回密码:forget 绑定手机 wxbind
    private boolean isFromSplash;

    private boolean isRegisterFromRedPacket;

    private static final String SMS_ACTION = "smsAction";
    private static final String PHONE = "phone";
    private static final String ISFROMSPLASH = "isFromSplash";

    public static Intent newIntent(Activity activity, String smsAction) {
        Intent intent = new Intent(activity, InputPhoneActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(SMS_ACTION, smsAction);
75
        intent.putExtra(YDLConstants.BUNDLE, bundle);
konghaorui committed
76 77 78 79 80 81 82 83 84
        return intent;
    }

    public static void start(Activity activity, String smsAction, String phone, boolean isFromSplash) {
        Intent intent = new Intent(activity, InputPhoneActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(SMS_ACTION, smsAction);
        bundle.putString(PHONE, phone);
        bundle.putBoolean(ISFROMSPLASH, isFromSplash);
85
        intent.putExtra(YDLConstants.BUNDLE, bundle);
konghaorui committed
86 87 88 89 90 91 92 93
        activity.startActivity(intent);
    }

    public static void start(Activity activity, String smsAction, boolean isRegisterFromRedPacket) {
        Intent intent = new Intent(activity, InputPhoneActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(SMS_ACTION, smsAction);
        bundle.putBoolean(UserConstants.IS_REGISTER_FROM_PACKET, isRegisterFromRedPacket);
94
        intent.putExtra(YDLConstants.BUNDLE, bundle);
konghaorui committed
95 96 97
        activity.startActivity(intent);
    }

98 99 100 101 102 103
    @NotNull
    @Override
    public StatusBarOptions getStatusViewOptions() {
        return new StatusBarOptions(true,true);
    }

konghaorui committed
104 105
    @Override
    protected int layoutResId() {
106
        return R.layout.user_activity_input_phone;
konghaorui committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }

    @Override
    protected void initDataAndEvent() {
        jtv_country = findViewById(R.id.jtv_country);
        detv_phone = findViewById(R.id.detv_phone);
        tv_protocol = findViewById(R.id.tv_protocol);
        ll_protocol = findViewById(R.id.ll_protocol);
        tvFreePacketHint = findViewById(R.id.tv_free_packet_hint);
        rcb_next = findViewById(R.id.rcb_next);
        jtv_country.setOnClickListener(this);
        tv_protocol.setOnClickListener(this);
        rcb_next.setOnClickListener(this);

        String action = null;
122
        Bundle bundle = getIntent().getBundleExtra(YDLConstants.BUNDLE);
konghaorui committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        if (bundle != null) {
            action = bundle.getString(SMS_ACTION);
            LogHelper.Companion.getInstance().writeLogSync(String.format("%s页面创建", action));
            phoneNum = bundle.getString(PHONE);
            isFromSplash = bundle.getBoolean(ISFROMSPLASH, false);
            isRegisterFromRedPacket = bundle.getBoolean(UserConstants.IS_REGISTER_FROM_PACKET, false);
        } else {
            ToastUtil.toastShort("请稍后重试");
            finish();
        }
        if (action == null) {
            smsAction = UserConstants.REGISTER_ACTION;
        } else {
            smsAction = action;
        }
        detv_phone.requestFocus();
        InputMethodManager manager = (InputMethodManager) detv_phone.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager != null) {
            manager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
        }
        init();

        Observable.timer(300, TimeUnit.MILLISECONDS)
                .subscribe(aLong -> {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(detv_phone, 0);
                }, t -> {
                });

    }


    void init() {
        if (isRegisterFromRedPacket) {
            tvFreePacketHint.setVisibility(View.VISIBLE);
        }
        jtv_country.setLeftText(String.format(FORMAT, country_name, country_code));
        if (smsAction.equals(UserConstants.REGISTER_ACTION)) {
            ll_protocol.setVisibility(View.VISIBLE);
        } else {
            ll_protocol.setVisibility(View.INVISIBLE);
        }
        if (!TextUtils.isEmpty(phoneNum)) {
            detv_phone.setText(phoneNum);
        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.jtv_country) {
            startActivityForResult(new Intent(this, CountryListActivity.class), 44);

        } else if (i == R.id.tv_protocol) {
            NewH5Activity.start(this, new H5Params("http://www.yidianling.com/agreement/reg", "用户协议"));

        } else if (i == R.id.rcb_next) {
            if ("0086".equals(country_code)) {
                if (detv_phone.getText().toString().length() != 11) {
                    ToastHelper.Companion.show("请输入11位手机号");
                    return;
                }
            } else {
                if (TextUtils.isEmpty(detv_phone.getText())) {
                    YdlCommonOut.Companion.showToast("请输入正确的手机号");
                    return;
                }
            }
            if (TextUtils.isEmpty(detv_phone.getText().toString())) {
                ToastHelper.Companion.show("请输入手机号!");
                return;
            }
            switch (smsAction) {
                case UserConstants.BIND_PHONE_ACTION://绑定手机
                    judge(UserConstants.BIND_PHONE_ACTION);
                    break;
                case UserConstants.REGISTER_ACTION://注册
                    judge(UserConstants.REGISTER_ACTION);
                    break;

                case UserConstants.FORGET_ACTION://忘记密码
                    judge(UserConstants.FORGET_ACTION);
                    break;

                case UserConstants.SIGNIN_ACTION:// 短信快捷登录
                    judge(UserConstants.SIGNIN_ACTION);
                    break;
            }

        }
    }


    private void judge(final String action) {

        UserHttp userHttp = UserHttpImpl.Companion.getInstance();
        userHttp.phoneExist(new ExistParam(country_code, detv_phone.getText().toString()))
                .compose(RxUtils.resultData())
                .map(ExistResponse::isExist)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> showProgressDialog(""))
                .doAfterTerminate(this::dismissProgressDialog)
                .subscribe(isExist -> {
                    LogHelper.Companion.getInstance().writeLogSync(String.format("%s成功", action));
                    switch (action) {
                        case UserConstants.BIND_PHONE_ACTION:
                            if (isExist == 1) {
                                getCode(action, true);
                            } else {
                                getCode(action, false);
                            }
                            break;
                        case UserConstants.REGISTER_ACTION:
                            if (isExist == 1) {
                                new CommonDialog(InputPhoneActivity.this)
                                        .setMessage("该手机号已注册")
                                        .setRightClick("登录", v -> {
                                            startActivity(new Intent(InputPhoneActivity.this, RegisterAndLoginActivity.class));
                                            finish();
                                        })
                                        .setLeftOnclick("取消", null)
                                        .show();
                            } else {
                                getCode(action, false);
                            }
                            break;
                        case UserConstants.FORGET_ACTION:
                            if (isExist == 0) {
                                ToastHelper.Companion.show("该手机还未注册");
                            } else {
                                getCode(action, true);
                            }
                            break;
                        case UserConstants.SIGNIN_ACTION:
                            if (isExist == 0) {
                                getCode(UserConstants.REGISTER_ACTION, false);
                            } else {
                                SmsLoginActivity.Companion.start(this, country_code, detv_phone.getText().toString(), isFromSplash);
                                finish();
                            }
                            break;
                    }
                }, new ThrowableConsumer() {
                    @Override
                    public void accept(@NotNull String msg) {
                        ToastHelper.Companion.show(msg);
                    }
                });

    }

    private void getCode(final String action, final boolean phoneIsExits) {

        UserHttp userHttp = UserHttpImpl.Companion.getInstance();
        userHttp.code(new CodeParam(country_code, detv_phone.getText().toString(), action))
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> showProgressDialog(""))
                .doAfterTerminate(this::dismissProgressDialog)
                .subscribe(o -> {
                    LogHelper.Companion.getInstance().writeLogSync("获取验证码成功");
                    Intent intent = new Intent(InputPhoneActivity.this, GetIdentifyingCodeActivity.class);
                    intent.putExtra("action", action);
                    intent.putExtra("code", country_code);
                    intent.putExtra("isRegisted", phoneIsExits);
                    intent.putExtra("isFromSplash", isFromSplash);
                    intent.putExtra("lastGetCodeTime", (int) (System.currentTimeMillis() / 1000));
                    intent.putExtra("phoneNum", detv_phone.getText().toString());
                    intent.putExtra(UserConstants.IS_REGISTER_FROM_PACKET, isRegisterFromRedPacket);
                    startActivity(intent);
                    finish();
                }, new ThrowableConsumer() {
                    @Override
                    public void accept(@NotNull String msg) {
                        ToastHelper.Companion.show(msg);
                        LogHelper.Companion.getInstance().writeLogSync(String.format("获取验证码失败:%s", msg));
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 44 && resultCode == 45) {
            country_code = data.getStringExtra("code");
            country_name = data.getStringExtra("name");
            jtv_country.setLeftText(String.format("%s   +%s", country_name, country_code));
        }
    }

}