admin
2021-07-20 27bd1f81221b8c8e8047118a64c2beb7bc214bbb
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
package com.yeshi.base.ui;
 
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
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.base.R;
 
/**
 * 忘记密码,修改密码 Created by weikou2015 on 2016/10/27.
 */
 
public class ForgetPwdActivity extends BaseActivity implements
        View.OnClickListener {
 
    private EditText et_email;
    private EditText et_verfication_code;
    private TextView tv_obtain_verfication_code;
    private EditText et_pwd;
    private TextView tv_confirm;
 
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_forget_pwd);
        et_email = (EditText) findViewById(R.id.et_email);
        et_verfication_code = (EditText) findViewById(R.id.et_verfication_code);
        tv_obtain_verfication_code = (TextView) findViewById(R.id.tv_obtain_verfication_code);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
 
        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);
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_obtain_verfication_code) {
            String email = et_email.getText().toString();
            if (StringUtils.isEmpty(email)) {
                SingleToast.showToast(ForgetPwdActivity.this, "请先输入邮箱号");
                return;
            } else {
                Matcher m = IsEmail.isEmailAddress().matcher(email);
                if (m.matches()) {
                    getVerficationCode(email);
                } else {
                    SingleToast.showToast(ForgetPwdActivity.this,
                            "输入的邮箱账号有误,请查证!");
                    return;
                }
            }
        } else if (id == R.id.tv_confirm) {
            String str = et_email.getText().toString();
            if (StringUtils.isEmpty(str)) {
                SingleToast.showToast(ForgetPwdActivity.this, "请先输入邮箱号");
                return;
            } else {
                Matcher m = IsEmail.isEmailAddress().matcher(str);
                if (!m.matches()) {
                    SingleToast.showToast(ForgetPwdActivity.this,
                            "输入的邮箱账号有误,请查证!");
                    return;
                }
            }
            if (StringUtils.isEmpty(et_pwd.getText().toString())) {
                SingleToast.showToast(ForgetPwdActivity.this, "密码不能为空!");
                return;
            }
            if (et_pwd.getText().toString().length() < 6) {
                SingleToast.showToast(ForgetPwdActivity.this, "密码长度不能低于6位!");
                return;
            }
            revisePwd();
        } else if (id == R.id.tv_back) {
            finish();
        }
    }
 
    Timer timer;
    int timecount = 59;
 
    /**
     * 修改密码
     */
    private void revisePwd() {
        SharedPreferences sp = getSharedPreferences("user",
                Context.MODE_PRIVATE);
        String uid = sp.getString("uid", "");
        BaseApiUtil.revisePwd(this, uid, et_email.getText().toString(),
                et_verfication_code.getText().toString(), et_pwd.getText()
                        .toString(), new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.optBoolean("IsPost")) {
                            SingleToast.showToast(ForgetPwdActivity.this,
                                    "修改密码成功!");
                            ForgetPwdActivity.this.finish();
                        } else {
                            SingleToast.showToast(ForgetPwdActivity.this,
                                    jsonObject.optString("Error") + ",修改密码失败!");
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
                        SingleToast.showToast(ForgetPwdActivity.this, "修改密码失败");
                    }
                });
    }
 
    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(ForgetPwdActivity.this, uid, email,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.optBoolean("IsPost")) {
                            SingleToast.showToast(ForgetPwdActivity.this,
                                    "获取验证码成功,请打开邮箱查看!");
                        } else {
                            SingleToast.showToast(ForgetPwdActivity.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;
            }
        }
    };
}