wpc
2018-11-27 680fbc9e73da3e11988557cf88fd935efd3e0b1e
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package com.haicaojie.android.ui.mine;
 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.util.MobileUtil;
import com.lcjian.library.util.SingleToast;
import com.lcjian.library.util.common.StringUtils;
import com.lcjian.library.widget.ClearEditText;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.haicaojie.android.BasicTextHttpResponseHandler;
import com.haicaojie.android.R;
import com.haicaojie.android.ShoppingApi;
import com.haicaojie.android.ShoppingApplication;
import com.haicaojie.android.entity.UserInfo;
import com.haicaojie.android.ui.BaseActivity;
import com.haicaojie.android.util.VerificationCodeErrorDialog;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
/**
 * Created by weikou2015 on 2018/5/29.
 * 手机登录
 */
 
public class MobileLoginActvity extends BaseActivity implements View.OnClickListener {
 
    private EditText et_verification_code;
    private ClearEditText et_mobile_num;
    private TextView tv_time, tv_login, tv_title;
    private ImageView iv_back;
    String title;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_mobile);
 
        /*
         * 计算状态栏高度并设置
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int result = 0;
            int resourceId = getResources().getIdentifier("status_bar_height",
                    "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
            //设置状态栏文字颜色及图标为深色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            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_mobile_num = (ClearEditText) findViewById(R.id.et_mobile_num);
        et_verification_code = (EditText) findViewById(R.id.et_verification_code);
        tv_time = (TextView) findViewById(R.id.tv_time_hint);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_login = (TextView) findViewById(R.id.tv_login_confirm);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_login.setEnabled(false);
        tv_login.setBackgroundResource(R.drawable.shape_mobile_login);
        title = getIntent().getStringExtra("title");
        et_mobile_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 
            }
 
            @Override
            public void afterTextChanged(Editable editable) {
                if (title.equalsIgnoreCase("登录")) {
                    if (editable.toString().length() == 11) {
                        tv_time.setEnabled(true);
                    } else {
                        tv_time.setEnabled(false);
                    }
                }
            }
        });
 
        et_verification_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 
            }
 
            @Override
            public void afterTextChanged(Editable editable) {
 
                if (editable.toString().length() == 6 && et_mobile_num.getText().toString().length() == 11) {
                    tv_login.setEnabled(true);
                    tv_login.setBackgroundResource(R.drawable.shape_mobile_login_light);
                } else {
                    tv_login.setEnabled(false);
                    tv_login.setBackgroundResource(R.drawable.shape_mobile_login);
                }
            }
 
        });
 
        if (!StringUtils.isEmpty(title)) {
            tv_title.setText(title);
        } else {
            tv_title.setText("登录");
        }
 
        findViewById(R.id.fl_verification_code).setVisibility(View.VISIBLE);
        tv_time.setOnClickListener(this);
        tv_login.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }
 
    /**
     * 获取手机验证码
     */
    private void getVerificationCode(String phone) {
        String uid = getSharedPreferences("user", MODE_PRIVATE).getString("uid", "0");
        ShoppingApi.sendSms(this, uid, phone, uid, new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (!jsonObject.optString("code").equalsIgnoreCase("0")) {
                    SingleToast.showToast(MobileLoginActvity.this, jsonObject.optString("msg"));
                } else {
                    new CountDownTimer(60 * 1000, 1000) {
                        @Override
                        public void onTick(long l) {
                            tv_time.setText(l / 1000 + "s");
                            tv_time.setEnabled(false);
                        }
 
                        @Override
                        public void onFinish() {
                            tv_time.setText("重新发送");
                            tv_time.setEnabled(true);
                        }
                    }.start();
                    SingleToast.showToast(MobileLoginActvity.this, "即将发送验证码,请稍侯...");
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }
        });
    }
 
    private boolean isIntent = false;
 
    @Override
    protected void onPause() {
        super.onPause();
        if (isIntent) {
            finish();
        }
    }
 
    /**
     * 登录
     */
    private void setLogin(String mobileNum, String verificationNum, String loginType) {
        boolean wxInstall = false;
        if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {//微信登录
            wxInstall = true;
        } else {
            wxInstall = false;
        }
        ShoppingApi.login(MobileLoginActvity.this, getSharedPreferences("user", MODE_PRIVATE).getBoolean("first", false), "", "", "", "", verificationNum, wxInstall, mobileNum, loginType, new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, final JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("1")) {
                        Gson gson = new GsonBuilder().serializeNulls().create();
                        final UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("user").toString(), new TypeToken<UserInfo>() {
                        }.getType());
                        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putBoolean("isLogin", true);
                        editor.putBoolean("isTBBind", true);
                        editor.putBoolean("isFirstInput", false);
                        editor.putString("uid", info.getId());
                        MiPushClient.setAlias(MobileLoginActvity.this, info.getId(), null);
 
                        if (StringUtils.isEmpty(info.getOpenid())) {
                            editor.putBoolean("isTBBind", false);
                        } else {
                            editor.putBoolean("isTBBind", true);
                        }
                        if (StringUtils.isEmpty(info.getWxOpenId())) {
                            editor.putBoolean("isWxBind", false);
                        } else {
                            editor.putBoolean("isWxBind", true);
                        }
                        editor.commit();
                        SingleToast.showToast(MobileLoginActvity.this, "登录成功!");
                        finish();
                    } else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("2")) {
                        VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
                        builder.setMessage("需要绑定微信账号").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {//微信登录
                                    SendAuth.Req req = new SendAuth.Req();
                                    req.scope = "snsapi_userinfo";
                                    req.state = "haicaojie";
                                    ShoppingApplication.application.mWxApi.sendReq(req);
                                } else {
                                    SingleToast.showToast(MobileLoginActvity.this, "请先安装微信客户端!");
                                }
                                dialogInterface.dismiss();
                                finish();
                            }
                        }).setNegativeButton("", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                AlibcLogin.getInstance().logout(new AlibcLoginCallback() {
                                    @Override
                                    public void onSuccess(int i) {
 
                                    }
 
                                    @Override
                                    public void onFailure(int i, String s) {
 
                                    }
                                });
                                dialogInterface.dismiss();
                                SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();
                                editor.putBoolean("first", true);
                                editor.commit();
                            }
                        }).create().show();
                    } else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("3")) {
                        Gson gson = new GsonBuilder().serializeNulls().create();
                        final UserInfo mainUser = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("mainUser").toString(), new TypeToken<UserInfo>() {
                        }.getType());
                        final UserInfo lessUser = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("lessUser").toString(), new TypeToken<UserInfo>() {
                        }.getType());
                        final UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("user").toString(), new TypeToken<UserInfo>() {
                        }.getType());
                        Intent intent = new Intent(MobileLoginActvity.this, MergeAccountActivity.class);
                        intent.putExtra("mainUser", mainUser);
                        intent.putExtra("lessUser", lessUser);
                        intent.putExtra("user", info);
                        startActivity(intent);
 
                    } else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("4")) {
                        VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
                        builder.setMessage("您将使用该手机号").setTitle("温馨提示").setPositiveButton("创建新账号", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {//微信登录
                                dialogInterface.dismiss();
                                registeAccount();
                            }
                        }).setNegativeButton("", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                finish();
                            }
                        }).create().show();
                    }
                } else {
                    VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
                    builder.setMessage("验证码错误").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).create().show();
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                getSharedPreferences("user", MODE_PRIVATE).edit().putBoolean("first", false).commit();
            }
        });
    }
 
    private void registeAccount() {
        ShoppingApi.register(MobileLoginActvity.this, "", "", "", et_verification_code.getText().toString(),
                et_mobile_num.getText().toString(), new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                        if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                            Gson gson = new GsonBuilder().serializeNulls().create();
                            final UserInfo info = gson.fromJson(jsonObject.getJSONObject("data").getJSONObject("user").toString(), new TypeToken<UserInfo>() {
                            }.getType());
                            SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putBoolean("isLogin", true);
                            editor.putBoolean("isTBBind", true);
                            editor.putBoolean("isFirstInput", false);
                            editor.putString("uid", info.getId());
                            MiPushClient.setAlias(MobileLoginActvity.this, info.getId(), null);
 
                            if (StringUtils.isEmpty(info.getOpenid())) {
                                editor.putBoolean("isTBBind", false);
                            } else {
                                editor.putBoolean("isTBBind", true);
                            }
                            if (StringUtils.isEmpty(info.getWxOpenId())) {
                                editor.putBoolean("isWxBind", false);
                            } else {
                                editor.putBoolean("isWxBind", true);
                            }
                            editor.commit();
                            SingleToast.showToast(MobileLoginActvity.this, "注册成功!");
                            finish();
                        } else if (jsonObject.optString("code").equalsIgnoreCase("90001")) {
                            VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
                            builder.setMessage("验证码错误").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            }).create().show();
                        } else {
                            SingleToast.showToast(MobileLoginActvity.this, jsonObject.optString("msg"));
                        }
                    }
                });
    }
 
    private void bindPhone(String code, String phone) {
        String uid = getSharedPreferences("user", MODE_PRIVATE).getString("uid", "0");
        ShoppingApi.bindPhone(this, uid, code, phone, new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    SingleToast.showToast(MobileLoginActvity.this, "绑定成功!");
                    finish();
                } else if (jsonObject.optString("code").equalsIgnoreCase("90001")) {
                    VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
                    builder.setMessage("验证码错误").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).create().show();
                } else {
                    SingleToast.showToast(MobileLoginActvity.this, jsonObject.optString("msg"));
                }
            }
        });
    }
 
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_time_hint:
                if (StringUtils.isEmpty(et_mobile_num.getText().toString())) {
                    SingleToast.showToast(MobileLoginActvity.this, "手机号码不能为空");
                }
                if (et_mobile_num.getText().toString().length() == 11) {
                    getVerificationCode(et_mobile_num.getText().toString());
                } else {
                    SingleToast.showToast(MobileLoginActvity.this, "请输入正确的手机号码");
                }
                break;
            case R.id.tv_login_confirm:
                if (et_mobile_num.getText().toString().length() != 11) {
                    SingleToast.showToast(MobileLoginActvity.this, "请输入正确手机号码");
                    break;
                }
                if (!StringUtils.isEmpty(et_verification_code.getText().toString())) {
                    if (title.equalsIgnoreCase("登录")) {
                        setLogin(et_mobile_num.getText().toString(), et_verification_code.getText().toString(), "3");
                    } else if (title.equalsIgnoreCase("绑定")) {
                        bindPhone(et_verification_code.getText().toString(), et_mobile_num.getText().toString());
                    }
                } else {
                    SingleToast.showToast(MobileLoginActvity.this, "验证码不能为空");
                }
                break;
        }
    }
}