fix
杨锴
2025-04-16 f646fbebad928833d15546a36e37768d00fe6173
WanPai/Root/Home/VC/HomeVC.swift
@@ -22,9 +22,16 @@
            private var layout:FlowLayout!
            @IBOutlet weak var label_store: UILabel!
            private var items = Array<Any>()
    private var items_1 = [InnerItemModel]()
            private var storeModel:HomeStoreModel?
            private var storeId:Int?
            private var currentLocal:CLLocation?
    struct InnerItemModel{
        var contents = [Any]()
        var displayType = 2 // 1:横屏 2:竖屏
    }
            override func viewDidAppear(_ animated: Bool) {
                        super.viewDidAppear(animated)
@@ -67,7 +74,6 @@
                        view.backgroundColor = UIColor(hexStr: "EEF0F3",darkStr: nil)
                        layout = FlowLayout()
                        layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34)
                        collectionView.collectionViewLayout = layout
                        collectionView.delegate = self
                        collectionView.dataSource = self
@@ -125,7 +131,6 @@
            private func downloadImgs(){
                        let group = DispatchGroup()
                        let queue = DispatchQueue(label: "imgRadio")
                        let w = (view.frame.width - 78.0) / 2.0
                        for item in items{
                                    if let m = item as? HomeStoreConfigModel{
                                                queue.async(group: group){ () in
@@ -134,23 +139,27 @@
                                                                        SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
                                                                        } completed: { image, data, error, status in
                            m.imageSize = image?.size ?? .zero
                                                                                    guard !m.backgroundImage.isEmpty else {
                                                                                                m.cellHeight = 100
                                if image?.size.width ?? 0 > image?.size.height ?? 0{
                                    m.displayType = 1
                                }else{
                                    m.displayType = 2
                                }
                                                                                                group.leave()
                                                                                                return
                                                                                    }
                                                                                    if let img = image{
                                                                                                m.cellHeight = w / (img.size.width / img.size.height)
                                                                                    }else{
                                                                                                m.cellHeight =  w / (m.type.defaultImg.size.width / m.type.defaultImg.size.height)
                                                                                    }
                                                                                    group.leave()
                                                                        }
                                                                        return
                                                            }
                                                            if let data = SDImageCache.shared.diskCache.data(forKey: m.backgroundImage),let img = UIImage(data: data){
                                                                        m.cellHeight = w / (img.size.width / img.size.height)
                        m.imageSize = img.size
                        if img.size.width > img.size.height{
                            m.displayType = 1
                        }else{
                            m.displayType = 2
                        }
                                                                        group.leave()
                                                            }
                                                }
@@ -162,25 +171,34 @@
                                                            guard SDImageCache.shared.diskImageDataExists(withKey: m.backgroundImage) else {
                                                                        SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
                                                                        } completed: { image, data, error, status in
                                                                                    if let img = image{
                                                                                                m.cellHeight = w / (img.size.width / img.size.height)
                                                                                    }else{
                                                                                                m.cellHeight = 100
                                                                                    }
                            if let img = image{
                                m.imageSize = img.size
                                if img.size.width > img.size.height{
                                    m.displayType = 1
                                }else{
                                    m.displayType = 2
                                }
                            }
                                                                                    group.leave()
                                                                        }
                                                                        return
                                                            }
                                                            if let data = SDImageCache.shared.diskCache.data(forKey: m.backgroundImage),let img = UIImage(data: data){
                                                                        m.cellHeight = w / (img.size.width / img.size.height)
                        m.imageSize = img.size
                        if img.size.width > img.size.height{
                            m.displayType = 1
                        }else{
                            m.displayType = 2
                        }
                                                                        group.leave()
                                                            }
                                                }
                                    }
                        }
                        group.notify(queue: .main) {
                        group.notify(queue: .main) {[unowned self] in
            self.items_1.removeAll()
                                    self.items = self.items.sorted { v1, v2 in
                                                var sort1 = 0
@@ -204,7 +222,7 @@
                                                return sort1 < sort2
                                    }
                                    let v = self.items.compactMap { any in
            _ = self.items.compactMap { any in
                                                if let m = any as? HomeStoreTopBannerModel{
                                                            return "\(m.sort)"
                                                }
@@ -213,6 +231,33 @@
                                                }
                                                return ""
                                    }
            var displayItems_row = [Any]() //竖
            for v in self.items{
                var displayT:Int = 0
                if let m =  v as? HomeStoreTopBannerModel{
                    displayT = m.displayType
                }
                if let m =  v as? HomeStoreConfigModel{
                    displayT = m.displayType
                }
                if displayT == 1{ //横
                    if !displayItems_row.isEmpty{ //添加竖,清除竖
                        self.items_1.append(InnerItemModel(contents: displayItems_row, displayType: 2))
                        displayItems_row.removeAll()
                    }
                    self.items_1.append(InnerItemModel(contents: [v], displayType: displayT))
                }else {
                    displayItems_row.append(v)
                }
            }
            self.items_1.append(InnerItemModel(contents: displayItems_row, displayType: 2))
                                    self.layout.collectionView?.reloadData()
                                    self.collectionView.reloadData()
@@ -287,8 +332,16 @@
extension HomeVC:UICollectionViewDelegate,UICollectionViewDelegateWaterFlowLayout{
            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, colum section: Int) -> Int {
                        return 2
        if items_1[section].displayType == 1{
            return 1
        }else{
            return 2
        }
            }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return items_1.count
    }
            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacing section: Int) -> CGFloat {
                        return 10
@@ -298,17 +351,29 @@
                        return UIEdgeInsets(top: 0, left: 34, bottom: 14, right: 34)
            }
            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, itemWidth: CGFloat, caculateHeight indexPath: IndexPath) -> CGFloat {
                        let item = items[indexPath.row]
                        if let m = item as? HomeStoreConfigModel{
                                    return m.cellHeight
                        }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, itemWidth: CGFloat, caculateHeight indexPath: IndexPath) -> CGFloat {
                        if let m = item as? HomeStoreTopBannerModel{
                                    return m.cellHeight
                        }
                        return 200
            }
        let v = items_1[indexPath.section]
        let item = v.contents[indexPath.row]
        if let m = item as? HomeStoreConfigModel{
            if m.displayType == 2{
                return (JQ_ScreenW - 78) / 2 / m.imageSize.aspectRatio
            }
            return (JQ_ScreenW - 78) / m.imageSize.aspectRatio
        }
        if let m = item as? HomeStoreTopBannerModel{
            if m.displayType == 2{
                return (JQ_ScreenW - 78) / 2 / m.imageSize.aspectRatio
            }
            return (JQ_ScreenW - 78) / m.imageSize.aspectRatio
        }
        return 0
    }
            func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
@@ -362,6 +427,9 @@
                                                            }else{
                                                                        alertError(msg: "当前门店获取失败")
                                                            }
                case .huimin:
                    let vc = CardCenterVC()
                    push(vc: vc)
                                                case .nothing:
                                                            break
                                    }
@@ -377,8 +445,9 @@
extension HomeVC:UICollectionViewDataSource{
            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeCCell", for: indexPath) as! HomeCCell
                        guard indexPath.row < items.count else {return cell}
                        let item = items[indexPath.row]
        let item_1 = items_1[indexPath.section]
        let item = item_1.contents[indexPath.row]
                        if let m = item as? HomeStoreConfigModel{
                                    cell.img.sd_setImage(with: URL(string: m.backgroundImage), placeholderImage: nil, options: .lowPriority)
@@ -387,12 +456,14 @@
                        if let m = item as? HomeStoreTopBannerModel{
                                    cell.img.sd_setImage(with: URL(string: m.backgroundImage), placeholderImage: nil, options: .lowPriority)
                        }
                        cell.img.cornerRadius = 10
                        cell.cornerRadius = 10
        cell.layer.masksToBounds = true
                        return cell
            }
            func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                        return items.count
        return items_1[section].contents.count
            }
}