JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
//  Copyright © 2018年 dahua. All rights reserved.
//    手动输入序列号
 
import UIKit
 
fileprivate let kMinScCodeLength = 6
fileprivate let kMaxScCodeLength = 8
fileprivate let kMinDeviceIdLength = 10
fileprivate let kMaxDeviceIdLength = 32
 
class DHInputSNViewController: DHAddBaseViewController, DHIdentifyContainerProtocol {
    
    
    public static func storyboardInstance() -> DHInputSNViewController {
        let storyboard = UIStoryboard(name: "AddDevice", bundle: Bundle.dh_addDeviceBundle())
        if let controller = storyboard.instantiateViewController(withIdentifier: "DHInputSNViewControllerSID") as? DHInputSNViewController {
            return controller
        }
        return DHInputSNViewController()
    }
    
    @IBOutlet weak var snTipLabel: UILabel!
    @IBOutlet weak var qrCodeImageView: UIImageView!
    @IBOutlet weak var inputSnTextField: LCTextField!
    @IBOutlet weak var boxTipLabel: UILabel!
    @IBOutlet weak var nextStepButton: UIButton!
    @IBOutlet weak var autoKeyboardView: DHAutoKeyboardView!
    @IBOutlet weak var inputScTextField: LCTextField!
    @IBOutlet weak var scTipLabel: UILabel!
    @IBOutlet weak var qrImageHeightConstraint: NSLayoutConstraint!
    
    public var qrCodeScanFailedClosure: (() -> Void)? = nil
    
    // MARK: Life Cycles
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.dhcolor_c43()
        self.title = "add_device_title".lc_T
        setupCustomContens()
        
        // Do any additional setup after loading the view.
        
        //⚠️ 二维码扫描失败进入到手动输入SN界面会走这个closure
        self.qrCodeScanFailedClosure?()
        
        //按钮样式配置
        nextStepButton.layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
        nextStepButton.backgroundColor = DHModuleConfig.shareInstance().commonButtonColor()
        
        //颜色规范
        snTipLabel.textColor = UIColor.dhcolor_c2()
        boxTipLabel.textColor = UIColor.dhcolor_c5()
        scTipLabel.textColor = UIColor.dhcolor_c5()
        nextStepButton.setTitleColor(UIColor.dhcolor_c43(), for: .normal)
        
        setupTextFiledStyle(textField: inputSnTextField)
        setupTextFiledStyle(textField: inputScTextField)
        
        //海外输入限制处理,国内不需要进行限制
        inputSnTextField.lc_setInputRule(withRegEx: "^[A-Za-z0-9:-]$", andInputLength: UInt32(kMaxDeviceIdLength))
        inputScTextField.lc_setInputRule(withRegEx: "^[A-Za-z0-9:-]$", andInputLength: UInt32(kMaxScCodeLength))
        
        nextStepButton.dh_enable = false
        inputSnTextField.textChanged = { [weak self] text in
            
            guard let `self` = self else {
                return
            }
            
            let snText = text ?? ""
            
            //【*】SMB不限制SC码位数
            self.nextStepButton.dh_enable = snText.count >= kMinDeviceIdLength
        }
        
        inputScTextField.textChanged = { [weak self] text in
            
            guard let `self` = self else {
                return
            }
            
            let snCount = self.inputSnTextField.text?.count ?? 0
 
            //【*】SMB不限制SC码位数
            self.nextStepButton.dh_enable = snCount >= kMinDeviceIdLength
        }
        
        autoKeyboardView.relatedView = nextStepButton
        
        //5s小屏手机兼容
        if dh_screenHeight < 667 {
            qrImageHeightConstraint.constant = 180
        }
        
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        //每次都要重置
        DHAddDeviceManager.sharedInstance.reset()
        
        //开启局域网搜索
        DHNetSDKSearchManager.sharedInstance()?.startSearch()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
    private func setupCustomContens() {
        snTipLabel.text = "add_device_input_sn_under_device".lc_T
        inputSnTextField.placeholder = "add_device_input_sn".lc_T
        nextStepButton.setTitle("common_next".lc_T, for: .normal)
        inputScTextField.placeholder = "add_device_input_sccode".lc_T
        
        //SMB特性:不显示乐盒
        boxTipLabel.text = ""
        scTipLabel.text = "add_device_without_input_sccode_tip".lc_T
        
        qrCodeImageView.image = UIImage(named: "adddevice_pic_serialnumber")
    }
    
    private func setupTextFiledStyle(textField: UITextField) {
        textField.backgroundColor = UIColor.clear
        textField.layer.borderWidth = 0.5
        textField.layer.borderColor = UIColor.dhcolor_c8().cgColor
        textField.autocapitalizationType = .allCharacters
        textField.keyboardType = .asciiCapable
        textField.leftViewMode = .always
        textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 45))
    }
    
    //MAKR: Actions
    @IBAction func onNextAction(_ sender: Any) {
        //SMB逻辑与海外一致,8位或不输入
        let scCode = inputScTextField.text ?? ""
        
        //判断是否是8位的安全验证码
        if scCode.count == kMaxScCodeLength {
            DHAddDeviceManager.sharedInstance.initialPassword = scCode
            DHAddDeviceManager.sharedInstance.isSupportSC = true
            DHAddDeviceManager.sharedInstance.isManualInputSC = true
        }
        
        inputSnTextField.resignFirstResponder()
        inputScTextField.resignFirstResponder()
        
        //统一转换成大写
        let deviceId = inputSnTextField.text ?? ""
        let presenter = DHIdentifyPresenter()
        presenter.setup(container: self)
        presenter.getDeviceInfo(deviceId: deviceId.uppercased(), qrModel: nil, ncCode: nil, marketModel: nil, manualCheckCode: scCode)
        
        addDeviceStartLog(deviceId: deviceId, scCode: scCode)
    }
    
    private func addDeviceStartLog(deviceId: String, scCode: String) {
        let dictStr = "{SN: \(deviceId), SC: \(scCode), imeiCode: \("")}"
        let model = DHAddDeviceLogModel()
        model.bindDeviceType = StartAddType.SN
        model.inputData = dictStr
        DHAddDeviceLogManager.shareInstance.addDeviceStartLog(model: model)
    }
    
    // MARK: DHIdentifyContainerProtocol
    func navigationVC() -> UINavigationController? {
        return self.navigationController
    }
    
    func mainView() -> UIView {
        return self.view
    }
    
    func mainController() -> UIViewController {
        return self
    }
    
    func pauseIdentify() {
        
    }
    
    func resumeIdenfity() {
        
    }
    
    func showAddBoxGuidView(needShoeBox: @escaping ((Bool) -> Void)) {
        
    }
    
    func retry() {
        
    }
    
    override func isRightActionHidden() -> Bool {
        return true
    }
    
    func smb_updateUI(deviceInfo: DHUserDeviceBindInfo) {
        
    }
}