Commit 358f08bd authored by Alex朱枝文's avatar Alex朱枝文

扫码增加

parent 633eaf09
This diff is collapsed.
...@@ -8,22 +8,44 @@ ...@@ -8,22 +8,44 @@
import UIKit import UIKit
// MARK: - 扫码控制器
class YHScanViewController: LBXScanViewController { class YHScanViewController: LBXScanViewController {
// MARK: - Properties
private let topOffset = 44.0 private let topOffset = 44.0
private let leftOffset = 48.0 private let leftOffset = 48.0
/// 扫码支持的类型(可以是单个或多个类型的组合)
var scanTypes: YHScanType? {
didSet {
updateUIForScanType()
}
}
/// 扫码成功回调
var scanCompletion: YHScanCompletion?
/// 提示文字标签
private lazy var instructionLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(hexString: "#EBF0F9")
label.font = .PFSC_M(ofSize: 18)
label.textAlignment = .center
label.numberOfLines = 0
return label
}()
// MARK: - Lifecycle
override var preferredStatusBarStyle: UIStatusBarStyle { override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent return .lightContent
} }
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
gk_navTitleColor = .white setupNavigationBar()
gk_navTitle = "扫码"
gk_navBarAlpha = 0.0
gk_navBackgroundColor = .clear
gk_backImage = UIImage(named: "back_icon_white")
setupScanStyle() setupScanStyle()
setupInstructionLabel()
updateUIForScanType()
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
...@@ -32,20 +54,73 @@ class YHScanViewController: LBXScanViewController { ...@@ -32,20 +54,73 @@ class YHScanViewController: LBXScanViewController {
if view.subviews.contains(navBar) { if view.subviews.contains(navBar) {
view.bringSubviewToFront(navBar) view.bringSubviewToFront(navBar)
} }
if view.subviews.contains(instructionLabel) {
view.bringSubviewToFront(instructionLabel)
}
} }
// override func handleCodeResult(arrayResult: [LBXScanResult]) { override func handleCodeResult(arrayResult: [LBXScanResult]) {
// // guard let firstResult = arrayResult.first,
// } let code = firstResult.strScanned else {
return
}
guard let scanTypes = scanTypes else {
super.handleCodeResult(arrayResult: arrayResult)
return
}
// 停止扫描
stopScan()
deinit { // 识别二维码类型
// let recognizedType = scanTypes.recognizeType(from: code)
// 验证类型
if recognizedType == nil && scanTypes != .all && scanTypes.rawValue != 0 {
// 类型不匹配,显示错误提示
showError(message: "二维码类型不匹配,请扫描正确的二维码")
// 延迟后重新开始扫描
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { [weak self] in
self?.startScan()
} }
return
}
// 震动反馈
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
// 创建结果
let result = YHScanResult(
code: code,
recognizedType: recognizedType,
supportedTypes: scanTypes
)
// 回调处理
if let completion = scanCompletion {
completion(result)
navigationController?.popViewController(animated: true)
} else {
// 如果没有设置回调,使用默认处理
handleDefaultResult(result)
}
}
deinit {
print("YHScanViewController deinit")
}
} }
// MARK: - Setup
extension YHScanViewController { extension YHScanViewController {
private func setupNavigationBar() {
gk_navTitleColor = .white
gk_navBarAlpha = 0.0
gk_navBackgroundColor = .clear
gk_backImage = UIImage(named: "back_icon_white")
}
private func setupScanStyle() { private func setupScanStyle() {
var style = LBXScanViewStyle() var style = LBXScanViewStyle()
style.centerUpOffset = topOffset style.centerUpOffset = topOffset
...@@ -56,13 +131,155 @@ extension YHScanViewController { ...@@ -56,13 +131,155 @@ extension YHScanViewController {
style.photoframeAngleH = 18 style.photoframeAngleH = 18
style.isNeedShowRetangle = false style.isNeedShowRetangle = false
style.anmiationStyle = LBXScanViewAnimationStyle.LineMove style.anmiationStyle = LBXScanViewAnimationStyle.LineMove
// 扫描横线图片
style.animationImage = UIImage(named: "scan_move_line") style.animationImage = UIImage(named: "scan_move_line")
// 4个角的颜色
style.colorAngle = UIColor.white style.colorAngle = UIColor.white
// 非矩形框区域颜色
style.color_NotRecoginitonArea = UIColor.black.withAlphaComponent(0.3) style.color_NotRecoginitonArea = UIColor.black.withAlphaComponent(0.3)
scanStyle = style scanStyle = style
} }
private func setupInstructionLabel() {
view.addSubview(instructionLabel)
instructionLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-45)
make.left.equalToSuperview().offset(20)
make.right.equalToSuperview().offset(-20)
}
}
private func updateUIForScanType() {
guard let scanTypes = scanTypes else {
gk_navTitle = YHScanType.all.title
instructionLabel.text = YHScanType.all.instructionText
return
}
gk_navTitle = scanTypes.title
instructionLabel.text = scanTypes.instructionText
}
}
// MARK: - Result Handling
extension YHScanViewController {
/// 默认结果处理(当没有设置回调时)
private func handleDefaultResult(_ result: YHScanResult) {
guard let type = result.recognizedType else {
showSuccessAndPop(message: "扫码成功")
return
}
if type == .checkIn {
handleCheckIn(code: result.code)
} else if type == .smartCabinet {
handleSmartCabinet(code: result.code)
}
}
// MARK: - 业务处理方法
private func handleCheckIn(code: String) {
printLog("签到扫码: \(code)")
// TODO: 调用签到 API
showSuccessAndPop(message: "签到成功")
}
private func handleSmartCabinet(code: String) {
printLog("智能柜扫码: \(code)")
// TODO: 调用智能柜开柜 API
showSuccessAndPop(message: "开柜成功")
}
// MARK: - Helper
private func showError(message: String) {
YHCommonAlertView.show("提示", message, "", "确定", fullGuestureEnable: false) {
} callBack: {
}
}
private func showSuccessAndPop(message: String) {
YHCommonAlertView.show("成功", message, "", "确定", fullGuestureEnable: false) {
} callBack: { [weak self] in
self?.navigationController?.popViewController(animated: true)
}
}
}
// MARK: - 便捷初始化
extension YHScanViewController {
/// 便捷初始化方法
/// - Parameters:
/// - types: 扫码支持的类型(可以是单个或多个的组合)
/// - completion: 扫码成功回调
/// - Returns: 扫码控制器实例
static func create(types: YHScanType, completion: YHScanCompletion? = nil) -> YHScanViewController {
let vc = YHScanViewController()
vc.scanTypes = types
vc.scanCompletion = completion
return vc
}
} }
// MARK: - 使用示例
/*
// 示例1: 单一类型 - 仅签到
let checkInVC = YHScanViewController.create(types: .checkIn) { result in
print("签到码: \(result.code)")
print("识别类型: \(result.recognizedType)")
}
navigationController?.pushViewController(checkInVC, animated: true)
// 示例2: 单一类型 - 仅智能柜
let cabinetVC = YHScanViewController.create(types: .smartCabinet) { result in
print("智能柜码: \(result.code)")
}
navigationController?.pushViewController(cabinetVC, animated: true)
// 示例3: 复合类型 - 同时支持签到和智能柜 ⭐
let multiVC = YHScanViewController.create(types: [.checkIn, .smartCabinet]) { result in
print("扫码内容: \(result.code)")
// 根据识别出的类型进行不同处理
if let type = result.recognizedType {
if type == .checkIn {
print("这是签到码")
APIManager.checkIn(code: result.code)
} else if type == .smartCabinet {
print("这是智能柜码")
APIManager.openCabinet(code: result.code)
}
}
}
navigationController?.pushViewController(multiVC, animated: true)
// 示例4: 所有类型
let allTypesVC = YHScanViewController.create(types: .all) { result in
// 自动识别并处理
}
navigationController?.pushViewController(allTypesVC, animated: true)
// 示例5: 二维码格式示例
// 签到码格式: "CHECKIN_xxxxx"
// 智能柜码格式: "CABINET_xxxxx"
// 或 JSON 格式: {"type": "checkin", "data": "xxxxx"}
// 示例6: 后续扩展新类型
/*
struct YHScanType: OptionSet {
let rawValue: Int
static let checkIn = YHScanType(rawValue: 1 << 0)
static let smartCabinet = YHScanType(rawValue: 1 << 1)
static let document = YHScanType(rawValue: 1 << 2) // ← 新增
static let payment = YHScanType(rawValue: 1 << 3) // ← 新增
}
// 使用组合
let vc = YHScanViewController.create(types: [.checkIn, .document]) { result in
// 同时支持签到和文档扫码
}
*/
*/
//
// YHScanModel.swift
// galaxy
//
// Created by alexzzw on 2025/10/14.
// Copyright © 2025 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
// MARK: - 扫码场景类型(支持复合类型)
struct YHScanType: OptionSet {
let rawValue: Int
static let checkIn = YHScanType(rawValue: 1 << 0) // 签到
static let smartCabinet = YHScanType(rawValue: 1 << 1) // 智能柜
// 预留扩展位
// static let document = YHScanType(rawValue: 1 << 2) // 文档
// static let payment = YHScanType(rawValue: 1 << 3) // 支付
/// 所有类型
static let all: YHScanType = [.checkIn, .smartCabinet]
var title: String {
if self == .checkIn {
return "扫码签到"
} else if self == .smartCabinet {
return "智能柜扫码"
} else if self == .all {
return "扫码"
} else {
return "多功能扫码"
}
}
var instructionText: String? {
if self == .checkIn {
return "将二维码放到框内"
} else if self == .smartCabinet {
return "将二维码放到框内"
} else if contains(.checkIn) && contains(.smartCabinet) {
return "将二维码放到框内"
} else {
return "将二维码放到框内"
}
}
/// 从二维码内容识别类型
func recognizeType(from code: String) -> YHScanType? {
// 根据二维码内容规则识别类型
if contains(.checkIn) && code.hasPrefix("CHECKIN_") {
return .checkIn
}
if contains(.smartCabinet) && code.hasPrefix("CABINET_") {
return .smartCabinet
}
// 可以添加更多识别规则
// 示例: JSON 格式识别
if let data = code.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let type = json["type"] as? String {
switch type {
case "checkin":
return contains(.checkIn) ? .checkIn : nil
case "cabinet":
return contains(.smartCabinet) ? .smartCabinet : nil
default:
break
}
}
// 如果只有一个类型,直接返回
if self == .checkIn {
return .checkIn
} else if self == .smartCabinet {
return .smartCabinet
}
return nil
}
}
// MARK: - 扫码结果回调
typealias YHScanCompletion = (YHScanResult) -> Void
struct YHScanResult {
let code: String
let recognizedType: YHScanType? // 识别出的具体类型
let supportedTypes: YHScanType // 支持的类型集合
let timestamp: Date
init(code: String, recognizedType: YHScanType?, supportedTypes: YHScanType) {
self.code = code
self.recognizedType = recognizedType
self.supportedTypes = supportedTypes
timestamp = Date()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment