ScanViewController.m 5.17 KB
Newer Older
Sandy's avatar
Sandy committed
1 2 3 4 5 6 7 8 9 10
//
//  ScanViewController.m
//  Car
//
//  Created by Javen on 2016/12/27.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "ScanViewController.h"
#import "CardViewController.h"
Sandy's avatar
Sandy committed
11
#import "WatchLoginViewController.h"
Sandy's avatar
Sandy committed
12 13
@interface ScanViewController ()
@property (strong, nonatomic) UIButton *btnGoInput;
Sandy's avatar
Sandy committed
14
@property (strong, nonatomic) UILabel *labelInfo;
Sandy's avatar
Sandy committed
15 16 17 18 19 20
@end

@implementation ScanViewController

- (void)viewDidLoad {
  [super viewDidLoad];
Sandy's avatar
Sandy committed
21
  self.title = @"扫码";
Sandy's avatar
Sandy committed
22 23 24 25 26 27
  
  // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
Sandy's avatar
Sandy committed
28
  [self labelInfo];
Sandy's avatar
Sandy committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  
  switch (self.type) {
    case scanTypeCard:
    {
      [self btnGoInput];
    }
      break;
    case scanTypeLogin:
    {
      
    }
      break;
      
    default:
      break;
  }
Sandy's avatar
Sandy committed
45 46
}

Sandy's avatar
Sandy committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
- (UILabel *)labelInfo {
  CGFloat y = kHeight/2 - 44 - (kWidth - 120)/2 - 60;
  if (!_labelInfo) {
    _labelInfo = [[UILabel alloc] initWithFrame:CGRectMake(0, y, kWidth, 30)];
    _labelInfo.text = @"将二维码放入框中,即可自动扫描";
    _labelInfo.font = [UIFont systemFontOfSize:13];
    _labelInfo.textAlignment = NSTextAlignmentCenter;
    _labelInfo.textColor = [UIColor whiteColor];
//    _labelInfo.backgroundColor = [UIColor redColor];
    [self.view addSubview:_labelInfo];
  }
  
  return _labelInfo;
}

Sandy's avatar
Sandy committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
- (UIButton *)btnGoInput {
  if (!_btnGoInput) {
    _btnGoInput = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnGoInput.frame = CGRectMake(0, kHeight - 200, 120, 48);
    _btnGoInput.backgroundColor = [UIColor grayColor];
    [_btnGoInput setTitle:@"手工输入" forState:UIControlStateNormal];
    [_btnGoInput setTitleColor:[UIColor colorWithRed:0.89 green:0.44 blue:0.23 alpha:1.00] forState:UIControlStateNormal];
    _btnGoInput.layer.cornerRadius = 24;
    _btnGoInput.centerX = self.view.centerX;
    [_btnGoInput addTarget:self action:@selector(actionGoInput) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnGoInput];
  }
  return _btnGoInput;
}


/**
 跳转到手工输入界面
 */
- (void)actionGoInput {
  [self goCardVCWithNumber:nil];
}

Sandy's avatar
Sandy committed
85 86 87 88

/**
 跳转核销卡券页面
 */
Sandy's avatar
Sandy committed
89 90 91 92 93 94
- (void)goCardVCWithNumber:(NSString *)number {
  CardViewController *cardVC = [CardViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
  cardVC.cardNumber = number;
  [kGlobal.mainNaVC pushViewController:cardVC animated:YES];
}

Sandy's avatar
Sandy committed
95
- (void)goWatchLoginWithUrl:(NSString *)url {
Sandy's avatar
Sandy committed
96 97 98 99 100 101 102 103 104
  NSString *device = [self getStringAfter:@"deviceId=" string:url];
  NSString *token = [self getStringAfter:@"token=" string:url];
  WS(weakSelf);
  if (device == nil || token == nil) {
    [MBProgressHUD j_error:@"设备信息异常!" complete:^{
      [weakSelf reStartDevice];
    }];
    return;
  }
Sandy's avatar
Sandy committed
105
  WatchLoginViewController *loginVC = [WatchLoginViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
Sandy's avatar
Sandy committed
106 107
  loginVC.device = device;
  loginVC.token = token;
Sandy's avatar
Sandy committed
108 109
  [kGlobal.mainNaVC pushViewController:loginVC animated:YES];
}
Sandy's avatar
Sandy committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#pragma mark -实现类继承该方法,作出对应处理
- (void)scanResultWithArray:(NSArray<LBXScanResult *> *)array {

  if (array.count < 1) {
   

    return;
  }

  //经测试,可以同时识别2个二维码,不能同时识别二维码和条形码
  for (LBXScanResult *result in array) {

    NSLog(@"scanResult:%@", result.strScanned);
  }

  LBXScanResult *scanResult = array[0];

  NSString *strResult = scanResult.strScanned;

  self.scanImage = scanResult.imgScanned;

  if (!strResult) {


    return;
  }

  //震动提醒
  [LBXScanWrapper systemVibrate];
  //声音提醒
  [LBXScanWrapper systemSound];
Sandy's avatar
Sandy committed
141 142 143 144
  
  if (self.type == scanTypeCard) {
    
    [self goCardVCWithNumber:strResult];
Sandy's avatar
Sandy committed
145
    
Sandy's avatar
Sandy committed
146 147 148
  }else if (self.type == scanTypeLogin){
    [self goWatchLoginWithUrl:strResult];
  }
Sandy's avatar
Sandy committed
149 150 151

}

Sandy's avatar
Sandy committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/**
 从prefix后开始截取连续字符串(字符串中可包涵数字字母)
 
 @param prefix 从哪里开始截取
 @param string 需要截取的字符串
 @return 截取后的字符串
 */
- (NSString *)getStringAfter:(NSString *)prefix string:(NSString *)string {
  NSString *grex = [NSString stringWithFormat:@"%@[a-z0-9A-Z]*",prefix];
  
  NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:grex options:NSRegularExpressionCaseInsensitive error:nil];
  
  NSArray * matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
  if (matches.count == 1) {
    NSTextCheckingResult *result = matches[0];
    NSString *tempString = [string substringWithRange:result.range];
    NSString *matchedString = [tempString componentsSeparatedByString:@"="][1];
    return matchedString;
  }
  CLog(@"无法匹配到prefix:%@ \n string = %@",prefix, string);
  return nil;
}

Sandy's avatar
Sandy committed
175 176
//- (void)goWatchLoginViewWith

Sandy's avatar
Sandy committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

/*
#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