import SnapKit import UIKit /// 原样展示服务端小程序码图片,加载失败只重试图片,不重新发起订单确认。 @MainActor final class DroneDockConfirmationCodeCell: UICollectionViewCell { /// 请求重新下载同一张小程序码图片。 var onRetry: (() -> Void)? private let gradientLayer = CAGradientLayer() private let codeFrame = UIView() private let imageView = UIImageView() private let countLabel = DroneDockStyle.label(size: 12) private let stateLabel = DroneDockStyle.label(size: 13, color: DroneDockStyle.secondary) private let retryButton = UIButton(type: .system) /// 图片保留完整内容和留白,禁止裁剪成普通二维码样式。 override init(frame: CGRect) { super.init(frame: frame) contentView.backgroundColor = .white contentView.layer.cornerRadius = 17 contentView.layer.borderWidth = 1 contentView.layer.borderColor = UIColor(hex: 0xE1EAFB).cgColor contentView.clipsToBounds = true gradientLayer.colors = [UIColor(hex: 0xF1F5FF).cgColor, UIColor.white.cgColor] gradientLayer.startPoint = CGPoint(x: 0.5, y: 0) gradientLayer.endPoint = CGPoint(x: 0.5, y: 1) contentView.layer.insertSublayer(gradientLayer, at: 0) let status = UIView() status.backgroundColor = UIColor(hex: 0xEAF2FF) status.layer.cornerRadius = 13 status.layer.borderWidth = 1 status.layer.borderColor = UIColor(hex: 0xD9E8FF).cgColor let statusLabel = DroneDockStyle.label("等待游客确认", size: 11, color: UIColor(hex: 0x066BFF)) status.addSubview(statusLabel) statusLabel.snp.makeConstraints { $0.leading.trailing.equalToSuperview().inset(10); $0.centerY.equalToSuperview() } status.snp.makeConstraints { $0.height.equalTo(26) } let title = DroneDockStyle.label("请游客扫码确认收货", size: 20, weight: .semibold) let detail = DroneDockStyle.label("查看本订单全部成片后,一次确认", size: 12, color: DroneDockStyle.secondary) let explanation = DroneDockStyle.label("扫描不等于收货,需原下单游客主动确认", size: 10, color: DroneDockStyle.secondary) for label in [title, detail, countLabel, explanation, stateLabel] { label.numberOfLines = 0 label.textAlignment = .center } codeFrame.backgroundColor = .white codeFrame.layer.cornerRadius = 12 codeFrame.layer.borderWidth = 1 codeFrame.layer.borderColor = UIColor(hex: 0xE8EDF5).cgColor codeFrame.snp.makeConstraints { $0.size.equalTo(216).priority(999) } imageView.contentMode = .scaleAspectFit imageView.backgroundColor = .white imageView.isAccessibilityElement = true imageView.accessibilityIdentifier = "drone.confirmation.code.image" imageView.accessibilityLabel = "游客交付确认小程序码" codeFrame.addSubview(imageView) imageView.snp.makeConstraints { $0.edges.equalToSuperview().inset(10) } retryButton.setTitle("重新加载图片", for: .normal) retryButton.accessibilityIdentifier = "drone.confirmation.image.retry" retryButton.addTarget(self, action: #selector(retryTapped), for: .touchUpInside) retryButton.snp.makeConstraints { $0.height.equalTo(44).priority(999) } let stack = UIStackView(arrangedSubviews: [status, title, detail, codeFrame, stateLabel, retryButton, countLabel, explanation]) stack.axis = .vertical stack.alignment = .center stack.spacing = 12 stack.setCustomSpacing(9, after: status) stack.setCustomSpacing(4, after: title) stack.setCustomSpacing(20, after: detail) stack.setCustomSpacing(16, after: codeFrame) contentView.addSubview(stack) stack.snp.makeConstraints { $0.leading.trailing.equalToSuperview().inset(16) $0.top.equalToSuperview().offset(22) $0.bottom.equalToSuperview().inset(20) } for label in [title, detail, countLabel, explanation, stateLabel] { label.snp.makeConstraints { $0.width.equalTo(stack) } } accessibilityIdentifier = "drone.confirmation.code" } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { super.layoutSubviews() CATransaction.begin() CATransaction.setDisableActions(true) gradientLayer.frame = contentView.bounds CATransaction.commit() } override func prepareForReuse() { super.prepareForReuse(); onRetry = nil; clearImage() } /// 订单完成或身份失效时立即移除旧码,不等待 Diffable 回收单元格。 func clearImage() { imageView.image = nil imageView.isHidden = true codeFrame.isHidden = true stateLabel.text = nil stateLabel.isHidden = true retryButton.isHidden = true } /// 图片读取结果与请求状态分开,订单状态刷新不会清除已有图片。 func apply(image: UIImage?, error: String?, workCount: Int? = nil, pointCount: Int? = nil) { imageView.image = image imageView.isHidden = image == nil && error != nil codeFrame.isHidden = image == nil && error != nil if let workCount, let pointCount { countLabel.text = "云端 \(workCount) 个成片 · \(pointCount) 个打卡点" countLabel.isHidden = false } else { countLabel.text = nil countLabel.isHidden = true } stateLabel.text = error ?? (image == nil ? "正在加载确认码图片…" : nil) stateLabel.isHidden = image != nil && error == nil retryButton.isHidden = error == nil } @objc private func retryTapped() { onRetry?() } }