package com.basic.security.fragment; import android.text.TextUtils; import com.basic.security.R; import com.basic.security.base.BaseFragment; import com.basic.security.manager.BaseSettingManager; import com.basic.security.manager.HintSignUpManager; import com.basic.security.manager.PersonIdentityManager; import com.basic.security.manager.PersonManager; import com.basic.security.manager.impl.cblite.AccountManager; import com.basic.security.model.ModelAdapter; import com.basic.security.model.Person; import com.basic.security.utils.BasicIdUtil; import com.basic.security.utils.ButtonClickIntervalsUtils; import com.basic.security.utils.CheckIdCard; import com.basic.security.utils.FaceId; import com.basic.security.utils.PhoneNumberCheckUtils; import com.basic.security.utils.ToastUtil; import com.basic.security.widget.ConfirmDialog; import org.androidannotations.annotations.EFragment; import org.androidannotations.annotations.UiThread; import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.Date; import java.util.HashMap; @EFragment(R.layout.fragment_person_manage) public class PersonManageFragment extends BaseFragment { public ModelAdapter selectedPersonDocument; public boolean needVerifyIDCard = false; public ConfirmDialog confirmDialog; public String savedPersonId; boolean messageComplete = false; private boolean isFromAdminRegister = false; private boolean canSavePerson = true; private String all_device = null; public boolean needIdCardInfo() { return needVerifyIDCard; } public void setNeedVerifyIDCard(boolean needIDCard) { this.needVerifyIDCard = needIDCard; } @Override public void show() { if (mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_wait_idcard) { mainActivity().fragment_person_identity.setIdentityWithPerson(null); } // 解决从注册页面跳到人员管理页面,第一次不显示请在刷卡区验证身份证 if (selectedPersonDocument != null && isFromAdminRegister) { mainActivity().fragment_face_detail.setPerson(selectedPersonDocument); } } @Override public void hide() { if (mainActivity().currentFragment != mainActivity().fragment_person_manage) { // 当人工注册身份证验证时,会把selectedPersonDocument置为null,暂时没找到置为null的原因,为了不影响已有的逻辑 // 当为工注册身份证验证时,不把selectedPersonDocument置为null, if (mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_compare_success || mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_wait_idcard || mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_wait_face) { } else { selectedPersonDocument = null; } if (confirmDialog != null) { confirmDialog.dismiss(); } } } public boolean savePerson() { if (!ButtonClickIntervalsUtils.isCanClick()) { return false; } final String[] msg = {"保存成功"}; canSavePerson = true; BaseFragment currentFragment = mainActivity().currentFragment; class SavePersonHelper { public boolean execute() { if (checkPersonDocument()) { if (!canSavePerson) {// 校验身份证号和手机号是否符合要求 return false; } if (needSaveIdCard()) { saveIdCard(); } // 验证方式为空或者idCard不为null时,在设置验证方式(修改当为人工注册未身份验证时,修改注册方式为普通注册时, // 点击保存;仍然走这个方法,使注册改变bug) if (TextUtils.isEmpty(selectedPersonDocument.getString("verify_status")) || idCard != null) { setVerifyStatus(); } setIsRegistered(); if (mainActivity().fragment_person_identity.savePersonIdentity(selectedPersonDocument.getId())) { byte[] camera_image_feature = selectedPersonDocument.getBlob("camera_image_feature"); if (camera_image_feature != null && camera_image_feature != null) { // 解决在personManager的已注册中人员信息不完整的提示,人员信息不完整,不保存 // boolean messageComplete = ((FaceDetailFragment) mainActivity().fragment_face_detail).detailMessageIsComplete(); int identitySize = mainActivity().fragment_person_identity.getIdentitySize(); identitySize = 1; // 不判断是否有身份信息 if ("已注册".equals(selectedPersonDocument.getString("sign_up_status")) && currentFragment == mainActivity().fragment_person_manage) { if (!messageComplete) { return false; } if (identitySize == 0) { ToastUtil.show("请选择身份"); return false; } ToastUtil.show(msg[0]); } //保存选择设备 setDevice(); PersonManager.savePerson(selectedPersonDocument); savedPersonId = selectedPersonDocument.getId(); afterSavePerson(selectedPersonDocument); if (isNewPerson()) { try { String camera_image_path = selectedPersonDocument.getString(Person.camera_image_path); byte[] feature = FaceId.instance.extractFeature(camera_image_path); if (feature == null) { PersonManager.deletePerson(selectedPersonDocument); return false; } } catch (Exception e) { PersonManager.deletePerson(selectedPersonDocument); return false; } FaceId.instance.addFeatureToDb(selectedPersonDocument.getId(), selectedPersonDocument.getBlob("camera_image_feature")); ToastUtil.show("注册成功"); } } else { ToastUtil.show("获取头像失败,请稍后重试!"); } mainActivity().runOnUiThread(new Runnable() { @Override public void run() { jumpToPage(); } }); } } return true; } public void setDevice() { //获取选择的设备:当前设备还是集群所有设备 String s = mainActivity().fragment_person_identity.aboutDevice(); selectedPersonDocument.setString("all_device", s); } public void setIsRegistered() { if (!AccountManager.adminLoggedIn()) { selectedPersonDocument.setString("sign_up_status", "已注册"); } if (AccountManager.adminLoggedIn()) { String sign_up_status = selectedPersonDocument.getString("sign_up_status"); messageComplete = mainActivity().fragment_face_detail.detailMessageIsComplete(); int identitySize = mainActivity().fragment_person_identity.getIdentitySize(); identitySize = 1; // 不判断是否有身份信息都可以保存 if (mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_compare_success || mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_wait_idcard || mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_wait_face || mainActivity().currentFragment == mainActivity().fragment_su_logged_nic_wait_detail || mainActivity().currentFragment == mainActivity().fragment_su_logged_nic_wait_face) { // if (messageComplete || ("未注册".equals(selectedPersonDocument.getString("sign_up_status")) && messageComplete && identitySize > 0)) { // selectedPersonDocument.setString("sign_up_status", "已注册"); // ToastUtil.show("注册成功"); // } else if (TextUtils.isEmpty(sign_up_status)) { // selectedPersonDocument.setString("sign_up_status", "未注册"); // } if (messageComplete) { selectedPersonDocument.setString("sign_up_status", "已注册"); } else { selectedPersonDocument.setString("sign_up_status", "未注册"); } } else { // 在未注册中修改信息变为已注册(在personManager页面) if (!"已注册".equals(sign_up_status)) { if (messageComplete) { selectedPersonDocument.setString("sign_up_status", "已注册"); msg[0] = HintSignUpManager.getRegisterSuccessHint(); if (identitySize > 0) { selectedPersonDocument.setString("sign_up_status", "已注册"); msg[0] = HintSignUpManager.getRegisterSuccessHint(); } else { ToastUtil.show("请选择身份!"); selectedPersonDocument.setString("sign_up_status", "未注册"); } } else { selectedPersonDocument.setString("sign_up_status", "未注册"); ToastUtil.show("信息填写不完整!"); } } } } } public void setVerifyStatus() { if ("自助注册".equals(selectedPersonDocument.getString("sign_up_method"))) { if (BaseSettingManager.isAutoSignUpIDCardMode()) { selectedPersonDocument.setString("verify_status", "未验证身份证"); } else { selectedPersonDocument.setString("verify_status", "不需要验证身份证"); } } else { if (BaseSettingManager.isAdminAuthSignUpModeNeedIdCard()) { selectedPersonDocument.setString("verify_status", "未验证身份证"); } else { selectedPersonDocument.setString("verify_status", "不需要验证身份证"); } } if (selectedPersonDocument.getBlob("id_card_image_path") != null) { selectedPersonDocument.setString("verify_status", "已经验证身份证"); } } public boolean checkPersonDocument() { /** * 测试保存设备 */ boolean isNewPerson = false; if (isNewPerson()) { isNewPerson = true; createNewPerson(); if (!AccountManager.adminLoggedIn()) { selectedPersonDocument.setString("sign_up_method", "自助注册"); } else { selectedPersonDocument.setString("sign_up_method", "管理员注册"); } if (!savePhotoAndFeature()) { return false; } if (PersonManager.isDuplicate(selectedPersonDocument)) { System.out.println("PersonManagerFragment:114,不应该有重复"); return false; } } else { if (selectedPersonDocument == null) { return false; } } return saveFaceDetail(isNewPerson); } public void createNewPerson() { selectedPersonDocument = new ModelAdapter(BasicIdUtil.getPersonId()); selectedPersonDocument.setString("del_flag", "0"); selectedPersonDocument.setString("table", "person"); selectedPersonDocument.setString("sign_up_time", new Date().getTime() + ""); } public void jumpToPage() { isFromAdminRegister = false; if (AccountManager.adminLoggedIn()) { if (currentFragment == mainActivity().fragment_su_logged_nic_wait_detail) { mainActivity().showFragment(mainActivity().fragment_su_logged_nic_wait_face); mainActivity().fragment_face_detail.clearInPutText(); return; } if (currentFragment == mainActivity().fragment_su_logged_ic_wait_face) { isFromAdminRegister = true; } if (currentFragment == mainActivity().fragment_su_logged_ic_compare_success) { isFromAdminRegister = true; mainActivity().showFragment(mainActivity().fragment_su_logged_ic_wait_face); mainActivity().fragment_face_detail.clearInPutText(); return; } if (currentFragment == mainActivity().fragment_su_logged_ic_wait_idcard) { isFromAdminRegister = true; if (idCard == null) { ToastUtil.show("保存成功"); mainActivity().showFragment(mainActivity().fragment_su_logged_ic_wait_face); return; } else { mainActivity().showFragment(mainActivity().fragment_su_logged_ic_compare_success); return; } } if (mainActivity().currentFragment != mainActivity().fragment_person_manage) { if (mainActivity().currentFragment != mainActivity().fragment_su_logged_nic_wait_face || mainActivity().currentFragment != mainActivity().fragment_su_logged_nic_wait_detail) { mainActivity().fragment_face_detail.setPerson(null); mainActivity().fragment_person_identity.setIdentityWithPerson(null); mainActivity().fragment_person_identity.setDevice("0"); } else { personChanged(null); } } else { if ("未注册".equals(mainActivity().fragment_face_list.isRegister)) { mainActivity().fragment_face_list.findNextPosition(selectedPersonDocument); } else { personChanged(PersonManager.findPersonById(selectedPersonDocument.getId()), false); } } } else { if (currentFragment == mainActivity().fragment_su_auto_ic_compare_success || currentFragment == mainActivity().fragment_su_auto_nic_wait_detail) { ToastUtil.show("注册成功"); mainActivity().showFragment(mainActivity().fragment_home); // new Thread(new Runnable() { // @Override // public void run() { // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // mainActivity().runOnUiThread(new Runnable() { // @Override // public void run() { // mainActivity().showFragment(mainActivity().fragment_home); // } // }); // } // }).start(); } } idCard = null;// 清空IdCard,如果不清空,在下一个人刷人脸没有刷身份证点保存会显示 } public void saveIdCard() { if (idCard != null) { selectedPersonDocument.setString("name", idCard.name); selectedPersonDocument.setString("id_card_number", idCard.cardNumber); saveBlob(selectedPersonDocument, "id_card_image_feature", idCard.featureData); saveBlob(selectedPersonDocument, "id_card_image_path", idCard.jpgData); } } public boolean needSaveIdCard() { if (!AccountManager.adminLoggedIn()) { if (currentFragment == mainActivity().fragment_su_auto_ic_compare_success || currentFragment == mainActivity().fragment_su_auto_ic_wait_phone || currentFragment == mainActivity().fragment_su_auto_ic_wait_idcard) { return true; } } if (AccountManager.adminLoggedIn()) { if (currentFragment == mainActivity().fragment_su_logged_ic_wait_idcard || currentFragment == mainActivity().fragment_su_logged_ic_compare_success) { return true; } } if (needIdCardInfo() && currentFragment == mainActivity().fragment_person_manage) { return true; } return false; } public boolean saveFaceDetail(boolean isNewPerson) { boolean success = true; boolean allowPhone = true; boolean allowIDNumber = true; if (!AccountManager.adminLoggedIn()) { selectedPersonDocument.setString("sign_up_rule", BaseSettingManager.getDefaultRegisterRule()); if ("有效时间内通行".equals(BaseSettingManager.getDefaultRegisterRule())) { if (TextUtils.isEmpty(BaseSettingManager.getDefaultRegisterTimeId())) { ToastUtil.show("请选择有效时间内通行具体时间段"); canSavePerson = false; } selectedPersonDocument.setString("checked_time_rule_id", BaseSettingManager.getDefaultRegisterTimeId()); } if (currentFragment == mainActivity().fragment_su_auto_ic_compare_success) { String phone = mainActivity().fragment_su_auto_ic_compare_success.phone_number.getText().toString().trim(); if (!TextUtils.isEmpty(phone) && !PhoneNumberCheckUtils.isPhone(phone)) { allowPhone = false; } else { selectedPersonDocument.setString("phone", phone); } } else if (currentFragment == mainActivity().fragment_su_auto_wait_face) { } else if (currentFragment == mainActivity().fragment_su_auto_nic_wait_detail) { SuAutoNicWaitDetailFragment fragment_su_auto_nic_wait_detail = mainActivity().fragment_su_auto_nic_wait_detail; String phone = fragment_su_auto_nic_wait_detail.phone_number.getText().toString(); String idCardNumber = fragment_su_auto_nic_wait_detail.id_card.getText().toString(); selectedPersonDocument.setString("name", fragment_su_auto_nic_wait_detail.name.getText().toString()); if (!TextUtils.isEmpty(idCardNumber)) { CheckIdCard idCard = new CheckIdCard(idCardNumber); if (idCard.validate()) { selectedPersonDocument.setString("id_card_number", idCardNumber); } else { allowIDNumber = false; } } else { selectedPersonDocument.setString("id_card_number", idCardNumber); } if (!TextUtils.isEmpty(phone) && !PhoneNumberCheckUtils.isPhone(phone)) { allowPhone = false; } else { selectedPersonDocument.setString("phone", phone); } } } if (AccountManager.adminLoggedIn()) { FaceDetailFragment faceDetailFragment = mainActivity().fragment_face_detail; String phone = faceDetailFragment.getInPutPhoneNumber(); String idCardNumber = faceDetailFragment.getInPutIdCardNumber(); selectedPersonDocument.setString("sign_up_rule", faceDetailFragment.getSignUpRule()); if ("有效时间内通行".equals(faceDetailFragment.getSignUpRule())) { if (TextUtils.isEmpty(faceDetailFragment.getCheckedTimeRuleId())) { ToastUtil.show("请选择有效时间内通行具体时间段"); canSavePerson = false; } selectedPersonDocument.setString("checked_time_rule_id", faceDetailFragment.getCheckedTimeRuleId()); } selectedPersonDocument.setString("name", faceDetailFragment.getInPutName()); if (!isNewPerson && StringUtils.isEmpty(selectedPersonDocument.getString(Person.name)) && BaseSettingManager.isAutoSignUpNotIDCardModeNeedName()) { ToastUtil.show("姓名不能为空"); success = false; return false; } if (!TextUtils.isEmpty(idCardNumber)) { CheckIdCard idCard = new CheckIdCard(idCardNumber); if (idCard.validate()) { selectedPersonDocument.setString("id_card_number", idCardNumber); } else { if (!isNewPerson) { ToastUtil.show("请输入正确的身份证号码"); success = false; return false; } allowIDNumber = false; } } else { if (!isNewPerson && BaseSettingManager.isAutoSignUpNotIDCardModeNeedIDNumber()) { ToastUtil.show("身份证号码不能为空"); success = false; return false; } selectedPersonDocument.setString("id_card_number", idCardNumber); } if (!TextUtils.isEmpty(phone) && !PhoneNumberCheckUtils.isPhone(phone)) { allowPhone = false; if (!isNewPerson) { ToastUtil.show("请输入正确的手机号码"); return false; } } else { selectedPersonDocument.setString("phone", phone); } if (!isNewPerson && StringUtils.isEmpty(selectedPersonDocument.getString(Person.phone)) && BaseSettingManager.isAutoSignUpNotIDCardModeNeedPhone()) { ToastUtil.show("电话号码不能为空"); success = false; return false; } } if ("已经验证身份证".equals(selectedPersonDocument.getString("verify_status")) || "未验证身份证".equals(selectedPersonDocument.getString("verify_status"))) { if (!allowPhone) { ToastUtil.show("请输入正确的手机号码"); canSavePerson = false; } } else { if (allowPhone && !allowIDNumber) { ToastUtil.show("请输入正确的身份证号码"); canSavePerson = false; } else if (!allowPhone && allowIDNumber) { ToastUtil.show("请输入正确的手机号码"); canSavePerson = false; } else if (!allowPhone && !allowIDNumber) { ToastUtil.show("请输入正确的身份证号码和手机号码"); canSavePerson = false; } } if (isNewPerson) { return true; } else { return success; } } public boolean savePhotoAndFeature() { if (!saveBlob(selectedPersonDocument, "camera_image_feature", getFaceFeature()) || !saveBlob(selectedPersonDocument, "camera_image_path", getFacePositionJpgData())) { System.out.println("没有头像图片和头像特征"); return false; } // System.out.println(selectedPersonDocument.getString("id") + "保存的feature base64=" + selectedPersonDocument.getString("camera_image_feature")); return true; } public boolean isNewPerson() { if (Arrays.asList( mainActivity().fragment_su_logged_nic_wait_face, mainActivity().fragment_su_logged_ic_wait_face).contains(currentFragment)) { return true; } if (!AccountManager.adminLoggedIn()) { return true; } return false; } } return new SavePersonHelper().execute(); } @UiThread public void afterSavePerson(ModelAdapter selectedPersonDocument) { // 解决当修改过人员信息后再点击保存清空数据 try { if (selectedPersonDocument != null) { mainActivity().fragment_face_detail.setFaceDetailUI(selectedPersonDocument.getId()); // personChanged(selectedPersonDocument); } } catch (Exception e) { e.printStackTrace(); } } public void deletePerson() { // 优化后代码 if (selectedPersonDocument == null) { return; } else { if ("1".equals(selectedPersonDocument.getString("auto_init"))) { ToastUtil.show("系统管理员不能被删除"); return; } } confirmDialog = new ConfirmDialog(mainActivity(), "确认是否删除", "确认删除", "取消"); confirmDialog.show(); confirmDialog.setClickListener(new ConfirmDialog.ClickListenerInterface() { @Override public void doConfirm() { FaceId.instance.removeFeatureFromDb(selectedPersonDocument.getId());// 只要特征库有,就删除 PersonManager.deletePerson(selectedPersonDocument); PersonIdentityManager.deletePersonIdentityByPersonId(selectedPersonDocument.getId()); if (mainActivity().currentFragment == mainActivity().fragment_person_manage) { // 此方法删除后会选中前一个,如果是第一个会选中后一个 mainActivity().fragment_face_list.findNextPosition(selectedPersonDocument); } if (mainActivity().currentFragment == mainActivity().fragment_su_logged_nic_wait_detail || mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_compare_success || mainActivity().currentFragment == mainActivity().fragment_su_logged_ic_wait_idcard) { if (BaseSettingManager.isAdminAuthSignUpModeNeedIdCard()) { mainActivity().showFragment(mainActivity().fragment_su_logged_ic_wait_face); } else { mainActivity().showFragment(mainActivity().fragment_su_logged_nic_wait_face); } mainActivity().fragment_person_manage.personChanged(null, false); } confirmDialog.dismiss(); } @Override public void doCancel() { confirmDialog.dismiss(); } }); } //取消 public void giveUpSave() { mainActivity().fragment_menu.person_add(); } public void personChanged(ModelAdapter personDocument) { mainActivity().fragment_face_detail.clearInPutText(); personChanged(personDocument, true); } public void personChanged(ModelAdapter personDocument, boolean reloadFaceList) { personChanged(personDocument, reloadFaceList, false); } //人员管理--选中的人员信息 @UiThread public void personChanged(ModelAdapter personDocument, boolean reloadFaceList, boolean selectPerson) { showHotPoint(personDocument); if (personDocument == null) { if (reloadFaceList) { mainActivity().fragment_face_list.findPersonList(null, null); } } else { if (selectPerson) { mainActivity().fragment_face_list.findPersonList(personDocument.getString("sign_up_status"), new HashMap<>(), personDocument); } all_device = personDocument.getString("all_device"); } selectedPersonDocument = personDocument; mainActivity().fragment_face_detail.setPerson(personDocument); mainActivity().fragment_person_identity.setIdentityWithPerson(personDocument != null ? personDocument.getId() : null); mainActivity().fragment_person_identity.setDevice(all_device); } private void showHotPoint(ModelAdapter personDocument) { if (personDocument == null || "系统初始化分配".equals(personDocument.getString("sign_up_method"))) { mainActivity().fragment_face_detail.needPhone(false); mainActivity().fragment_face_detail.needName(false); mainActivity().fragment_face_detail.needIdNumber(false); } else { // 根据当前的注册方式决定是否显示前面的红点,统一导入的暂不处理 if ("管理员注册".equals(personDocument.getString("sign_up_method"))) { if ("已经验证身份证".equals(personDocument.getString("verify_status")) || "未验证身份证".equals(personDocument.getString("verify_status"))) { mainActivity().fragment_face_detail.needPhone(BaseSettingManager.adminIDCardSignUpModeNeedPhone()); mainActivity().fragment_face_detail.needName(false); mainActivity().fragment_face_detail.needIdNumber(false); } if ("不需要验证身份证".equals(personDocument.getString("verify_status"))) { mainActivity().fragment_face_detail.needName(BaseSettingManager.adminNormalSignUpModeNeedName()); mainActivity().fragment_face_detail.needIdNumber(BaseSettingManager.adminNormalSignUpModeNeedIDNumber()); mainActivity().fragment_face_detail.needPhone(BaseSettingManager.adminNormalSignUpModeNeedPhone()); } } if ("自助注册".equals(personDocument.getString("sign_up_method"))) { if ("已经验证身份证".equals(personDocument.getString("verify_status")) || "未验证身份证".equals(personDocument.getString("verify_status"))) { mainActivity().fragment_face_detail.needPhone(BaseSettingManager.isAutoSignUpIDCardModeNeedPhone()); mainActivity().fragment_face_detail.needName(false); mainActivity().fragment_face_detail.needIdNumber(false); } if ("不需要验证身份证".equals(personDocument.getString("verify_status"))) { mainActivity().fragment_face_detail.needName(BaseSettingManager.isAutoSignUpNotIDCardModeNeedName()); mainActivity().fragment_face_detail.needIdNumber(BaseSettingManager.isAutoSignUpNotIDCardModeNeedIDNumber()); mainActivity().fragment_face_detail.needPhone(BaseSettingManager.isAutoSignUpNotIDCardModeNeedPhone()); } } } } //验证身份证和手机号 private boolean checkIdCardAndPhone() { if (!TextUtils.isEmpty(selectedPersonDocument.getString("id_card_number"))) { CheckIdCard checkIdCard = new CheckIdCard(selectedPersonDocument.getString("id_card_number")); if (!checkIdCard.validate()) { ToastUtil.show("请输入正确的身份证号"); return false; } } if (!TextUtils.isEmpty(selectedPersonDocument.getString("phone"))) { if (!PhoneNumberCheckUtils.isPhone(selectedPersonDocument.getString("phone"))) { ToastUtil.show("请输入正确的手机号"); return false; } } return true; } }