admin
2021-02-03 1981dee5aec45793d3c4ebdbc4e637528c71b3c5
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
package com.weikou.beibeivideo.ui.login;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.ui.BaseActivity;
import com.weikou.beibeivideo.ui.mine.BrowserActivity;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.IsEmail;
import com.weikou.beibeivideo.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 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);
        /*
         * 计算状态栏高度并设置
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int result = 0;
            int resourceId = getResources().getIdentifier("status_bar_height",
                    "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    result);
            findViewById(R.id.v_status_bar).setLayoutParams(params);
        } else {
            findViewById(R.id.v_status_bar).setVisibility(View.GONE);
        }
        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);
        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) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_user_agreement:
                Intent intent = new Intent(RegisterActivity.this, BrowserActivity.class);
                intent.putExtra("url", BeibeiConstant.USER_AGREEMENT);
                startActivity(intent);
                break;
            case R.id.tv_obtain_verfication_code:
                String email = et_email.getText().toString();
                if (StringUtils.isEmpty(email)) {
                    SingleToast.showToast(RegisterActivity.this, "请先输入邮箱账号");
                    break;
                }
 
                if (!email.endsWith("@qq.com")) {
                    SingleToast.showToast(RegisterActivity.this, "请使用QQ邮箱注册");
                    break;
                }
 
                Matcher m = IsEmail.isEmailAddress().matcher(email);
                if (m.matches()) {
                    tv_obtain_verfication_code.setEnabled(false);
                    getVerficationCode(email);
                } else {
                    SingleToast.showToast(RegisterActivity.this, "输入的邮箱账号有误,请查证!");
                    return;
                }
 
                break;
 
            case R.id.tv_confirm:
                if (!cb_user_agreement.isChecked()) {
                    SingleToast.showToast(RegisterActivity.this, "请同意《用户使用协议》");
                    break;
                }
 
                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, "请先输入邮箱账号");
                    break;
                } else {
                    if (!email1.endsWith("@qq.com")) {
                        SingleToast.showToast(RegisterActivity.this, "请使用QQ邮箱注册");
                        break;
                    }
 
                    Matcher mc = IsEmail.isEmailAddress().matcher(email1);
                    if (!mc.matches()) {
                        SingleToast.showToast(RegisterActivity.this, "输入的邮箱账号有误,请查证!");
                        break;
                    }
                }
                if (StringUtils.isEmpty(pwd)) {
                    SingleToast.showToast(RegisterActivity.this, "密码不能为空");
                    break;
                }
                if (pwd.length() < 6) {
                    SingleToast.showToast(RegisterActivity.this, "密码长度不能低于6位");
                    break;
                }
                if (StringUtils.isEmpty(verficationCode)) {
                    SingleToast.showToast(RegisterActivity.this, "验证码不能为空");
                    break;
                }
                userRegister();
                break;
        }
    }
 
    private void userRegister() {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        final String uid = sp.getString("uid", "");
        BeibeiVideoAPI.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) {
        BeibeiVideoAPI.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", "");
        BeibeiVideoAPI.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);
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                tv_obtain_verfication_code.setEnabled(true);
            }
        });
    }
 
    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;
            }
        }
    };
}