admin
2021-07-15 49982f5a1a305c0cc7da04735e1c604b802d2a22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.yeshi.base.ui;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.lcjian.library.util.SingleToast;
import com.lcjian.library.util.common.StringUtils;
import com.yeshi.base.utils.BaseApiUtil;
import com.yeshi.base.utils.IsEmail;
import com.yeshi.base.utils.http.BasicTextHttpResponseHandler;
import com.yeshi.ec.base.R;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
 
/**
 * Created by weikou2015 on 2016/10/27.
 */
 
public class RegisterActivity extends BaseActivity implements
        View.OnClickListener {
 
    private LinearLayout ll_login_content;
    private ImageView iv_register_top;
    private EditText et_email;
    private EditText et_pwd;
    private EditText et_verfication_code;
    private TextView tv_obtain_verfication_code;
    private CheckBox cb_user_agreement;
    private TextView tv_confirm;
 
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_register);
        iv_register_top = (ImageView) findViewById(R.id.iv_register_top);
        et_email = (EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        et_verfication_code = (EditText) findViewById(R.id.et_verfication_code);
        tv_obtain_verfication_code = (TextView) findViewById(R.id.tv_obtain_verfication_code);
        cb_user_agreement = (CheckBox) findViewById(R.id.cb_user_agreement);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        ll_login_content = (LinearLayout) findViewById(R.id.ll_login_content);
 
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        ll_login_content.measure(w, h);
        int width = ll_login_content.getMeasuredWidth();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
                (int) (width * 0.434));
        params.gravity = Gravity.CENTER_HORIZONTAL;
        iv_register_top.setLayoutParams(params);
 
        et_email.addTextChangedListener(new TextWatcher() {
 
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
 
            }
 
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
 
            }
 
            @Override
            public void afterTextChanged(Editable arg0) {
                // et_email.getText().toString()
                if (!StringUtils.isEmpty(et_verfication_code.getText()
                        .toString())
                        && !StringUtils.isEmpty(et_pwd.getText().toString())
                        && !StringUtils.isEmpty(arg0.toString())) {
                    tv_confirm.setBackground(getResources().getDrawable(
                            R.drawable.shape_login2));
                } else {
                    tv_confirm.setBackground(getResources().getDrawable(
                            R.drawable.shape_login1));
                }
 
                if (!StringUtils.isEmpty(arg0.toString())) {
                    tv_obtain_verfication_code.setBackground(getResources()
                            .getDrawable(R.drawable.shape_login2));
                } else {
                    tv_obtain_verfication_code.setBackground(getResources()
                            .getDrawable(R.drawable.shape_login1));
                }
 
            }
        });
        et_verfication_code.addTextChangedListener(new TextWatcher() {
 
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
 
            }
 
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
 
            }
 
            @Override
            public void afterTextChanged(Editable arg0) {
                if (!StringUtils.isEmpty(et_email.getText().toString())
                        && !StringUtils.isEmpty(et_pwd.getText().toString())
                        && !StringUtils.isEmpty(arg0.toString())) {
                    tv_confirm.setBackground(getResources().getDrawable(
                            R.drawable.shape_login2));
                } else {
                    tv_confirm.setBackground(getResources().getDrawable(
                            R.drawable.shape_login1));
                }
 
            }
        });
 
        et_pwd.addTextChangedListener(new TextWatcher() {
 
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
 
            }
 
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
 
            }
 
            @Override
            public void afterTextChanged(Editable arg0) {
                if (!StringUtils.isEmpty(et_verfication_code.getText()
                        .toString())
                        && !StringUtils.isEmpty(et_email.getText().toString())
                        && !StringUtils.isEmpty(arg0.toString())) {
                    tv_confirm.setBackground(getResources().getDrawable(
                            R.drawable.shape_login2));
                } else {
                    tv_confirm.setBackground(getResources().getDrawable(
                            R.drawable.shape_login1));
                }
            }
        });
 
        tv_obtain_verfication_code.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);
        findViewById(R.id.tv_user_agreement).setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_back) {
            finish();
        } else if (id == R.id.tv_user_agreement) {
            Intent intent = new Intent(RegisterActivity.this,
                    BrowserActivity.class);
            intent.putExtra("url", getString(R.string.agreement));
            startActivity(intent);
        } else if (id == R.id.tv_obtain_verfication_code) {
            String email = et_email.getText().toString();
            if (StringUtils.isEmpty(email)) {
                SingleToast.showToast(RegisterActivity.this, "请先输入邮箱账号");
            } else {
                Matcher m = IsEmail.isEmailAddress().matcher(email);
                if (m.matches()) {
                    getVerficationCode(email);
                } else {
                    SingleToast.showToast(RegisterActivity.this,
                            "输入的邮箱账号有误,请查证!");
                    return;
                }
            }
        } else if (id == R.id.tv_confirm) {
            String email1 = et_email.getText().toString();
            String pwd = et_pwd.getText().toString();
            String verficationCode = et_verfication_code.getText().toString();
            if (StringUtils.isEmpty(email1)) {
                SingleToast.showToast(RegisterActivity.this, "请先输入邮箱账号");
                return;
            } else {
                Matcher m = IsEmail.isEmailAddress().matcher(email1);
                if (!m.matches()) {
                    SingleToast.showToast(RegisterActivity.this,
                            "输入的邮箱账号有误,请查证!");
                    return;
                }
            }
            if (StringUtils.isEmpty(pwd)) {
                SingleToast.showToast(RegisterActivity.this, "密码不能为空");
                return;
            }
            if (pwd.length() < 6) {
                SingleToast.showToast(RegisterActivity.this, "密码长度不能低于6位");
                return;
            }
            if (StringUtils.isEmpty(verficationCode)) {
                SingleToast.showToast(RegisterActivity.this, "验证码不能为空");
                return;
            }
            userRegister();
        }
    }
 
    private void userRegister() {
        SharedPreferences sp = getSharedPreferences("user",
                Context.MODE_PRIVATE);
        final String uid = sp.getString("uid", "");
        BaseApiUtil.emailRegister(this, uid, et_email.getText().toString(),
                et_pwd.getText().toString(), et_verfication_code.getText()
                        .toString(), "无名氏", new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.optBoolean("IsPost")) {
                            SingleToast.showToast(RegisterActivity.this,
                                    "注册成功!");
                            emailLogin(et_email.getText().toString(), et_pwd
                                    .getText().toString(), uid);
                        } else {
                            SingleToast.showToast(RegisterActivity.this,
                                    jsonObject.optString("Error") + ",注册失败!");
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
                        SingleToast.showToast(RegisterActivity.this,
                                "注册失败,请重新注册");
                    }
                });
    }
 
    /**
     * 注册成功后登陆,获取登录信息
     */
    /**
     * Email登陆
     */
    private void emailLogin(String email, String pwd, String uid) {
        BaseApiUtil.emailLogin(RegisterActivity.this, uid, email, pwd,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.optBoolean("IsPost")) {
                            SharedPreferences sp = getSharedPreferences("user",
                                    MODE_PRIVATE);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putString("portrait", jsonObject
                                    .optJSONObject("Data")
                                    .optString("Portrait"));
                            edit.putString("LoginUid", jsonObject
                                    .getJSONObject("Data").getString("Id"));
                            edit.putString(
                                    "name",
                                    jsonObject.optJSONObject("Data").optString(
                                            "NickName"));
                            edit.commit();
                            SingleToast
                                    .showToast(RegisterActivity.this, "登陆成功");
                            Intent intent = new Intent(RegisterActivity.this,
                                    PersonInfoActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            SingleToast
                                    .showToast(RegisterActivity.this, "登陆失败");
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
                        SingleToast.showToast(RegisterActivity.this, "登陆失败");
                    }
                });
 
    }
 
    Timer timer;
    int timecount = 59;
 
    private void getVerficationCode(String email) {
        timer = new Timer();
        timer.schedule(new MyTask(), 1000, 1000);
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        String uid = sp.getString("uid", "");
        BaseApiUtil.getVerficationCode(RegisterActivity.this, uid, email,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.optBoolean("IsPost")) {
                            SingleToast.showToast(RegisterActivity.this,
                                    "获取验证码成功,请打开邮箱查看!");
                        } else {
                            SingleToast.showToast(RegisterActivity.this,
                                    "获取验证码失败,请重新获取!");
                            handler.sendEmptyMessage(1);
                        }
                    }
                });
    }
 
    class MyTask extends TimerTask {
        @Override
        public void run() {
            if (timecount > 0) {
                handler.sendEmptyMessage(0);
            } else {
                handler.sendEmptyMessage(1);
            }
        }
    }
 
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 0:
                tv_obtain_verfication_code.setClickable(false);
                tv_obtain_verfication_code.setText("重新发送(" + timecount + "s)");
                timecount--;
                break;
            case 1:
                timer.cancel();
                tv_obtain_verfication_code.setText("获取验证码");
                tv_obtain_verfication_code.setClickable(true);
                timecount = 59;
                break;
            }
        }
    };
}