| package com.dayu.qiheonlinelibrary.activity; | 
|   | 
| import android.content.Intent; | 
| import android.os.Bundle; | 
| import android.text.Editable; | 
| import android.text.InputFilter; | 
| import android.text.Spanned; | 
| import android.text.TextUtils; | 
| import android.text.TextWatcher; | 
| import android.view.LayoutInflater; | 
| import android.view.View; | 
| import android.widget.EditText; | 
|   | 
| import androidx.annotation.NonNull; | 
|   | 
| import com.dayu.baselibrary.net.subscribers.SubscriberListener; | 
| import com.dayu.baselibrary.tools.Utils; | 
| import com.dayu.baselibrary.utils.DeviceUtils; | 
| import com.dayu.baselibrary.utils.TipUtil; | 
| import com.dayu.baselibrary.utils.ToastUtil; | 
|   | 
| import com.dayu.qiheonlinelibrary.QHOnLineApplication; | 
| import com.dayu.qiheonlinelibrary.bean.AddUserResult; | 
| import com.dayu.qiheonlinelibrary.bean.UserListResult; | 
| import com.dayu.qiheonlinelibrary.card.UserCard; | 
| import com.dayu.qiheonlinelibrary.databinding.ActivityNewCardQhlBinding; | 
| import com.dayu.qiheonlinelibrary.dbBean.UserCardBean; | 
| import com.dayu.qiheonlinelibrary.net.ApiManager; | 
| import com.dayu.qiheonlinelibrary.net.BaseResponse; | 
| import com.hjq.permissions.OnPermissionCallback; | 
| import com.hjq.permissions.Permission; | 
| import com.hjq.permissions.XXPermissions; | 
| import com.kernal.passportreader.sdk.CardsCameraActivity; | 
| import com.kernal.passportreader.sdk.utils.DefaultPicSavePath; | 
|   | 
| import java.util.Calendar; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
| import java.util.regex.Matcher; | 
| import java.util.regex.Pattern; | 
|   | 
| import kernal.idcard.android.ResultMessage; | 
| import kernal.idcard.camera.CardOcrRecogConfigure; | 
| import kernal.idcard.camera.SharedPreferencesHelper; | 
|   | 
| /** | 
|  * Copyright (C), 2023, | 
|  * Author: zuo | 
|  * Date: 2023-11-10 19:52 | 
|  * Description: 新卡注册 | 
|  */ | 
| public class NewCardActivityQHOline extends QHOlineBaseActivity { | 
|   | 
|     public static final int SCAN_IDCARD_REQUEST = 1; | 
|   | 
|     ActivityNewCardQhlBinding newCardBinding; | 
|   | 
|     static NewCardActivityQHOline newCardActivity; | 
|     private int defValueMainId = 2; | 
|     private int defValueSubId = 0; | 
|   | 
|   | 
|     @Override | 
|     protected void onCreate(Bundle savedInstanceState) { | 
|         super.onCreate(savedInstanceState); | 
|         newCardActivity = this; | 
|         newCardBinding = ActivityNewCardQhlBinding.inflate(LayoutInflater.from(this)); | 
|         setContentView(newCardBinding.getRoot()); | 
|         setData(); | 
|         initView(); | 
|     } | 
|   | 
|     private void initView() { | 
|         setPricePoint(newCardBinding.newCardMorny); | 
| //        newCardBinding.newCardName.setFilters(new InputFilter[]{new ChineseInputFilter()}); | 
|         newCardBinding.newCardId.setFilters(new InputFilter[]{new AlphaNumericXFilter(), new InputFilter.LengthFilter(18)}); | 
|         //身份证识别 | 
|         newCardBinding.newCardScanBtn.setOnClickListener(new View.OnClickListener() { | 
|             @Override | 
|             public void onClick(View v) { | 
|                 rxPermission(); | 
|             } | 
|         }); | 
|         //开户 | 
|         newCardBinding.newCardRegistBtn.setOnClickListener(v -> { | 
|   | 
|             String userName = newCardBinding.newCardName.getText().toString(); | 
|             String phone = newCardBinding.newCardPhone.getText().toString(); | 
|             String userID = newCardBinding.newCardId.getText().toString().toUpperCase(); | 
|             String morny = newCardBinding.newCardMorny.getText().toString(); | 
|   | 
|             if (!TextUtils.isEmpty(userName) | 
|                     && !TextUtils.isEmpty(phone) && !TextUtils.isEmpty(userID) | 
|             ) { | 
|                 if (userName.length() <= 1) { | 
|                     TipUtil.show(NewCardActivityQHOline.this, "请输入正确姓名"); | 
|                 } else if (phone.length() < 11 || !isValidPhoneNumber(phone)) { | 
|                     TipUtil.show(NewCardActivityQHOline.this, "请输入正确手机号"); | 
|                 } else if (!Utils.check(userID)) { | 
|                     TipUtil.show(NewCardActivityQHOline.this, "请输入正确身份证号"); | 
|                 } else { | 
|                     selectUser(userID, phone, userName, morny); | 
|                 } | 
|   | 
|             } else { | 
|                 TipUtil.show(NewCardActivityQHOline.this, "请输入完整内容"); | 
|             } | 
|         }); | 
|     } | 
|   | 
|     //校验手机号 | 
|     private boolean isValidPhoneNumber(String phoneNumber) { | 
|         // 定义手机号的正则表达式,确保数字部分没有连续6位相同的数字 | 
|         String phoneRegex = "^1[0-9]{10}$"; | 
|   | 
|         // 创建 Pattern 对象 | 
|         Pattern pattern = Pattern.compile(phoneRegex); | 
|   | 
|         // 创建 matcher 对象 | 
|         Matcher matcher = pattern.matcher(phoneNumber); | 
|   | 
|         // 判断手机号是否匹配正则表达式 | 
|         return matcher.matches() && !hasSixConsecutiveSameDigits(phoneNumber); | 
|     } | 
|   | 
|     /** | 
|      * 判断是否有6个相同的连续数字 | 
|      * | 
|      * @param input | 
|      * @return | 
|      */ | 
|     public static boolean hasSixConsecutiveSameDigits(String input) { | 
|         char[] digits = input.toCharArray(); | 
|   | 
|         for (int i = 0; i <= digits.length - 6; i++) { | 
|             boolean consecutiveSame = true; | 
|             for (int j = 1; j < 6; j++) { | 
|                 if (digits[i + j] != digits[i + j - 1]) { | 
|                     consecutiveSame = false; | 
|                     break; | 
|                 } | 
|             } | 
|             if (consecutiveSame) { | 
|                 return true; | 
|             } | 
|         } | 
|         return false; | 
|     } | 
|   | 
|     private void rxPermission() { | 
|         XXPermissions.with(this) | 
|                 // 申请单个权限 | 
| //                    .permission(Permission.RECORD_AUDIO) | 
|                 // 申请多个权限 | 
|                 .permission(Permission.CAMERA) | 
|                 .request(new OnPermissionCallback() { | 
|   | 
|                     @Override | 
|                     public void onGranted(@NonNull List<String> permissions, boolean allGranted) { | 
|                         if (allGranted) {//所有申请的权限都已通过 | 
|                             startCamera(); | 
|                         } | 
|                     } | 
|   | 
|                     @Override | 
|                     public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) { | 
|                         if (doNotAskAgain) { | 
|                             // 如果是被永久拒绝就跳转到应用权限系统设置页面 | 
|                         } else { | 
| //                                toast("获取录音和日历权限失败"); | 
|                         } | 
|                     } | 
|                 }); | 
|     } | 
|   | 
|   | 
|     @Override | 
|     protected void onActivityResult(int requestCode, int resultCode, Intent data) { | 
|         super.onActivityResult(requestCode, resultCode, data); | 
|         if (resultCode == RESULT_OK) { | 
|             switch (requestCode) { | 
|                 case SCAN_IDCARD_REQUEST: | 
|                     try { | 
|                         //跳转扫描界面识别完成之后,数据回传 | 
|                         if (data != null) { | 
|                             //数据回传的获取 | 
|                             Bundle bundle = data.getBundleExtra("resultbundle"); | 
|                             //bundle不为null,代表这识别成功 | 
|                             if (bundle != null) { | 
|                                 ResultMessage resultMessage = (ResultMessage) bundle.getSerializable("resultMessage"); | 
|                                 newCardBinding.newCardName.setText(resultMessage.GetRecogResult[1]); | 
|                                 newCardBinding.newCardId.setText(resultMessage.GetRecogResult[6]); | 
|                                 newCardBinding.newCardIdTip.setVisibility(View.VISIBLE); | 
|                             } else { | 
|                                 String error = data.getStringExtra("error"); | 
|                                 String StrPath = data.getStringExtra("strpicpath"); | 
|                                 ToastUtil.show(error); | 
|                             } | 
|   | 
|                         } | 
|                     } catch (Exception e) { | 
|                         e.printStackTrace(); | 
|                     } | 
|   | 
|                     break; | 
|             } | 
|         } | 
|   | 
|     } | 
|   | 
|     private void startCamera() { | 
|         CardOcrRecogConfigure.getInstance() | 
|                 .initLanguage(getApplicationContext()) | 
|                 .setSaveCut(true) | 
|                 .setOpenIDCopyFuction(true) | 
|                 .setnMainId(getSharedPreferencesStoreMainId()) | 
|                 .setnSubID(getSharedPreferencesStoreSubId()) | 
|                 .setFlag(0) | 
|                 .setnCropType(0) | 
|                 .setSavePath(new DefaultPicSavePath(this, true)); | 
|         Intent intent = new Intent(this, CardsCameraActivity.class); | 
|         startActivityForResult(intent, SCAN_IDCARD_REQUEST); | 
|     } | 
|   | 
|     public int getSharedPreferencesStoreMainId() { | 
|         return SharedPreferencesHelper.getInt( | 
|                 getApplicationContext(), "nMainId", defValueMainId); | 
|     } | 
|   | 
|     public int getSharedPreferencesStoreSubId() { | 
|         return SharedPreferencesHelper.getInt( | 
|                 getApplicationContext(), "nSubID", defValueSubId); | 
|     } | 
|   | 
|   | 
|     private void setData() { | 
|   | 
|         try { | 
|             newCardBinding.newCardArerNumber.setText(QHOnLineApplication.getInstance().arerNumber); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|   | 
|     } | 
|   | 
|   | 
|     public boolean validateName(String name) { | 
|         // 使用正则表达式匹配姓名 | 
|         String regex = "^[\\u4e00-\\u9fa5]+$"; // 只允许中文字符 | 
|         return name.matches(regex); | 
|     } | 
|   | 
|     private static class AlphaNumericXFilter implements InputFilter { | 
|         // 正则表达式定义只能输入数字和字母 "X"(大写或小写) | 
|         private final String regex = "[0-9Xx]*"; | 
|   | 
|         @Override | 
|         public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { | 
|             // 检查每个输入字符是否符合正则表达式 | 
|             for (int i = start; i < end; i++) { | 
|                 if (!String.valueOf(source.charAt(i)).matches(regex)) { | 
|                     return ""; // 不符合规定的字符被过滤掉 | 
|                 } | 
|             } | 
|             return null; // 允许输入字符 | 
|         } | 
|     } | 
|   | 
|     public class ChineseInputFilter implements InputFilter { | 
|         @Override | 
|         public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { | 
|             StringBuilder builder = new StringBuilder(); | 
|             for (int i = start; i < end; i++) { | 
|                 char currentChar = source.charAt(i); | 
|                 // 只允许汉字 | 
|                 if (isChineseCharacter(currentChar)) { | 
|                     builder.append(currentChar); | 
|                 } | 
|             } | 
|             return builder.toString(); | 
|         } | 
|   | 
|         private boolean isChineseCharacter(char c) { | 
|             // 这里使用Unicode范围判断是否为汉字 | 
|             // 汉字的Unicode范围是:0x4e00 - 0x9fa5 | 
|             return (c >= 0x4e00 && c <= 0x9fa5); | 
|         } | 
|   | 
|     } | 
|   | 
|     @Override | 
|     protected void onDestroy() { | 
|         super.onDestroy(); | 
|         newCardActivity = null; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 查询用户 | 
|      * | 
|      * @param peasantIdNumber | 
|      * @param peasantPhone | 
|      * @param peasantName | 
|      * @param morny           开卡金额 | 
|      */ | 
|     private void selectUser(String peasantIdNumber, String peasantPhone, String peasantName, String morny) { | 
|         Map<String, Object> data = new HashMap<>(); | 
|         data.put("adcd", QHOnLineApplication.getInstance().adcd); | 
|         data.put("peasantIdNumber", peasantIdNumber); | 
|         data.put("current", 1); | 
|         data.put("pageSize", 10); | 
|         ApiManager.getInstance().requestPostLoading(this, "base/peasant/pagePeasantVo", UserListResult.class, data, new SubscriberListener<BaseResponse<UserListResult>>() { | 
|             @Override | 
|             public void onNext(BaseResponse<UserListResult> t) { | 
|                 if (t.isSuccess()) { | 
|                     if (t.getData().getTotal() == 0) { | 
|                         if (!TextUtils.isEmpty(QHOnLineApplication.getInstance().planId)) { | 
|                             addUser(peasantIdNumber, peasantPhone, peasantName, QHOnLineApplication.getInstance().planId, morny); | 
|                         } | 
|                     } else { | 
|                         startNFCWreatActivity(peasantIdNumber, t.getData().getRecords().get(0).getPeasantPhone(), t.getData().getRecords().get(0).getPeasantName(), t.getData().getRecords().get(0).getPeasantId(), morny); | 
|                     } | 
|                 } else { | 
|                     ToastUtil.show(t.getMsg()); | 
|                 } | 
|             } | 
|         }); | 
|     } | 
|   | 
|     private void startNFCWreatActivity(String peasantIdNumber, String peasantPhone, String peasantName, String peasanId, String morny) { | 
|         UserCard userCard = new UserCard(); | 
|         userCard.setArerNumber(Integer.valueOf(QHOnLineApplication.getInstance().arerNumber)); | 
|         userCard.setUserCode(QHOnLineApplication.getInstance().adcd); | 
|         userCard.setCardWriteState(0); | 
|         userCard.setCardState(1); | 
|         userCard.setBalance(0); | 
|         userCard.setSurplusElecticity(0); | 
|         userCard.setTotalMorny(0); | 
|         userCard.setRechargeDate(Calendar.getInstance()); | 
|         userCard.setElectricPrice(Float.valueOf(QHOnLineApplication.getInstance().electriclePriceStr)); | 
|         UserCardBean userCardBean = new UserCardBean(); | 
|   | 
|         userCardBean.setUserName(peasantName); | 
|         userCardBean.setUserID(peasantIdNumber); | 
|         userCardBean.setPhone(peasantPhone); | 
|         userCardBean.setPersonId(peasanId); | 
|         if (!TextUtils.isEmpty(morny)) { | 
|             userCardBean.setCardMorny(Integer.valueOf(morny)); | 
|         } else { | 
|             userCardBean.setCardMorny(0); | 
|         } | 
|         userCardBean.setAddressCode(QHOnLineApplication.getInstance().adcd); | 
|         userCardBean.setAddressName(QHOnLineApplication.getInstance().addressName); | 
|         userCardBean.setArerNumber(QHOnLineApplication.getInstance().arerNumber); | 
|         userCardBean.setDate(System.currentTimeMillis()); | 
|         userCardBean.setSerial(DeviceUtils.getSN()); | 
|   | 
|         Intent intent = new Intent(NewCardActivityQHOline.this, NFCWreatActivityQHOline.class); | 
|         intent.putExtra("userCard", userCard); | 
|         intent.putExtra("dbUserCard", userCardBean); | 
|         startActivity(intent); | 
|     } | 
|   | 
|     /** | 
|      * 添加用户 | 
|      * | 
|      * @param peasantIdNumber | 
|      * @param peasantPhone | 
|      * @param peasantName | 
|      * @param planId          价格ID | 
|      */ | 
|     private void addUser(String peasantIdNumber, String peasantPhone, String peasantName, String planId, String morny) { | 
|         Map<String, Object> data = new HashMap<>(); | 
|         data.put("planId", planId); | 
|         data.put("adcd", QHOnLineApplication.getInstance().adcd); | 
|         data.put("peasantIdNumber", peasantIdNumber); | 
|         data.put("peasantCode", ""); | 
|         data.put("peasantName", peasantName); | 
|         data.put("peasantPhone", peasantPhone); | 
|         ApiManager.getInstance().requestPostLoading(this, "base/peasant/savePeasant", AddUserResult.class, data, new SubscriberListener<BaseResponse<AddUserResult>>() { | 
|             @Override | 
|             public void onNext(BaseResponse<AddUserResult> t) { | 
|                 if (t.isSuccess()) { | 
|                     startNFCWreatActivity(peasantIdNumber, t.getData().getPeasantPhone(), t.getData().getPeasantName(), t.getData().getPeasantId(), morny); | 
|                 } else { | 
|                     ToastUtil.show(t.getMsg()); | 
|                 } | 
|             } | 
|         }); | 
|     } | 
|   | 
|     //校验金额 | 
|     public void setPricePoint(final EditText editText) { | 
|         editText.addTextChangedListener(new TextWatcher() { | 
|             @Override | 
|             public void onTextChanged(CharSequence s, int start, int before, | 
|                                       int count) { | 
|                 if (s.toString().contains(".")) { | 
|                     if (s.length() - 1 - s.toString().indexOf(".") > 2) { | 
|                         s = s.toString().subSequence(0, | 
|                                 s.toString().indexOf(".") + 3); | 
|                         editText.setText(s); | 
|                         editText.setSelection(s.length()); | 
|                     } | 
|                 } | 
|                 if (s.toString().trim().substring(0).equals(".")) { | 
|                     s = "0" + s; | 
|                     editText.setText(s); | 
|                     editText.setSelection(2); | 
|                 } | 
|   | 
|                 if (s.toString().startsWith("0") | 
|                         && s.toString().trim().length() > 1) { | 
|                     if (!s.toString().substring(1, 2).equals(".")) { | 
|                         editText.setText(s.subSequence(0, 1)); | 
|                         editText.setSelection(1); | 
|                     } | 
|                 } | 
|                 String morny = editText.getText().toString(); | 
|                 if (!TextUtils.isEmpty(morny)) { | 
| //                    rechageWater = MornyUtil.intDiv(MornyUtil.changeY2F(editText.getText().toString()), waterPrice); | 
| //                    binding.redRechargeWater.setText("充值水量:" + rechageWater + " 立方米(吨)"); | 
|                 } | 
|   | 
|   | 
|             } | 
|   | 
|             @Override | 
|             public void beforeTextChanged(CharSequence s, int start, int count, | 
|                                           int after) { | 
|   | 
|             } | 
|   | 
|             @Override | 
|             public void afterTextChanged(Editable s) { | 
|                 // TODO Auto-generated method stub | 
|   | 
|             } | 
|   | 
|         }); | 
|   | 
|     } | 
| } |