陈奇
2019-05-29 c5bf501d8b507d1e3e79541e7ac9b45910860b4d
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
package com.moral.yunfushao.activity;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.lzy.okgo.cache.CacheMode;
import com.moral.andbrickslib.utils.FastJsonTools;
import com.moral.andbrickslib.utils.NetworkUtil;
import com.moral.andbrickslib.utils.StatusBarUtil;
import com.moral.yunfushao.MainActivity;
import com.moral.yunfushao.MainApp;
import com.moral.yunfushao.R;
import com.moral.yunfushao.base.BaseActivity;
import com.moral.yunfushao.common.API;
import com.moral.yunfushao.httputils.HttpCallBack;
import com.moral.yunfushao.httputils.HttpUtils;
import com.moral.yunfushao.model.User;
import com.moral.yunfushao.utils.SharedPreferencesUtil;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by haijiang on 2017/5/9.
 */
 
public class LoginActivity extends BaseActivity {
    private Button bt_sure;
    private ImageView weixinlogin;
    private TextView tv_forget, tv_register;
    private EditText et_phone, et_pwd;
 
    @Override
    protected void getBundleExtras(Bundle extras) {
 
    }
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.translucentStatusBar(LoginActivity.this);
 
        int type = SharedPreferencesUtil.getUtil(this).getLoginInfo().getLogin_type();
 
        if (!NetworkUtil.isAvailable(this)) {
            return;
        }
        switch (type) {
            case 1:
                //手机登陆
                goHome();
                break;
            case 2:
                //微信登陆
                sendReq();
                break;
        }
    }
 
    private void goHome() {
        if (!TextUtils.isEmpty(MainApp.userId)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
        finish();
    }
 
 
 
    @Override
    protected int getLayoutId() {
        return R.layout.ac_login_layout;
    }
 
    @Override
    protected void initViews() {
        bt_sure = findView(R.id.bt_sure);
        tv_forget = findView(R.id.tv_forget);
        tv_register = findView(R.id.tv_register);
        et_phone = findView(R.id.et_phone);
        et_pwd = findView(R.id.et_pwd);
        weixinlogin = findView(R.id.weixinlogin);
    }
 
    @Override
    protected void initListener() {
        bt_sure.setOnClickListener(this);
        tv_forget.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        weixinlogin.setOnClickListener(this);
    }
 
    @Override
    protected void initData() {
 
    }
 
    @Override
    protected void processClick(View view) {
        switch (view.getId()) {
            case R.id.bt_sure:
                String userphone = et_phone.getText().toString().trim();
                String password = et_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(userphone)) {
                    mToatUtils.showSingletonToast("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    mToatUtils.showSingletonToast("请输入密码");
                    return;
                }
                login(userphone, password);
                break;
            case R.id.tv_forget:
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_register:
                Intent intentRgister = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intentRgister);
                break;
            case R.id.weixinlogin:
                weixinlogin();
                break;
        }
    }
 
    private void weixinlogin() {
        sendReq();
    }
 
    @Override
    protected void onErrorPageClick() {
 
    }
 
    private void login(String tel, final String pwd) {
        final String url = API.LOGIN;
        Map<String, String> params = new HashMap<>();
        params.put("phone", tel);
        params.put("password", pwd);
        HttpUtils.doPost(url, params, CacheMode.DEFAULT, true, new HttpCallBack() {
            @Override
            public void onSuccess(String res, String msg) {
                mToatUtils.showSingletonToast(msg);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                User user = FastJsonTools.getJson(res, User.class);
                if (user != null) {
                    MainApp.theApp.sharedPreferencesUtil.saveLoginInfo(user);
                    user.setLogin_type(1);
                    MainApp.theApp.userId = user.get_id();
                    jumpMain();
                }
            }
 
            @Override
            public void showLoadingDialog() {
                progressDialog.setTitleText("正在登陆...");
                progressDialog.show();
            }
 
            @Override
            public void onFail(int errno, String s) {
                mToatUtils.showSingletonToast(s);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
        });
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }
 
    private void jumpMain() {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("islogin", true);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }
}