| xqd
@@ -1,22 +1,32 @@
|
|
|
package com.zhilin.patrol.ui.activity;
|
|
|
|
|
|
import android.Manifest;
|
|
|
+import android.annotation.SuppressLint;
|
|
|
import android.annotation.TargetApi;
|
|
|
import android.content.Intent;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.widget.Button;
|
|
|
import android.widget.EditText;
|
|
|
+import android.widget.ImageView;
|
|
|
|
|
|
+import com.bumptech.glide.Glide;
|
|
|
+import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
+import com.bumptech.glide.load.model.GlideUrl;
|
|
|
import com.zhilin.patrol.R;
|
|
|
import com.zhilin.patrol.bean.HomeEntity;
|
|
|
import com.zhilin.patrol.bean.LoginEntity;
|
|
|
import com.zhilin.patrol.bean.LoginEntityNull;
|
|
|
import com.zhilin.patrol.http.HttpUrl;
|
|
|
import com.zhilin.patrol.tool.CheckPermissionUtils;
|
|
|
+import com.zhilin.patrol.tool.CodeUtils;
|
|
|
import com.zhilin.patrol.tool.DialogUtil;
|
|
|
import com.zhilin.patrol.tool.EncryptUtil;
|
|
|
import com.zhilin.patrol.tool.IpUtils;
|
| xqd
@@ -25,18 +35,28 @@ import com.zhilin.patrol.tool.SPUtil;
|
|
|
import com.zhilin.patrol.tool.Utils;
|
|
|
import com.zhilin.patrol.views.CustomProgressDialog;
|
|
|
import com.zhy.http.okhttp.OkHttpUtils;
|
|
|
-import com.zhy.http.okhttp.builder.PostFormBuilder;
|
|
|
+import com.zhy.http.okhttp.callback.BitmapCallback;
|
|
|
import com.zhy.http.okhttp.callback.Callback;
|
|
|
-import com.zhy.http.okhttp.request.RequestCall;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import butterknife.OnClick;
|
|
|
import okhttp3.Call;
|
|
|
+import okhttp3.Cookie;
|
|
|
+import okhttp3.CookieJar;
|
|
|
+import okhttp3.Headers;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
import okhttp3.Response;
|
|
|
|
|
|
+import static com.zhilin.patrol.http.HttpUrl.IMAGEPNG_GET;
|
|
|
+
|
|
|
/**
|
|
|
* 登录界面
|
|
|
*/
|
| xqd
@@ -47,13 +67,21 @@ public class LoginActivity extends BaseActivity {
|
|
|
EditText mAccountEt;//账号
|
|
|
@BindView(R.id.et_pwd)
|
|
|
EditText mPwdEt;//密码
|
|
|
-
|
|
|
+ @BindView(R.id.image)
|
|
|
+ ImageView imageVe;
|
|
|
@BindView(R.id.btn_login)
|
|
|
Button mLoginBtn;
|
|
|
+ @BindView(R.id.et_code)
|
|
|
+ EditText mCodeEt;//账号
|
|
|
+
|
|
|
private CustomProgressDialog mProgressDialog;
|
|
|
private LoginEntity.InfoListBean.UserInfoBean mUserInfo;
|
|
|
private HomeEntity.InfoListBean homeInfo;
|
|
|
-
|
|
|
+ private CodeUtils codeUtils;
|
|
|
+ private String PHPSESSID;
|
|
|
+ public static final int GET_DATA_SUCCESS = 1;
|
|
|
+ public static final int NETWORK_ERROR = 2;
|
|
|
+ public static final int SERVER_ERROR = 3;
|
|
|
String[] mPermissions = {
|
|
|
Manifest.permission.ACCESS_COARSE_LOCATION,
|
|
|
Manifest.permission.ACCESS_FINE_LOCATION,
|
| xqd
@@ -64,9 +92,9 @@ public class LoginActivity extends BaseActivity {
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
-// request();
|
|
|
isContainNavi(false);
|
|
|
setContentView(R.layout.activity_login);
|
|
|
+
|
|
|
LoginEntity.InfoListBean infoList = (LoginEntity.InfoListBean) SPUtil.getShareObject(LoginActivity.this, SPUtil.SP_COMMON_CONFIG, "infoList");
|
|
|
if (infoList!=null){//登录过直接去主页
|
|
|
toMain(infoList);
|
| xqd
@@ -75,6 +103,7 @@ public class LoginActivity extends BaseActivity {
|
|
|
ButterKnife.bind(this);
|
|
|
mProgressDialog = new CustomProgressDialog(this);
|
|
|
checkPermissions();
|
|
|
+ requestImg();
|
|
|
}
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
| xqd
@@ -89,27 +118,194 @@ public class LoginActivity extends BaseActivity {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @OnClick(R.id.btn_login)
|
|
|
+ @OnClick({R.id.btn_login,R.id.image})
|
|
|
public void onClick(View view) {
|
|
|
if (view.getId() == R.id.btn_login) {//登录
|
|
|
String accountText = mAccountEt.getText().toString().trim();
|
|
|
String pwdText = mPwdEt.getText().toString().trim();
|
|
|
+ String codeText = mCodeEt.getText().toString().trim();
|
|
|
if (isEmpt(accountText, "请输入账号")) return;
|
|
|
if (isEmpt(pwdText, "请输入密码")) return;
|
|
|
+ if (isEmpt(codeText, "请输入验证码")) return;
|
|
|
String ipAddress = IpUtils.getIpAddress(this);
|
|
|
Log.i(TAG,"ipAddress地址");
|
|
|
Log.i(TAG,ipAddress);
|
|
|
+
|
|
|
try {
|
|
|
String pwdByMd5 = EncryptUtil.encodeByMd5(pwdText);
|
|
|
Log.i("PASSWORD",pwdByMd5);
|
|
|
- postLogin(accountText, ipAddress, pwdByMd5);
|
|
|
- request();
|
|
|
+ postLogin(accountText, ipAddress, pwdByMd5,codeText);
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
Utils.showToast(this, "md5加密失败");
|
|
|
}
|
|
|
}
|
|
|
+ if(view.getId()==R.id.image){
|
|
|
+ requestImg();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 特殊的自动管理Cookies
|
|
|
+ */
|
|
|
+ /* private class CookiesManager implements CookieJar {
|
|
|
+
|
|
|
+ //保存每个url的cookie
|
|
|
+ private HashMap<okhttp3.HttpUrl, List<Cookie>> cookieStore = new HashMap<okhttp3.HttpUrl, List<Cookie>>();
|
|
|
+
|
|
|
+ //上一个请求url
|
|
|
+ private okhttp3.HttpUrl url;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveFromResponse(okhttp3.HttpUrl httpUrl, List<Cookie> list) {
|
|
|
+ //保存链接的cookie
|
|
|
+ cookieStore.put(httpUrl, list);
|
|
|
+ //保存上一次的url,供给下一次cookie的提取。
|
|
|
+ url = httpUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Cookie> loadForRequest(okhttp3.HttpUrl httpUrl) {
|
|
|
+
|
|
|
+ //加载上一个链接的cookie
|
|
|
+ List<Cookie> cookies = cookieStore.get(url);
|
|
|
+ return cookies != null ? cookies : new ArrayList<Cookie>();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 获取图形验证码
|
|
|
+ * */
|
|
|
+ public void requestImg() {
|
|
|
+// URL url = new URL("http://39.106.14.189:8081/index.php/ApiMobi/getImgPng");
|
|
|
+// returnBitMap("http://39.106.14.189:8081/index.php/ApiMobi/getImgPng");
|
|
|
+// image.setImageURI(url);
|
|
|
+ setImageURL(IMAGEPNG_GET);
|
|
|
+ //初始化okhttp,
|
|
|
}
|
|
|
|
|
|
+ @SuppressLint("HandlerLeak")
|
|
|
+ private Handler handler = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ switch (msg.what) {
|
|
|
+ case GET_DATA_SUCCESS:
|
|
|
+ Bitmap bitmap = (Bitmap) msg.obj;
|
|
|
+ imageVe.setImageBitmap(bitmap);
|
|
|
+ break;
|
|
|
+ case NETWORK_ERROR:
|
|
|
+ Utils.showToast(LoginActivity.this, "网络连接失败");
|
|
|
+ break;
|
|
|
+ case SERVER_ERROR:
|
|
|
+ Utils.showToast(LoginActivity.this, "服务器发生错误");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ //设置网络图片
|
|
|
+ public void setImageURL(final String path) {
|
|
|
+ //开启一个线程用于联网
|
|
|
+ new Thread() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ //把传过来的路径转成URL
|
|
|
+// URL url = new URL(path);
|
|
|
+ //获取连接
|
|
|
+
|
|
|
+// OkHttpUtils.post().url(path).build().execute(new BitmapCallback() {
|
|
|
+// @Override
|
|
|
+// public void onResponse(Bitmap bitmap, int i) {
|
|
|
+// try {
|
|
|
+// imageVe.setImageBitmap(bitmap);
|
|
|
+//
|
|
|
+// } catch (Exception e) {
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onError(Call arg0, Exception arg1, int arg2) {
|
|
|
+// // TODO Auto-generated method stub
|
|
|
+// }
|
|
|
+// });
|
|
|
+ /*Response response = null;
|
|
|
+ try {
|
|
|
+ response = OkHttpUtils.post()
|
|
|
+ .url(path)
|
|
|
+ .build().execute();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Headers responseHeaders = response.headers();
|
|
|
+ int responseHeadersLength = responseHeaders.size();
|
|
|
+ for (int i = 0; i < responseHeadersLength; i++){
|
|
|
+ String headerName = responseHeaders.name(i);
|
|
|
+ String headerValue = responseHeaders.get(headerName);
|
|
|
+ if(headerName.equals("Set-Cookie")){
|
|
|
+ PHPSESSID=headerValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.print("TAG----------->Name:"+headerName+"------------>Value:"+headerValue+"\n");
|
|
|
+ }*/
|
|
|
+ Response response = OkHttpUtils.post()
|
|
|
+ .url(path)
|
|
|
+ .build().execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ byte[] jsonStr = response.body().bytes();
|
|
|
+// Log.i("验证码图片=",jsonStr);
|
|
|
+// InputStream inputStream = response.body().byteStream();
|
|
|
+ Bitmap img = BitmapFactory.decodeByteArray(jsonStr,0,jsonStr.length);
|
|
|
+// Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
|
|
+ Message msg = Message.obtain();
|
|
|
+ msg.obj = img;
|
|
|
+ msg.what = GET_DATA_SUCCESS;
|
|
|
+ handler.sendMessage(msg);
|
|
|
+// inputStream.close();
|
|
|
+ Headers responseHeaders = response.headers();
|
|
|
+ int responseHeadersLength = responseHeaders.size();
|
|
|
+ for (int i = 0; i < responseHeadersLength; i++){
|
|
|
+ String headerName = responseHeaders.name(i);
|
|
|
+ String headerValue = responseHeaders.get(headerName);
|
|
|
+ if(headerName.equals("Set-Cookie")){
|
|
|
+ PHPSESSID=headerValue;
|
|
|
+ }
|
|
|
+ System.out.print("TAG----------->Name:"+headerName+"------------>Value:"+headerValue+"\n");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ Utils.showToast(LoginActivity.this, "获取验证码失败");
|
|
|
+ }
|
|
|
+/*
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ //使用GET方法访问网络
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ //超时时间为10秒
|
|
|
+ connection.setConnectTimeout(10000);
|
|
|
+ //获取返回码
|
|
|
+ int code = connection.getResponseCode();
|
|
|
+ if (code == 200) {
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+ //使用工厂把网络的输入流生产Bitmap
|
|
|
+
|
|
|
+ Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
|
|
+ //利用Message把图片发给Handler
|
|
|
+ Message msg = Message.obtain();
|
|
|
+ msg.obj = bitmap;
|
|
|
+ msg.what = GET_DATA_SUCCESS;
|
|
|
+ handler.sendMessage(msg);
|
|
|
+ inputStream.close();
|
|
|
+ } else {
|
|
|
+ //服务启发生错误
|
|
|
+ handler.sendEmptyMessage(SERVER_ERROR);
|
|
|
+ }*/
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ //网络连接错误
|
|
|
+ handler.sendEmptyMessage(NETWORK_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }.start();
|
|
|
+ }
|
|
|
/**
|
|
|
* 提交登录
|
|
|
*
|
| xqd
@@ -118,13 +314,15 @@ public class LoginActivity extends BaseActivity {
|
|
|
* @param pwdByMd5
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private void postLogin(String accountText, String ipAddress, String pwdByMd5) throws IOException {
|
|
|
+ private void postLogin(String accountText, String ipAddress, String pwdByMd5,String codeText) throws IOException {
|
|
|
mProgressDialog.showDialog("正在登录...");
|
|
|
OkHttpUtils.post()
|
|
|
.url(HttpUrl.LOGIN_URL)
|
|
|
+ .addHeader("Cookie", PHPSESSID)
|
|
|
.addParams("loginName", accountText)
|
|
|
.addParams("loginPwd", pwdByMd5)
|
|
|
.addParams("userIP", ipAddress)
|
|
|
+ .addParams("tokenCode",codeText)
|
|
|
.build().execute(new Callback<LoginEntity>() {
|
|
|
@Override
|
|
|
public LoginEntity parseNetworkResponse(Response response, int id) throws Exception {
|
| xqd
@@ -192,7 +390,9 @@ public class LoginActivity extends BaseActivity {
|
|
|
// }
|
|
|
SPUtil.savaToShared(LoginActivity.this, SPUtil.SP_COMMON_CONFIG, "infoList", infoList);
|
|
|
// toMain(infoBean);
|
|
|
+
|
|
|
toMain(infoList);
|
|
|
+
|
|
|
}
|
|
|
});
|
|
|
|
| xqd
@@ -213,7 +413,6 @@ public class LoginActivity extends BaseActivity {
|
|
|
}
|
|
|
|
|
|
if(mUserInfo.getUserRole()==3) {
|
|
|
- request();
|
|
|
Intent intent = new Intent(LoginActivity.this, RectifyMainActivity.class);
|
|
|
// intent.putExtra("userInfo", mUserInfo);
|
|
|
intent.putExtra("infoList", infoList);
|