admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package com.tejia.lijin.app.ui.mine;
 
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
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 android.widget.Toast;
 
import com.ali.auth.third.core.model.Session;
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.tejia.lijin.app.util.ui.dialog.DialogUtil;
import com.tejia.lijin.app.util.user.LoginManager;
import com.umeng.analytics.MobclickAgent;
import com.wpc.library.util.GetManifestDataUtil;
import com.wpc.library.util.common.StringUtils;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.entity.UserInfo;
import com.tejia.lijin.app.entity.eventbus.WXLoginEvent;
import com.tejia.lijin.app.ui.dialog.ShapeLoadingDialog;
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
import com.tejia.lijin.app.util.KeFuUtil;
import com.tejia.lijin.app.util.SystemParamsUtil;
import com.tejia.lijin.app.util.TopStatusSettings;
import com.tejia.lijin.app.util.umengCustomEvent.UserCustomEvent;
import com.tejia.lijin.app.util.user.UserUtil;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.Timer;
import java.util.TimerTask;
 
import de.greenrobot.event.EventBus;
 
/**
 * Created by weikou2015 on 2017/12/15.
 */
 
public class LoginActivity extends Activity implements View.OnClickListener {
 
    public final static int TYPE_LOGIN = 1;//登录
    public final static int TYPE_BIND = 2;//绑定
    public final static int TYPE_BIND_WITHOUT_LOGINED = 3;//未登录绑定
    private ImageView iv_close, iv_contact_kefu, iv_login_tb;
    private TextView tv_user_agreement, tv_privacy_policies, tv_login;
    LinearLayout ll_user_agreement;
    ShapeLoadingDialog pd;
    String uid;
 
    int type;
    String key;//微信登录尚未绑定手机时回传的标识
 
    private static String TAG = "LoginSelectActivity";
    private LinearLayout ll_phone;
    private TextView tv_send_code, tv_login_phone;
    private EditText et_phone, et_code;
 
    //设置视图
    private void setView(boolean onlining) {
        if (onlining) {
            ll_phone.setVisibility(View.VISIBLE);
            tv_login.setVisibility(View.GONE);
        } else {
            ll_phone.setVisibility(View.GONE);
            tv_login.setVisibility(View.VISIBLE);
        }
    }
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pd = new ShapeLoadingDialog.Builder(this).build();
        TopStatusSettings.setStatusViewAndDeepColorNoView(this);
        initData(getIntent());
        String from = getIntent().getStringExtra("from");
        UserCustomEvent.userLogin(this, StringUtils.isEmpty(from) ? "其它" : from);
        EventBus.getDefault().register(this);
        String onlining = "";
        String channel = GetManifestDataUtil.getAppMetaData(this, "UMENG_CHANNEL");
//        if ("QQ".equalsIgnoreCase(channel)) {
        onlining = SystemParamsUtil.getParam(this, "onlining");
//        }
 
        setView("1".equalsIgnoreCase(onlining) ? true : false);
    }
 
    private void initData(Intent intent) {
        type = intent.getIntExtra("type", TYPE_LOGIN);
        init(intent);
        addListener();
    }
 
    Timer timer = null;
 
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "onNewIntent");
        initData(intent);
    }
 
    /*
        初始化控件
         */
    private void init(Intent intent) {
        ll_phone = findViewById(R.id.ll_phone);
        tv_send_code = findViewById(R.id.tv_send_code);
        tv_login_phone = findViewById(R.id.tv_login_phone);
        et_phone = findViewById(R.id.et_phone);
        et_code = findViewById(R.id.et_code);
        iv_close = findViewById(R.id.iv_close);
        iv_contact_kefu = findViewById(R.id.iv_contact_kefu);
        tv_user_agreement = findViewById(R.id.tv_user_agreement);
        iv_login_tb = findViewById(R.id.iv_login_tb);
        tv_privacy_policies = findViewById(R.id.tv_privacy_policies);
        iv_login_tb = findViewById(R.id.iv_login_tb);
        ll_user_agreement = findViewById(R.id.ll_user_agreement);
        tv_login = findViewById(R.id.tv_login);
        tv_login.setOnClickListener(this);
        iv_login_tb.setOnClickListener(this);
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        sp.edit().putBoolean("first", true).commit();
        uid = intent.getStringExtra("uid");
        key = intent.getStringExtra("key");
        getUserConfig();
 
        tv_login_phone.setOnClickListener(this);
        tv_send_code.setOnClickListener(this);
 
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
 
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                setSetLoginPhoneState();
            }
        });
 
 
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setSendCodeState();
                setSetLoginPhoneState();
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                setSendCodeState();
                setSetLoginPhoneState();
            }
        });
        setSendCodeState();
        setSetLoginPhoneState();
    }
 
    private void setSetLoginPhoneState() {
        //是手机号码
        if (StringUtils.isMobileNumber(et_phone.getText() + "") && et_code.getText() != null && !StringUtils.isEmpty(et_code.getText() + "")) {
            tv_login_phone.setBackgroundResource(R.drawable.shape_mobile_login_light);
            tv_login_phone.setEnabled(true);
        } else {
            tv_login_phone.setBackgroundResource(R.drawable.shape_mobile_login);
            tv_login_phone.setEnabled(false);
        }
    }
 
    private void setSendCodeState() {
        if (StringUtils.isMobileNumber(et_phone.getText() + "") && lastTime == 0) {
            tv_send_code.setEnabled(true);
            tv_send_code.setTextColor(getResources().getColor(R.color.theme));
        } else {
            tv_send_code.setEnabled(false);
            tv_send_code.setTextColor(getResources().getColor(R.color.gray));
        }
    }
 
 
    int lastTime = 0;
 
    /*
     *添加监听
     */
    private void addListener() {
        tv_user_agreement.setOnClickListener(this);
        iv_close.setOnClickListener(this);
        iv_contact_kefu.setOnClickListener(this);
        tv_privacy_policies.setOnClickListener(this);
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        boolean isLogin =UserUtil.isLogin(getApplicationContext());
        if (isLogin) {//用户已经登录
        } else {
        }
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.iv_contact_kefu:
                KeFuUtil.jumpKeFu(LoginActivity.this, "登录");
                break;
            case R.id.tv_user_agreement:
                UserCustomEvent.userLoginProtocol(LoginActivity.this);
                if (StringUtils.isEmpty(serviceProtocolLink))
                    break;
                Intent intent = new Intent(LoginActivity.this, ShareBrowserActivity.class);
                intent.putExtra("url", serviceProtocolLink);
                startActivity(intent);
                break;
            case R.id.tv_privacy_policies:
                UserCustomEvent.userLoginProtocol(LoginActivity.this);
                if (StringUtils.isEmpty(privacyProtocolLink))
                    break;
                Intent intent1 = new Intent(LoginActivity.this, ShareBrowserActivity.class);
                intent1.putExtra("url", privacyProtocolLink);
                startActivity(intent1);
                break;
 
            case R.id.iv_login_tb:
            case R.id.tv_login:
                CheckBox checkBox = findViewById(R.id.cb_check);
                if (!checkBox.isChecked()) {
                    Toast.makeText(this, "请勾选用户协议与隐私政策", Toast.LENGTH_SHORT).show();
                    return;
                }
 
                //淘宝登录
                final AlibcLogin alibcLogin = AlibcLogin.getInstance();
                if (alibcLogin.isLogin()) {
                    login(alibcLogin.getSession());
                } else
                    alibcLogin.showLogin(new AlibcLoginCallback() {
                        @Override
                        public void onSuccess(int i, String s, String s1) {
                            login(alibcLogin.getSession());
                        }
 
                        @Override
                        public void onFailure(int code, String msg) {
                            Toast.makeText(LoginActivity.this, "登录失败,请稍候重试",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                break;
 
            case R.id.tv_send_code://发送验证码
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                Toast.makeText(this, "验证码已发送,请注意查收", Toast.LENGTH_SHORT);
                lastTime = 60;
                setSendCodeState();
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        tv_send_code.post(new Runnable() {
                            @Override
                            public void run() {
                                if (lastTime > 0) {
                                    lastTime--;
                                    tv_send_code.setText(lastTime + "s重发");
                                    if (lastTime <= 0) {
                                        lastTime = 0;
                                        setSendCodeState();
                                        timer.cancel();
                                        tv_send_code.setText("发送验证码");
                                    }
                                }
                            }
                        });
                    }
                }, 0, 1000);
 
 
                break;
 
            case R.id.tv_login_phone://登录
            {
                CheckBox checkBox1 = findViewById(R.id.cb_check);
                if (!checkBox1.isChecked()) {
                    Toast.makeText(this, "请勾选用户协议与隐私政策", Toast.LENGTH_SHORT).show();
                    return;
                }
                loginPhone();
            }
            break;
        }
    }
 
    private void loginPhone() {
        LoginManager.loginByPhone(et_phone.getText() + "", et_code.getText() + "", null, new LoginManager.LoginAndBindListener() {
            @Override
            public void onLoginStart() {
                super.onLoginStart();
                DialogUtil.show(pd);
            }
 
            @Override
            public void onLoginFinish() {
                super.onLoginFinish();
                DialogUtil.dismiss(pd);
            }
 
            @Override
            public void onLoginSuccess(UserInfo user) {
                super.onLoginSuccess(user);
                UserUtil.loginSuccess(getApplicationContext(), user);
                setResult(100);
                finish();
            }
 
            @Override
            public void onLoginFail(int code, String msg) {
                super.onLoginFail(code, msg);
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
 
    }
 
 
    private void login(Session session) {
        String userData = new Gson().toJson(session);
        ShoppingApi.loginTB(this, userData, new BasicTextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                if (!pd.isShowing())
                    pd.show();
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                super.onSuccessPerfect(statusCode, headers, jsonObject);
                if (jsonObject.optInt("code") == 0) {//登录成功
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    JSONObject data = jsonObject.optJSONObject("data");
                    final UserInfo info = gson.fromJson(data.optJSONObject("userInfo").toString(), new TypeToken<UserInfo>() {
                    }.getType());
                    SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("isLogin", true);
                    editor.putBoolean("isFirstInput", false);
                    editor.putString("uid", info.getId());
                    editor.putString("openid", info.getOpenid());
                    editor.putString("portrait", info.getPortrait());
                    editor.commit();
                    MiPushClient.setAlias(LoginActivity.this, info.getId(), null);
                    Toast.makeText(LoginActivity.this, "恭喜你,登录成功", Toast.LENGTH_SHORT).show();
                    setResult(100);
                    finish();
                } else {//登录失败
                    Toast.makeText(LoginActivity.this, jsonObject.optString("msg"),
                            Toast.LENGTH_LONG).show();
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                if (pd.isShowing())
                    pd.dismiss();
            }
        });
    }
 
    private boolean isIntent = false;
 
    @Override
    protected void onPause() {
        super.onPause();
        if (isIntent) {
            finish();
        }
    }
 
    /**
     * 请求服务器发送验证码
     * 接口请求成功后隐藏发送按钮,同时倒计时开始
     */
    boolean slideVerify = false;
 
    /*
    登录协议
     */
    String serviceProtocolLink = "";// 用户协议链接
    String privacyProtocolLink = "";// 隐私条款链接
 
    private void getUserConfig() {
        ShoppingApi.getUserConfig(LoginActivity.this, UserUtil.getUid(getApplicationContext()), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    serviceProtocolLink = jsonObject.optJSONObject("data").optString("serviceProtocolLink");
                    privacyProtocolLink = jsonObject.optJSONObject("data").optString("privacyProtocolLink");
                }
            }
        });
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == 100) {
            finish();
        }
    }
 
    public void onEventMainThread(WXLoginEvent event) {
        if (event.getType() == TYPE_BIND_WITHOUT_LOGINED) {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.putExtra("type", event.getType());
            intent.putExtra("key", event.getKey());
            startActivity(intent);
            finish();
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}