|
@@ -1,27 +1,49 @@
|
|
|
package com.ynstkz.shitu.android.activity;
|
|
|
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.app.ProgressDialog;
|
|
|
import android.content.Intent;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
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.RelativeLayout;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import com.common.library.okhttp.callback.Callback;
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.ynstkz.shitu.android.R;
|
|
|
+import com.ynstkz.shitu.android.application.STSign;
|
|
|
import com.ynstkz.shitu.android.base.TitleBarActivity;
|
|
|
+import com.ynstkz.shitu.android.bean.LoginBean;
|
|
|
+import com.ynstkz.shitu.android.data.RequestGroup;
|
|
|
+import com.ynstkz.shitu.android.data.SharedPreferencesUtils;
|
|
|
+import com.ynstkz.shitu.android.event.LoginEvent;
|
|
|
+import com.ynstkz.shitu.android.helper.UmengLoginHelper;
|
|
|
+import com.ynstkz.shitu.android.listener.CheckVerifyCodeCallback;
|
|
|
+import com.ynstkz.shitu.android.listener.VerifyCodeCallback;
|
|
|
import com.ynstkz.shitu.android.utils.StatusBarUtil;
|
|
|
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
import butterknife.Bind;
|
|
|
import butterknife.ButterKnife;
|
|
|
+import okhttp3.Call;
|
|
|
+import okhttp3.Response;
|
|
|
|
|
|
|
|
|
* 作者:fuchangle on 2018/2/7 15:02
|
|
|
*/
|
|
|
|
|
|
-public class LoginPhoneCodeActivity extends TitleBarActivity {
|
|
|
+public class LoginPhoneCodeActivity extends TitleBarActivity implements VerifyCodeCallback {
|
|
|
|
|
|
@Bind(R.id.iv_cancel)
|
|
|
ImageView ivCancel;
|
|
@@ -31,12 +53,24 @@ public class LoginPhoneCodeActivity extends TitleBarActivity {
|
|
|
TextView tvLogin;
|
|
|
@Bind(R.id.edit_username)
|
|
|
EditText editUsername;
|
|
|
- @Bind(R.id.edit_verification_code)
|
|
|
- EditText editVerificationCode;
|
|
|
@Bind(R.id.tv_forget_pw)
|
|
|
TextView tvForgetPw;
|
|
|
@Bind(R.id.btn_login)
|
|
|
Button btnLogin;
|
|
|
+ @Bind(R.id.rl_login_wx)
|
|
|
+ RelativeLayout rlLoginWx;
|
|
|
+ @Bind(R.id.rl_login_qq)
|
|
|
+ RelativeLayout rlLoginQq;
|
|
|
+ @Bind(R.id.rl_login_sina)
|
|
|
+ RelativeLayout rlLoginSina;
|
|
|
+ @Bind(R.id.edit_verification_code)
|
|
|
+ EditText editVerificationCode;
|
|
|
+ @Bind(R.id.tv_verifycode)
|
|
|
+ TextView tvVerifycode;
|
|
|
+
|
|
|
+ private int time_interval = 60;
|
|
|
+ private String phoneNumber;
|
|
|
+ private ProgressDialog progressDialog;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
@@ -55,15 +89,15 @@ public class LoginPhoneCodeActivity extends TitleBarActivity {
|
|
|
return R.layout.activity_login_phonecode;
|
|
|
}
|
|
|
|
|
|
- private void initView(){
|
|
|
+ private void initView() {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void initData(){
|
|
|
+ private void initData() {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void setListener(){
|
|
|
+ private void setListener() {
|
|
|
|
|
|
ivCancel.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
@@ -85,11 +119,242 @@ public class LoginPhoneCodeActivity extends TitleBarActivity {
|
|
|
startActivity(new Intent(LoginPhoneCodeActivity.this, LoginActivity.class));
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+
|
|
|
+ tvVerifycode.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ phoneNumber = editUsername.getText().toString().trim();
|
|
|
+ if(!TextUtils.isEmpty(phoneNumber)){
|
|
|
+ STSign.getInstance().getVerifycode(2, phoneNumber, LoginPhoneCodeActivity.this);
|
|
|
+ } else {
|
|
|
+ showToast(getString(R.string.empty_username));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ btnLogin.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ String code = editVerificationCode.getText().toString().trim();
|
|
|
+ if(TextUtils.isEmpty(code)){
|
|
|
+ showToast("验证码不能为空!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ loginCode(phoneNumber, editVerificationCode.getText().toString().trim());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ * 微信登陆
|
|
|
+ */
|
|
|
+ rlLoginWx.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ threeAuthorization(UmengLoginHelper.WX);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ * qq登陆
|
|
|
+ */
|
|
|
+ rlLoginQq.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ threeAuthorization(UmengLoginHelper.QQ);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ * 新浪登陆
|
|
|
+ */
|
|
|
+ rlLoginSina.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ threeAuthorization(UmengLoginHelper.SINA);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @SuppressLint("HandlerLeak")
|
|
|
+ Handler handler = new Handler(){
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ switch (msg.what){
|
|
|
+ case 1:
|
|
|
+ if(time_interval > 0){
|
|
|
+ tvVerifycode.setClickable(false);
|
|
|
+ tvVerifycode.setText(time_interval + "s后重新获取");
|
|
|
+ tvVerifycode.setTextColor(getResources().getColor(R.color.color_999));
|
|
|
+ time_interval --;
|
|
|
+ handler.sendEmptyMessageDelayed(1, 1000);
|
|
|
+ } else {
|
|
|
+ tvVerifycode.setClickable(true);
|
|
|
+ tvVerifycode.setText("获取手机验证码");
|
|
|
+ tvVerifycode.setTextColor(getResources().getColor(R.color.main_color));
|
|
|
+ time_interval = 60;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ * 登陆
|
|
|
+ * @param mobile
|
|
|
+ * @param code
|
|
|
+ */
|
|
|
+ private void loginCode(String mobile, String code){
|
|
|
+
|
|
|
+ RequestGroup.loginCode(mobile, code, new Callback() {
|
|
|
+ @Override
|
|
|
+ public Object parseNetworkResponse(Response response, int id) throws Exception {
|
|
|
+ return new Gson().fromJson(response.body().string(), LoginBean.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Call call, Exception e, int id) {
|
|
|
+ showToast(getString(R.string.error_msg));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Object response, int id) {
|
|
|
+ LoginBean loginBean = (LoginBean) response;
|
|
|
+ if (loginBean != null) {
|
|
|
+ if ("200".equals(loginBean.getCode())) {
|
|
|
+ if (loginBean.getData() != null) {
|
|
|
+ loginSuccess(loginBean);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ showToast(loginBean.getMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 第三方授权
|
|
|
+ */
|
|
|
+ private void threeAuthorization(final String type) {
|
|
|
+
|
|
|
+ progressDialog = new ProgressDialog(this);
|
|
|
+ progressDialog.setMessage("正在请求授权...");
|
|
|
+ progressDialog.show();
|
|
|
+
|
|
|
+ UmengLoginHelper mLogin = new UmengLoginHelper(LoginPhoneCodeActivity.this, new UmengLoginHelper.Callback() {
|
|
|
+ @Override
|
|
|
+ public void onComplete(Map<String, String> data) {
|
|
|
+ if (type.equals(UmengLoginHelper.QQ)) {
|
|
|
+ thirdLogin(data.get("uid"), LoginActivity.KEY_QQ);
|
|
|
+ } else if (type.equals(UmengLoginHelper.WX)) {
|
|
|
+ thirdLogin(data.get("uid"), LoginActivity.KEY_WEIXIN);
|
|
|
+ } else if (type.equals(UmengLoginHelper.SINA)) {
|
|
|
+ thirdLogin(data.get("uid"), LoginActivity.KEY_SINA);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError() {
|
|
|
+ if (progressDialog != null) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCancel() {
|
|
|
+ if (progressDialog != null) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mLogin.oAuth(type);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 三方登陆
|
|
|
+ *
|
|
|
+ * @param thirdId
|
|
|
+ * @param thirdType
|
|
|
+ */
|
|
|
+ private void thirdLogin(final String thirdId, final String thirdType) {
|
|
|
+
|
|
|
+ if (progressDialog != null) {
|
|
|
+ progressDialog.setMessage("正在登录...");
|
|
|
+ }
|
|
|
+
|
|
|
+ RequestGroup.thirdLogin(thirdId, thirdType, new Callback() {
|
|
|
+ @Override
|
|
|
+ public Object parseNetworkResponse(Response response, int id) throws Exception {
|
|
|
+ return new Gson().fromJson(response.body().string(), LoginBean.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Call call, Exception e, int id) {
|
|
|
+ if (progressDialog != null) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ }
|
|
|
+ showToast(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Object response, int id) {
|
|
|
+ if (progressDialog != null) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ }
|
|
|
+ LoginBean loginBean = (LoginBean) response;
|
|
|
+ if (loginBean != null) {
|
|
|
+ if ("200".equals(loginBean.getCode())) {
|
|
|
+ if (loginBean.getData() != null) {
|
|
|
+ boolean isBind = loginBean.getData().isIsBind();
|
|
|
+ if (isBind) {
|
|
|
+ loginSuccess(loginBean);
|
|
|
+ } else {
|
|
|
+ Intent intent = new Intent(LoginPhoneCodeActivity.this, RegisterActivity.class);
|
|
|
+ intent.putExtra("thirdId", thirdId);
|
|
|
+ intent.putExtra("thirdType", thirdType);
|
|
|
+ startActivity(intent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ showToast(loginBean.getMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 登陆成功
|
|
|
+ *
|
|
|
+ * @param loginBean
|
|
|
+ */
|
|
|
+ private void loginSuccess(LoginBean loginBean) {
|
|
|
+
|
|
|
+ SharedPreferencesUtils.setLogin(true);
|
|
|
+ SharedPreferencesUtils.saveToken(loginBean.getData().getToken());
|
|
|
+ SharedPreferencesUtils.saveUserId(loginBean.getData().getUserId());
|
|
|
+
|
|
|
+ showToast(loginBean.getMsg());
|
|
|
+ EventBus.getDefault().post(new LoginEvent());
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void codeCallback(int type) {
|
|
|
+ if(type == 2){
|
|
|
+ handler.sendEmptyMessage(1);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
ButterKnife.unbind(this);
|
|
|
+ if(handler != null){
|
|
|
+ handler.removeCallbacksAndMessages(null);
|
|
|
+ }
|
|
|
}
|
|
|
}
|