|
|
@@ -2,21 +2,27 @@ package com.modular.apputils.utils;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.os.Bundle;
|
|
|
+import android.support.annotation.StringRes;
|
|
|
+import android.text.TextUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.common.LogUtil;
|
|
|
import com.common.data.DateFormatUtil;
|
|
|
import com.common.data.JSONUtil;
|
|
|
import com.common.data.ListUtils;
|
|
|
import com.common.data.StringUtil;
|
|
|
import com.common.preferences.PreferenceUtils;
|
|
|
+import com.common.system.SystemUtil;
|
|
|
import com.core.api.wxapi.ApiConfig;
|
|
|
import com.core.api.wxapi.ApiUtils;
|
|
|
import com.core.app.MyApplication;
|
|
|
import com.core.dao.UserDao;
|
|
|
+import com.core.dao.WorkLocationDao;
|
|
|
import com.core.dao.work.WorkModelDao;
|
|
|
import com.core.model.User;
|
|
|
+import com.core.model.WorkLocationModel;
|
|
|
import com.core.model.WorkModel;
|
|
|
import com.core.net.http.http.OAHttpHelper;
|
|
|
import com.core.net.http.http.OnHttpResultListener;
|
|
|
@@ -24,65 +30,150 @@ import com.core.net.http.http.Request;
|
|
|
import com.core.net.location.BdLocationHelper;
|
|
|
import com.core.utils.BaiduMapUtil;
|
|
|
import com.core.utils.CommonInterface;
|
|
|
+import com.core.utils.CommonUtil;
|
|
|
import com.modular.apputils.R;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static com.core.net.http.ViewUtil.ct;
|
|
|
import static com.core.utils.CommonUtil.getSharedPreferences;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 只负责判断|签到
|
|
|
+ * 1.判断距离
|
|
|
+ * 2.判断mac
|
|
|
* Created by Bitlike on 2018/5/3.
|
|
|
*/
|
|
|
|
|
|
public class SignUtils implements OnHttpResultListener {
|
|
|
- private final int LOAD_WORKLOG = 12;
|
|
|
- private final int SIGNING = 11;
|
|
|
+ private final long INTERVAL = 10 * 1000;//间隔时间
|
|
|
+
|
|
|
+
|
|
|
+ private final int LOAD_MAC = 14;//获取网络mac地址
|
|
|
+ private final int MAC_VAL = 13;//判断mac地址
|
|
|
+ private final int LOAD_WORKLOG = 12;//获取打卡列表
|
|
|
+ private final int SIGNING = 11;//打卡
|
|
|
+
|
|
|
+
|
|
|
+ private boolean isMacInNet;
|
|
|
private SignListener mSignListener;
|
|
|
|
|
|
- private long lastTime = 0;
|
|
|
+ private long lastTime = 0;//最后一次打卡时间
|
|
|
|
|
|
public SignUtils(SignListener mSignListener) {
|
|
|
this.mSignListener = mSignListener;
|
|
|
+ getMac();
|
|
|
+ loadMacInNet();
|
|
|
}
|
|
|
|
|
|
public interface SignListener {
|
|
|
void sign(boolean signOk, String message);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void sign(final boolean isB2b, WorkModel model) {
|
|
|
- if (System.currentTimeMillis() - lastTime < 10000) {
|
|
|
- if (this.mSignListener !=null){
|
|
|
- this.mSignListener.sign(false,"");
|
|
|
- }
|
|
|
+ public void signFristMac(final boolean isB2b, WorkModel model) {
|
|
|
+ isMacInNet = true;
|
|
|
+ if (System.currentTimeMillis() - lastTime < INTERVAL) {
|
|
|
+ callBack(false, R.string.not_signin_agin);
|
|
|
return;
|
|
|
}
|
|
|
- lastTime = System.currentTimeMillis();
|
|
|
- MyApplication.getInstance().getBdLocationHelper().requestLocation();
|
|
|
- final float dis = isSubmitAble();
|
|
|
- if (dis > 0) {
|
|
|
- final Bundle bundle = new Bundle();
|
|
|
- bundle.putParcelable("data", model);
|
|
|
- if (isB2b) {
|
|
|
- signinWork(isB2b, bundle, "", dis);
|
|
|
+ if (!MyApplication.getInstance().isNetworkActive()) {
|
|
|
+ callBack(false, StringUtil.getMessage(R.string.networks_out));
|
|
|
+ } else {
|
|
|
+ //判断位置
|
|
|
+ MyApplication.getInstance().getBdLocationHelper().requestLocation();
|
|
|
+ List<WorkLocationModel> beanList = WorkLocationDao.getInstance().queryByEnCode();
|
|
|
+ if (ListUtils.isEmpty(beanList)) {
|
|
|
+ callBack(false, R.string.not_addr_message);
|
|
|
+ //没有打卡地址,TODO 获取班次接口
|
|
|
} else {
|
|
|
- CommonInterface.getInstance().getCodeByNet("CardLog", new CommonInterface.OnResultListener() {
|
|
|
- @Override
|
|
|
- public void result(boolean isOk, int result, String message) {
|
|
|
- signinWork(isB2b, bundle, message, dis);
|
|
|
+ BdLocationHelper helper = MyApplication.getInstance().getBdLocationHelper();
|
|
|
+ float minDis = -1;
|
|
|
+ for (WorkLocationModel bean : beanList) {
|
|
|
+ String chche = BaiduMapUtil.getInstence().getDistance(bean.getLongitude(),
|
|
|
+ bean.getLatitude(), helper.getLatitude(), helper.getLongitude());
|
|
|
+ Float dis = Float.valueOf(chche);
|
|
|
+ if (dis < bean.getValidrange() && (minDis < 0 || dis < minDis)) {
|
|
|
+ minDis = dis;
|
|
|
}
|
|
|
- });
|
|
|
+ }//判断打卡距离结束
|
|
|
+ if (minDis != -1) {
|
|
|
+ lastTime = System.currentTimeMillis();
|
|
|
+ //时间和距离上符合打卡
|
|
|
+ validatorMac(isB2b, minDis, model);
|
|
|
+ } else {
|
|
|
+ callBack(false, R.string.too_long);
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sign(final boolean isB2b, WorkModel model) {
|
|
|
+ LogUtil.i("isMacInNet=" + isMacInNet);
|
|
|
+ if (isMacInNet) {
|
|
|
+ signFristMac(isB2b, model);
|
|
|
} else {
|
|
|
- if (this.mSignListener != null) {
|
|
|
- this.mSignListener.sign(false, StringUtil.getMessage(R.string.too_long));
|
|
|
+ callBack(false, R.string.show_frist_mac);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*判断mac地址是否符合*/
|
|
|
+ private void validatorMac(boolean isB2b, float minDis, WorkModel model) {
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ param.put("macAddress", macAddress);
|
|
|
+ param.put("emcode", getSharedPreferences(MyApplication.getInstance(), "erp_username"));
|
|
|
+ Bundle bundle = new Bundle();
|
|
|
+ bundle.putBoolean(KEY.IS_B2B, isB2b);
|
|
|
+ bundle.putFloat(KEY.DISTANCE, minDis);
|
|
|
+ bundle.putParcelable(KEY.WORK, model);
|
|
|
+ String url = "mobile/addMobileMac.action";
|
|
|
+ Request request = new Request.Bulider()
|
|
|
+ .setBundle(bundle)
|
|
|
+ .setMode(Request.Mode.POST)
|
|
|
+ .setParam(param)
|
|
|
+ .setUrl(url)
|
|
|
+ .setWhat(MAC_VAL)
|
|
|
+ .bulid();
|
|
|
+ OAHttpHelper.getInstance().requestHttp(request, this);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String macAddress = null;
|
|
|
+
|
|
|
+ public String getMac() {
|
|
|
+ if (TextUtils.isEmpty(macAddress)) {
|
|
|
+ macAddress = SystemUtil.getMac(MyApplication.getInstance());
|
|
|
+ if (StringUtil.isEmpty(macAddress)) {
|
|
|
+ //当没有获取到MACAddress的时候,判断wifi状态,如果wifi为未启动
|
|
|
+ //mac地址不合法时候 提示开启wifi
|
|
|
+ WifiReceiverUtil wifiReceiver = new WifiReceiverUtil();
|
|
|
+ wifiReceiver.regReceiver(ct, new WifiReceiverUtil.OnWifiStatusChangeLinstener() {
|
|
|
+ @Override
|
|
|
+ public void callBack(boolean isOpen) {
|
|
|
+ if (isOpen) {
|
|
|
+ macAddress = SystemUtil.getMac(ct);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
+ return macAddress;
|
|
|
+ }
|
|
|
|
|
|
+ private void toSignWork(final Bundle bundle) {
|
|
|
+ final boolean isB2b = bundle.getBoolean(KEY.IS_B2B, false);
|
|
|
+ final float minDis = bundle.getFloat(KEY.DISTANCE, 1);
|
|
|
+ if (isB2b) {
|
|
|
+ signinWork(isB2b, bundle, "", minDis);
|
|
|
+ } else {
|
|
|
+ CommonInterface.getInstance().getCodeByNet("CardLog", new CommonInterface.OnResultListener() {
|
|
|
+ @Override
|
|
|
+ public void result(boolean isOk, int result, String message) {
|
|
|
+ signinWork(isB2b, bundle, message, minDis);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//签到
|
|
|
@@ -98,19 +189,15 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
if (!StringUtil.isEmpty(userId)) {
|
|
|
User user = UserDao.getInstance().getUserByUserId(userId);
|
|
|
phone = user.getTelephone();
|
|
|
- } else
|
|
|
+ } else {
|
|
|
phone = getSharedPreferences(MyApplication.getInstance(), "user_phone");
|
|
|
+ }
|
|
|
}
|
|
|
form.put("cl_phone", phone);
|
|
|
form.put("cl_emcode", getSharedPreferences(ct, "erp_username"));
|
|
|
- String emname = getSharedPreferences(ct, "erp_emname");
|
|
|
- if (StringUtil.isEmpty(emname)) {
|
|
|
- emname = MyApplication.getInstance().mLoginUser.getNickName().trim();
|
|
|
- }
|
|
|
- form.put("cl_emname", emname);
|
|
|
+ form.put("cl_emname", CommonUtil.getName());
|
|
|
form.put("cl_distance", dis);
|
|
|
-
|
|
|
- boolean isp = isPlay(form);
|
|
|
+ boolean isp = isHasLocation(form);
|
|
|
if (!isp) {
|
|
|
return;//不符合打卡
|
|
|
}
|
|
|
@@ -125,9 +212,6 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
param.put("emcode", getSharedPreferences(ct, "erp_username"));
|
|
|
String url = isB2b ? ApiConfig.getInstance(ApiUtils.getApiModel()).getmApiBase().punch_worksignin_url :
|
|
|
"mobile/saveCardLog.action";
|
|
|
- if (bunder != null) {
|
|
|
- bunder.putBoolean("isB2b", isB2b);
|
|
|
- }
|
|
|
Request request = new Request.Bulider()
|
|
|
.setBundle(bunder)
|
|
|
.setMode(Request.Mode.POST)
|
|
|
@@ -140,7 +224,7 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
}
|
|
|
|
|
|
/*判断是否符合打卡*/
|
|
|
- private boolean isPlay(Map<String, Object> form) {
|
|
|
+ private boolean isHasLocation(Map<String, Object> form) {
|
|
|
try {
|
|
|
//判断是否符合打卡
|
|
|
BdLocationHelper helper = MyApplication.getInstance().getBdLocationHelper();
|
|
|
@@ -159,24 +243,12 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private float isSubmitAble() {
|
|
|
- //先判断网络
|
|
|
- if (!MyApplication.getInstance().isNetworkActive()) {
|
|
|
- if (this.mSignListener != null) {
|
|
|
- this.mSignListener.sign(false, StringUtil.getMessage(R.string.networks_out));
|
|
|
- }
|
|
|
- return -1;
|
|
|
- }
|
|
|
- //判断位置
|
|
|
- float dis = BaiduMapUtil.getInstence().getDistance();
|
|
|
- return dis;
|
|
|
- }
|
|
|
|
|
|
//获取打卡记录
|
|
|
private void loadLog(Bundle bunder) {
|
|
|
boolean isB2b = false;
|
|
|
if (bunder != null) {
|
|
|
- isB2b = bunder.getBoolean("isB2b");
|
|
|
+ isB2b = bunder.getBoolean(KEY.IS_B2B);
|
|
|
}
|
|
|
String date = DateFormatUtil.long2Str(DateFormatUtil.YMD);
|
|
|
//获取网络数据
|
|
|
@@ -207,6 +279,23 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
OAHttpHelper.getInstance().requestHttp(request, this);
|
|
|
}
|
|
|
|
|
|
+ //判断mac地址是否已经上传
|
|
|
+ private void loadMacInNet() {
|
|
|
+ //获取网络数据
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ param.put("macaddress", "");
|
|
|
+ param.put("emcode", CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username"));
|
|
|
+ String url = "mobile/queryMobileMac.action";
|
|
|
+ Request request = new Request.Bulider()
|
|
|
+ .setUrl(url)
|
|
|
+ .setWhat(LOAD_MAC)
|
|
|
+ .setMode(Request.Mode.GET)
|
|
|
+ .setParam(param)
|
|
|
+ .bulid();
|
|
|
+ OAHttpHelper.getInstance().requestHttp(request, this);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void result(int what, boolean isJSON, String message, Bundle bundle) {
|
|
|
try {
|
|
|
@@ -219,6 +308,27 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
saveSignin2DB(bundle, JSON.parseObject(message).getJSONArray("listdata"));
|
|
|
}
|
|
|
break;
|
|
|
+
|
|
|
+ case MAC_VAL:
|
|
|
+ if (isJSON && JSONUtil.getBoolean(message, "success")) {
|
|
|
+ toSignWork(bundle);
|
|
|
+ } else {
|
|
|
+ CommonUtil.saveAutoLogtoLocal(StringUtil.getMessage(R.string.auto_sign_failed), message);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case LOAD_MAC://判断是否是第一次请求数据
|
|
|
+ if (isJSON) {
|
|
|
+ JSONObject object = JSON.parseObject(message);
|
|
|
+ if (object.containsKey("success") && object.getBoolean("success")) {
|
|
|
+ //返回成功
|
|
|
+ JSONArray array = object.getJSONArray("macaddress");
|
|
|
+ if ("0".equals(array.getJSONObject(0).getString("MACADDRESS"))) {
|
|
|
+ //获取到新的mac地址
|
|
|
+ isMacInNet = false;
|
|
|
+ } else isMacInNet = true;
|
|
|
+ }
|
|
|
+ } else isMacInNet = false;
|
|
|
+ break;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
if (this.mSignListener != null && e != null) {
|
|
|
@@ -229,16 +339,15 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
|
|
|
@Override
|
|
|
public void error(int what, String message, Bundle bundle) {
|
|
|
- if (this.mSignListener != null && message != null) {
|
|
|
- this.mSignListener.sign(false, message);
|
|
|
- }
|
|
|
+ lastTime = 0;
|
|
|
+ callBack(false, message);
|
|
|
}
|
|
|
|
|
|
//保存到数据库
|
|
|
private void saveSignin2DB(Bundle bundle, JSONArray array) throws Exception {
|
|
|
WorkModel model = null;
|
|
|
- if (bundle != null && bundle.getParcelable("data") != null) {
|
|
|
- model = bundle.getParcelable("data");
|
|
|
+ if (bundle != null && bundle.getParcelable(KEY.WORK) != null) {
|
|
|
+ model = bundle.getParcelable(KEY.WORK);
|
|
|
}
|
|
|
if (model == null) {
|
|
|
List<WorkModel> models = WorkModelDao.getInstance().query(false);
|
|
|
@@ -266,10 +375,26 @@ public class SignUtils implements OnHttpResultListener {
|
|
|
model.setOffSignin(time);
|
|
|
WorkModelDao.getInstance().update(model);
|
|
|
}
|
|
|
+ callBack(true, "打卡成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void callBack(boolean signOk, @StringRes int messageId) {
|
|
|
+ if (MyApplication.getInstance() != null) {
|
|
|
+ callBack(signOk, StringUtil.getMessage(messageId));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- VoiceUtils.signVoice(R.raw.voice_sign);
|
|
|
- if (this.mSignListener != null) {
|
|
|
- this.mSignListener.sign(true, "打卡成功");
|
|
|
+ private void callBack(boolean signOk, String message) {
|
|
|
+ if (mSignListener != null) {
|
|
|
+ mSignListener.sign(signOk, message);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private interface KEY {
|
|
|
+ String IS_B2B = "isB2b";
|
|
|
+ String WORK = "work";
|
|
|
+ String DISTANCE = "distance";
|
|
|
+ }
|
|
|
}
|