|
@@ -2,20 +2,267 @@ package com.sheishuo.app.login;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.EditText;
|
|
|
+import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
+import com.sheishuo.app.R;
|
|
|
+import com.sheishuo.app.common.util.net.INet;
|
|
|
+import com.sheishuo.app.common.util.net.NetImpl;
|
|
|
+import com.sheishuo.app.common.util.net.NetInfo;
|
|
|
+import com.sheishuo.app.common.util.net.ResponseCallback;
|
|
|
+import com.sheishuo.app.common.views.BaseToolbar;
|
|
|
import com.sheishuo.app.uikit_implements.SheishuoUI;
|
|
|
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import okhttp3.FormBody;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Created by KN on 2017/7/11.
|
|
|
*/
|
|
|
|
|
|
-public class RegisterActivity extends SheishuoUI {
|
|
|
+public class RegisterActivity extends SheishuoUI implements View.OnClickListener {
|
|
|
+
|
|
|
+ private String TAG = this.getClass().getSimpleName();
|
|
|
+ private Context context = this;
|
|
|
+ private EditText telET, codeET, pwdET, pwdConfirmET;
|
|
|
+ private TextView getCodeTV;
|
|
|
+ private Button registerBtn;
|
|
|
+ private BaseToolbar toolbar;
|
|
|
+ private JSONObject code;
|
|
|
+ private String msgCode;
|
|
|
+ private Handler handler;
|
|
|
+ private INet net = new NetImpl();
|
|
|
+
|
|
|
+ private int countDown = 60;
|
|
|
+
|
|
|
+
|
|
|
public static void start(Context context) {
|
|
|
- Intent intent = new Intent(context, LoginActivity.class);
|
|
|
+ Intent intent = new Intent(context, RegisterActivity.class);
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.register_layout);
|
|
|
+ findViews();
|
|
|
+ init();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void findViews() {
|
|
|
+ telET = $(R.id.register_tel);
|
|
|
+ codeET = $(R.id.register_code);
|
|
|
+ pwdET = $(R.id.register_password);
|
|
|
+ pwdConfirmET = $(R.id.register_confirm_password);
|
|
|
+ getCodeTV = $(R.id.register_get_code);
|
|
|
+ registerBtn = $(R.id.register_btn);
|
|
|
+ toolbar = $(R.id.toolbar);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init() {
|
|
|
+ toolbar.setTitle("注册");
|
|
|
+
|
|
|
+ getCodeTV.setOnClickListener(this);
|
|
|
+ registerBtn.setOnClickListener(this);
|
|
|
+
|
|
|
+ handler = new Handler(){
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ super.handleMessage(msg);
|
|
|
+ switch (msg.what){
|
|
|
+ case 0:
|
|
|
+ countDown--;
|
|
|
+ if (countDown > 0) {
|
|
|
+ getCodeTV.setText(countDown + "s后重发");
|
|
|
+ msg = handler.obtainMessage();
|
|
|
+ msg.what = 0;
|
|
|
+ handler.sendMessageDelayed(msg,1000);
|
|
|
+ }else {
|
|
|
+ countDown = 60;
|
|
|
+ getCodeTV.setEnabled(true);
|
|
|
+ getCodeTV.setText("获取验证码");
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private <T extends View> T $(int resId) {
|
|
|
+ return (T) findViewById(resId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showToast(final String toast) {
|
|
|
+ handler.post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ switch (v.getId()) {
|
|
|
+ case R.id.register_get_code:
|
|
|
+ getCode();
|
|
|
+ break;
|
|
|
+ case R.id.register_btn:
|
|
|
+ register();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取验证码
|
|
|
+ void getCode() {
|
|
|
+ showToast("已发送验证码,请注意查收");
|
|
|
+
|
|
|
+ startCountDown();
|
|
|
+
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ FormBody body = new FormBody.Builder()
|
|
|
+ .add("mobile", telET.getText().toString())
|
|
|
+ .build();
|
|
|
+ net.post(NetInfo.GET_CODE, body, new ResponseCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Object object) {
|
|
|
+ String result = (String) object;
|
|
|
+ try {
|
|
|
+ code = new JSONObject(result);
|
|
|
+ String codeData = code.getString("d");
|
|
|
+
|
|
|
+ JSONObject dataResult = new JSONObject(codeData);
|
|
|
+ if (200 == dataResult.getInt("code")) {
|
|
|
+ msgCode = dataResult.getString("obj");
|
|
|
+ } else {
|
|
|
+ showToast("获取验证码失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ showToast("获取验证码失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailed() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
+ //验证码倒计时
|
|
|
+ void startCountDown() {
|
|
|
+ getCodeTV.setEnabled(false);
|
|
|
+ handler.sendEmptyMessage(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ //注册
|
|
|
+ void register() {
|
|
|
+ String tel = telET.getText().toString().trim();
|
|
|
+ String code = codeET.getText().toString().trim();
|
|
|
+ String pwd = pwdET.getText().toString().trim();
|
|
|
+ String pwdConfirm = pwdConfirmET.getText().toString().trim();
|
|
|
+
|
|
|
+ if (tel.isEmpty() || code.isEmpty() || pwd.isEmpty() || pwdConfirm.isEmpty()) {
|
|
|
+ showToast("请将信息填写完整后注册");
|
|
|
+ } else if (!pwd.equals(pwdConfirm)) {
|
|
|
+ showToast("两次密码不一致");
|
|
|
+ } else {
|
|
|
+ registerBtn.setEnabled(false);
|
|
|
+ registerBtn.setText("注册中...");
|
|
|
+ new RegThread().start();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //注册成功
|
|
|
+ void registerSuccess() {
|
|
|
+ handler.post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ showToast("注册成功");
|
|
|
+ LoginActivity.start(context);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //注册失败
|
|
|
+ void registerFailed(@Nullable String failedMsg) {
|
|
|
+ if (failedMsg == null) failedMsg = "";
|
|
|
+ final String finalFailedMsg = failedMsg;
|
|
|
+ handler.post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ showToast("注册失败: " + finalFailedMsg);
|
|
|
+ registerBtn.setEnabled(true);
|
|
|
+ registerBtn.setText("注册");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //注册线程
|
|
|
+ private class RegThread extends Thread {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ super.run();
|
|
|
+ String tel = telET.getText().toString().trim();
|
|
|
+ String code = codeET.getText().toString().trim();
|
|
|
+ String pwd = pwdET.getText().toString().trim();
|
|
|
+
|
|
|
+ FormBody body = new FormBody.Builder()
|
|
|
+ .add("mobile", tel)
|
|
|
+ .add("pwd", pwd)
|
|
|
+ .add("code", code)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ net.post(NetInfo.REG_URL, body, new ResponseCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Object object) {
|
|
|
+ try {
|
|
|
+ String result = (String) object;
|
|
|
+ JSONObject jsonObject = new JSONObject(result);
|
|
|
+ if (0 == jsonObject.getInt("c")) {
|
|
|
+ registerSuccess();
|
|
|
+ } else {
|
|
|
+ registerFailed(jsonObject.getString("d"));
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ this.onFailed();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailed() {
|
|
|
+ registerFailed(null);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|