// // ScanCodeViewController.m // LekangGuard // // Created by ecell on 2022/10/11. // #import "ScanCodeViewController.h" #import #import "SelectContactViewController.h" #import "commWebViewController.h" #import @interface ScanCodeViewController () /// 识别后的字符串 @property (strong, nonatomic) NSString *stringValue; @property (nonatomic ,strong) SGScanCode *scanCode; @property (nonatomic, strong) SGScanView *scanView; @end @implementation ScanCodeViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = UIColor.darkTextColor; self.zx_navTitle = GJText(@"添加设备"); [self zx_setRightBtnWithText:GJText(@"二维码/条码丢失?") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { commWebViewController *vc = [commWebViewController new]; vc.url = QRcodeLostURLStr; vc.navTitle = GJText(@"二维码/条码丢失"); [self.navigationController pushViewController:vc animated:YES]; }]; [self.view addSubview:self.scanView]; } - (void)takeCameraAuthorityStatusCompletion:(void(^)(BOOL allow))completion { //获取摄像头权限当前的状态 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)) { //用户禁止app 访问摄像头 或设置了家长控制 可以根据需求 给用户做一个友好的提示来让用户解除限制 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:GJText(@"提示") message:F(GJText(@"请为%@打开相机权限"),APPName) preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:GJText(@"取消") style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { [self.navigationController popViewControllerAnimated:YES]; }]; UIAlertAction *confrmAction = [UIAlertAction actionWithTitle:GJText(@"设置") style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; [alertController addAction:cancelAction]; [alertController addAction:confrmAction]; [self presentViewController:alertController animated:YES completion:nil]; if (completion) { completion(NO); } } else { //判断是真机还是模拟器 if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { if (completion) { completion(YES); } } else { NSLog(@"模拟器中无法打开照相机,请在真机中使用"); if (completion) { completion(NO); } } } } /// 二维码扫描 - (void)setupCamera { self.scanCode = [[SGScanCode alloc] init]; if (![self.scanCode checkCameraDeviceRearAvailable]) return; self.scanCode.delegate = self; self.scanCode.sampleBufferDelegate = self; self.scanCode.preview = self.view; UILabel *bottomTitle = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(14) textColor:UIColor.whiteColor text:GJText(@"扫描设备背面的二维码或者手动添加设备的IMEI/MEID号") Radius:0]; [self.view addSubview:bottomTitle]; [self.view bringSubviewToFront:bottomTitle]; [bottomTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view.mas_bottom).inset(Adapted(180)); make.left.right.equalTo(self.view).inset(25); make.centerX.equalTo(self.view.mas_centerX); }]; UIButton *btn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(15) normalColor:KKWhiteColorColor normalText:GJText(@"手动输入IMEI/MEID") click:^(id x) { [self stop]; [self enterIMEIClick]; }]; [btn setBackgroundColor:KKMainColor]; btn.layer.cornerRadius = 3; btn.layer.masksToBounds = YES; [self.view addSubview:btn]; [self.view bringSubviewToFront:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bottomTitle.mas_bottom).offset(50); make.centerX.equalTo(bottomTitle.mas_centerX); make.left.right.equalTo(self.view).inset(14); make.height.offset(Adapted(44)); }]; } - (void)scanCode:(SGScanCode *)scanCode result:(NSString *)result { [self stop]; if (result.length > 0) { NSMutableDictionary *parameters = nil; if ([result hasPrefix:@"http"]) { parameters = [UICommon getDicWithUrl:result]; [parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { if ([[key lowercaseString] isEqualToString:@"imei"]) { [parameters setObject:obj forKey:@"imei"]; } }]; } else if (result.length <= 15) { parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"imei":result}]; } if (parameters[@"imei"] != nil) { [self nextActionWithParameters:parameters]; } else { EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:GJText(@"提示") subtitle:GJText(@"\n无效的二维码/条码,请重新扫描") AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"确定") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { [self start]; }]; }]; [alertV showAlertView]; } } } - (void)scanCode:(SGScanCode *)scanCode brightness:(CGFloat)brightness { } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; kWeakSelf(self) [self takeCameraAuthorityStatusCompletion:^(BOOL allow) { if (allow) { [weakself setupCamera]; [weakself start]; } }]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self stop]; } /// 开始扫描 - (void)start { [self.scanCode startRunning]; [self.scanView startScanning]; } /// 停止扫描 - (void)stop { [self.scanCode stopRunning]; [self.scanView stopScanning]; } /// 手动输入添加 - (void)enterIMEIClick { kWeakSelf(self) UIAlertController *alert = [UIAlertController alertControllerWithTitle:GJText(@"请输入IMEI/MEID号")message:@""preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.keyboardType = UIKeyboardTypeNumberPad; //设置 UIKeyboardTypeNamePhonePad textField.delegate = self; }]; [alert addAction:[UIAlertAction actionWithTitle:GJText(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { [self start]; }]]; [alert addAction:[UIAlertAction actionWithTitle:GJText(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *textField = alert.textFields[0]; NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"imei":textField.text}]; [weakself nextActionWithParameters:parameters]; }]]; [self presentViewController:alert animated:YES completion:nil]; } #pragma mark 添加学生证 /// 添加学生证 - (void)nextActionWithParameters:(NSMutableDictionary *)parameters { [UICommon MessageUpload:@"加载中"]; [[[APIManager sharedManager] APGET:F(@"%@/%@",ScanDevice_URL, parameters[@"imei"]) parameters:[NSDictionary new] resultClass:ScanCodeModel.class] subscribeNext:^(ScanCodeModel *model) { [UICommon HidenLoading]; /// "1"代表已绑定,"0"该设备未绑定,"2"该设备自己已经绑定了 if (model.binding_status < 2) { SelectContactViewController *vc = [SelectContactViewController new]; vc.model = model; vc.imei = parameters[@"imei"]; [self.navigationController pushViewController:vc animated:YES]; } else { [UICommon HidenLoading]; EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:GJText(@"该设备自己已经绑定了") subtitle:nil AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"确定") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { [self start]; [UICommon HidenLoading]; }]; }]; [alertV showAlertView]; } } error:^(NSError * _Nullable error) { [UICommon HidenLoading]; [self start]; NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } //MARK: UITextFieldDelegate 限制字数 和输入的 字符 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if([@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" containsString:string]){ // 最终的结果 NSString *str = textField.text; NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string]; if(resultStr.length > 15){ return NO; }else{ return YES; } }else{ if([string isEqualToString:@""]){ return YES; } return NO; } } - (SGScanView *)scanView { if (!_scanView) { SGScanViewConfigure *configure = [[SGScanViewConfigure alloc] init]; CGFloat x = 0; CGFloat y = 0; CGFloat w = self.view.frame.size.width; CGFloat h = self.view.frame.size.height; _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(x, y, w, h) configure:configure]; CGFloat scan_x = 0; CGFloat scan_y = 0.18 * self.view.frame.size.height; CGFloat scan_w = self.view.frame.size.width - 2 * x; CGFloat scan_h = self.view.frame.size.height - 2.55 * scan_y; _scanView.scanFrame = CGRectMake(scan_x, scan_y, scan_w, scan_h); __weak typeof(self) weakSelf = self; _scanView.doubleTapBlock = ^(BOOL selected) { __strong typeof(weakSelf) strongSelf = weakSelf; if (selected) { [strongSelf->_scanCode setVideoZoomFactor:4.0]; } else { [strongSelf->_scanCode setVideoZoomFactor:1.0]; } }; } return _scanView; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end