소스 검색

僵尸用户倒计时

zhujieshan 1 개월 전
부모
커밋
ce456599f4

+ 267 - 56
QuickSearchLocation/Classes/Common/Tool/QSLCountdownManager.swift

@@ -1,16 +1,69 @@
 //
-//  CountDownManager.swift
+//  QSLCountdownManager.swift
 //  QuickSearchLocation
 //
-//  Created by Destiny on 2025/9/24.
+//  Created by Trae AI on 2024-06-30.
 //
 
-import UIKit
+import Foundation
 
+/// 倒计时类型
+enum QSLCountdownType {
+    case homeProduct  // 首页商品倒计时
+    case trial        // 试用倒计时
+}
 
+/// 倒计时管理器代理协议
+protocol QSLCountdownManagerDelegate: AnyObject {
+    /// 倒计时更新
+    /// - Parameters:
+    ///   - manager: 倒计时管理器
+    ///   - type: 倒计时类型
+    ///   - remainingSeconds: 剩余秒数
+    func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int)
+    
+    /// 倒计时结束
+    /// - Parameters:
+    ///   - manager: 倒计时管理器
+    ///   - type: 倒计时类型
+    func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType)
+}
+
+// 使代理方法可选实现
+extension QSLCountdownManagerDelegate {
+    func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int) {}
+    func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType) {}
+}
+
+/// 倒计时管理器
 class QSLCountdownManager {
+    
+    // MARK: - 单例
     static let shared = QSLCountdownManager()
+    private init() {}
+    
+    // MARK: - 属性
     private var timer: Timer?
+    private var isHomeProductCountdownActive = false
+    private var isTrialCountdownActive = false
+    
+    // 倒计时剩余时间(秒)
+    private var homeProductRemainingSeconds: Int = 0
+    private var trialRemainingSeconds: Int = 0
+    
+    // 代理集合
+    private var delegates = NSHashTable<AnyObject>.weakObjects()
+    
+    // 倒计时回调(保留原有回调机制作为备选)
+    typealias CountdownCallback = (Int) -> Void
+    private var homeProductCallback: CountdownCallback?
+    private var trialCallback: CountdownCallback?
+    
+    // 倒计时结束回调(保留原有回调机制作为备选)
+    typealias CountdownFinishCallback = () -> Void
+    private var homeProductFinishCallback: CountdownFinishCallback?
+    private var trialFinishCallback: CountdownFinishCallback?
+    
     var selectGood: QSLGoodModel?{
         didSet{
             if(oldValue != nil){
@@ -35,78 +88,236 @@ class QSLCountdownManager {
         }
     }
     
-    var remainingSeconds: Int = 900 * 1000 // 15分钟 = 900秒  (正常套餐活动倒计时)
-    var trialSeconds: Int = -999 // 15分钟 = 900秒  (试用活动倒计时)
-    var updateHandler: ((String) -> Void)?
-    var updateHandler1: ((String) -> Void)?
-    var updateTrialHandler: ((String) -> Void)?
-    
-    var finishHandler: (() -> Void)?
+    // MARK: - 代理管理
     
-    func startCountdown() {
-        timer?.invalidate()
-        timer = nil
-        
-        remainingSeconds = 900 * 1000
-        
-        timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
-        RunLoop.current.add(timer!, forMode: .common)
+    /// 添加代理
+    /// - Parameter delegate: 代理对象
+    func addDelegate(_ delegate: QSLCountdownManagerDelegate) {
+        delegates.add(delegate)
     }
     
-    func checkIsCountDown() -> Bool{
-        if let curTimer = timer {
-            return true
-        }else{
-            return false
-        }
+    /// 移除代理
+    /// - Parameter delegate: 代理对象
+    func removeDelegate(_ delegate: QSLCountdownManagerDelegate) {
+        delegates.remove(delegate)
     }
     
-    func stopCountdown() {
-        finishHandler?()
-        timer?.invalidate()
-        timer = nil
+    /// 移除所有代理
+    func removeAllDelegates() {
+        delegates.removeAllObjects()
     }
     
-    @objc private func updateCountdown() {
+    // MARK: - 公共方法
+    
+    /// 开始倒计时
+    /// - Parameters:
+    ///   - type: 倒计时类型
+    ///   - seconds: 倒计时秒数
+    ///   - callback: 倒计时回调,返回剩余秒数
+    ///   - finishCallback: 倒计时结束回调
+    /// - Returns: 是否成功开启倒计时
+    @discardableResult
+    func startCountdown(type: QSLCountdownType, seconds: Int, callback: @escaping CountdownCallback, finishCallback: CountdownFinishCallback? = nil) -> Bool {
+        // 检查该类型倒计时是否已结束,如果已结束则不能再开启
         
+        switch type {
+        case .homeProduct:
+            if isHomeProductCountdownActive {
+                return false
+            }
+            homeProductRemainingSeconds = seconds
+            homeProductCallback = callback
+            homeProductFinishCallback = finishCallback
+            isHomeProductCountdownActive = true
+        case .trial:
+            if isTrialCountdownActive {
+                return false
+            }
+            trialRemainingSeconds = seconds
+            trialCallback = callback
+            trialFinishCallback = finishCallback
+            isTrialCountdownActive = true
+        }
         
-        remainingSeconds -= 1
-        trialSeconds -= 1
+        // 如果timer不存在,创建timer
+        if timer == nil {
+            timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
+            RunLoop.current.add(timer!, forMode: .common)
+        }
+        
+        // 立即执行一次回调,更新UI
+        executeCallback(for: type)
+        
+        return true
+    }
+    
+    /// 暂停倒计时
+    /// - Parameter type: 倒计时类型
+    func pauseCountdown(type: QSLCountdownType) {
+        switch type {
+        case .homeProduct:
+            isHomeProductCountdownActive = false
+        case .trial:
+            isTrialCountdownActive = false
+        }
+        
+        // 如果两种倒计时都不活跃,则销毁timer
+        if !isHomeProductCountdownActive && !isTrialCountdownActive {
+            invalidateTimer()
+        }
+    }
+    
+    /// 恢复倒计时
+    /// - Parameter type: 倒计时类型
+    /// - Returns: 是否成功恢复倒计时
+    @discardableResult
+    func resumeCountdown(type: QSLCountdownType) -> Bool {
+        switch type {
+        case .homeProduct:
+            if homeProductRemainingSeconds <= 0 {
+                return false
+            }
+            isHomeProductCountdownActive = true
+        case .trial:
+            if trialRemainingSeconds <= 0 {
+                return false
+            }
+            isTrialCountdownActive = true
+        }
         
+        // 如果timer不存在,创建timer
+        if timer == nil {
+            timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
+            RunLoop.current.add(timer!, forMode: .common)
+        }
         
-        if remainingSeconds <= 0 && trialSeconds <= 0{
-            stopCountdown()
+        return true
+    }
+    
+    /// 停止倒计时
+    /// - Parameter type: 倒计时类型
+    func stopCountdown(type: QSLCountdownType) {
+        switch type {
+        case .homeProduct:
+            isHomeProductCountdownActive = false
+            homeProductRemainingSeconds = 0
+            homeProductCallback = nil
+            homeProductFinishCallback = nil
+        case .trial:
+            isTrialCountdownActive = false
+            trialRemainingSeconds = 0
+            trialCallback = nil
+            trialFinishCallback = nil
         }
         
-        if remainingSeconds > 0 {
-            let totalSeconds = remainingSeconds / 1000
-            let minutes = totalSeconds / 60
-            let seconds = totalSeconds % 60
-            let milliseconds = remainingSeconds % 1000
+        // 如果两种倒计时都不活跃,则销毁timer
+        if !isHomeProductCountdownActive && !isTrialCountdownActive {
+            invalidateTimer()
+        }
+    }
+    
+    /// 获取剩余时间
+    /// - Parameter type: 倒计时类型
+    /// - Returns: 剩余秒数
+    func getRemainingSeconds(for type: QSLCountdownType) -> Int {
+        switch type {
+        case .homeProduct:
+            return homeProductRemainingSeconds
+        case .trial:
+            return trialRemainingSeconds
+        }
+    }
+    
+    /// 是否正在倒计时
+    /// - Parameter type: 倒计时类型
+    /// - Returns: 是否正在倒计时
+    func isCountdownActive(for type: QSLCountdownType) -> Bool {
+        switch type {
+        case .homeProduct:
+            return isHomeProductCountdownActive
+        case .trial:
+            return isTrialCountdownActive
+        }
+    }
+    
+    // MARK: - 私有方法
+    
+    /// 更新倒计时
+    @objc private func updateCountdown() {
+        // 更新首页商品倒计时
+        if isHomeProductCountdownActive {
+            if homeProductRemainingSeconds > 0 {
+                homeProductRemainingSeconds -= 1
+                executeCallback(for: .homeProduct)
+                notifyDelegatesCountdownUpdated(type: .homeProduct, remainingSeconds: homeProductRemainingSeconds)
+            }
             
-            // 格式:MM:SS:SSS(例如 "14:59:500")
-            let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
-            updateHandler?(timeString)
-            updateHandler1?(timeString)
-        }else{
-            remainingSeconds = 0
+            // 检查是否结束
+            if homeProductRemainingSeconds <= 0 {
+                isHomeProductCountdownActive = false
+                homeProductFinishCallback?()
+                homeProductCallback = nil
+                homeProductFinishCallback = nil
+                notifyDelegatesCountdownFinished(type: .homeProduct)
+            }
         }
         
-        if trialSeconds > 0 {
-            let totalSeconds = trialSeconds / 1000
-            let minutes = totalSeconds / 60
-            let seconds = totalSeconds % 60
-            let milliseconds = trialSeconds % 1000
+        // 更新试用倒计时
+        if isTrialCountdownActive {
+            if trialRemainingSeconds > 0 {
+                trialRemainingSeconds -= 1
+                executeCallback(for: .trial)
+                notifyDelegatesCountdownUpdated(type: .trial, remainingSeconds: trialRemainingSeconds)
+            }
             
-            // 格式:MM:SS:SSS(例如 "14:59:500")
-            let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
-            updateTrialHandler?(timeString)
-        }else{
-            trialSeconds = 0
+            // 检查是否结束
+            if trialRemainingSeconds <= 0 {
+                isTrialCountdownActive = false
+                trialFinishCallback?()
+                trialCallback = nil
+                trialFinishCallback = nil
+                notifyDelegatesCountdownFinished(type: .trial)
+            }
+        }
+        
+        // 如果两种倒计时都不活跃,则销毁timer
+        if !isHomeProductCountdownActive && !isTrialCountdownActive {
+            invalidateTimer()
+        }
+    }
+    
+    /// 执行倒计时回调
+    /// - Parameter type: 倒计时类型
+    private func executeCallback(for type: QSLCountdownType) {
+        switch type {
+        case .homeProduct:
+            homeProductCallback?(homeProductRemainingSeconds)
+        case .trial:
+            trialCallback?(trialRemainingSeconds)
         }
     }
     
-    deinit {
-        stopCountdown()
+    /// 通知代理倒计时更新
+    /// - Parameters:
+    ///   - type: 倒计时类型
+    ///   - remainingSeconds: 剩余秒数
+    private func notifyDelegatesCountdownUpdated(type: QSLCountdownType, remainingSeconds: Int) {
+        for case let delegate as QSLCountdownManagerDelegate in delegates.allObjects {
+            delegate.countdownManager(self, didUpdateCountdown: type, remainingSeconds: remainingSeconds)
+        }
+    }
+    
+    /// 通知代理倒计时结束
+    /// - Parameter type: 倒计时类型
+    private func notifyDelegatesCountdownFinished(type: QSLCountdownType) {
+        for case let delegate as QSLCountdownManagerDelegate in delegates.allObjects {
+            delegate.countdownManager(self, didFinishCountdown: type)
+        }
+    }
+    
+    /// 销毁定时器
+    private func invalidateTimer() {
+        timer?.invalidate()
+        timer = nil
     }
 }

+ 0 - 1
QuickSearchLocation/Classes/Common/Tool/QSLTools.swift

@@ -12,7 +12,6 @@ class QSLTools: NSObject {
     static let shared = QSLTools()
     var isOpen = false
     var urlScheme = ""
-    private override init() {}
 }
 
 extension QSLTools {

+ 0 - 8
QuickSearchLocation/Classes/Main/QSLBaseManager.swift

@@ -72,14 +72,6 @@ extension QSLBaseManager {
             )
         } else {
             self.userModel.vipEndTimestamp = nil
-            if let selectgood = QSLCountdownManager.shared.selectGood{
-                NotificationCenter.default.post(
-                    name: Notification.Name("QSLHomeUpdateCouponViewNoti"),
-                    object: nil,
-                    userInfo: ["showCoupon": true]
-                )
-            }
-            
         }
         
     }

+ 2 - 1
QuickSearchLocation/Classes/Pages/QSLHome/Controller/QSLHomeController.swift

@@ -223,6 +223,7 @@ class QSLHomeController: QSLBaseController {
         
         let imgView = UIImageView.init(image: UIImage(named: "home_find_friend"))
         imgView.frame = CGRectMake(0, 0, 34.rpx, 40.rpx)
+        imgView.isUserInteractionEnabled = true
         _homeResortBtn.addSubview(imgView)
         
         let label = YYLabel.init(frame: CGRectMake(0, 29.rpx, 34.rpx, 15.rpx))
@@ -243,7 +244,7 @@ class QSLHomeController: QSLBaseController {
         label.textAlignment = .center
         _homeResortBtn.addSubview(label)
         
-        let tapG = UIGestureRecognizer.init(target: self, action: #selector(homeFriendBtnAction))
+        let tapG = UITapGestureRecognizer.init(target: self, action: #selector(homeFriendBtnAction))
         _homeResortBtn.addGestureRecognizer(tapG)
         
         return _homeResortBtn

+ 128 - 16
QuickSearchLocation/Classes/Pages/QSLHome/View/QSLHomeFriendView.swift

@@ -54,6 +54,8 @@ class QSLHomeFriendView: UIView {
     /// 滑动底部距离顶部的距离
     var scrollBottomHeight: CGFloat?
     
+    var countDownType = 0
+    
     lazy var friMapLogoImageView: UIImageView = {
        
         let imageView = UIImageView()
@@ -170,7 +172,7 @@ class QSLHomeFriendView: UIView {
     
     override init(frame: CGRect) {
         super.init(frame: frame)
-        
+        QSLCountdownManager.shared.addDelegate(self)
         self.scrollCenterHeight = self.qsl_top
         self.scrollBottomHeight = qsl_kScreenH - qsl_kTabbarFrameH - 56 - 40
         self.setupUI()
@@ -331,15 +333,76 @@ extension QSLHomeFriendView: QSLHomeFriendTableViewCellDelegate {
     @objc private func handleCouponNotification(_ notification: Notification) {
         guard let shouldShow = notification.userInfo?["showCoupon"] as? Bool else { return }
         
-        updateCouponViewVisibility(show: shouldShow)
+        if ((notification.userInfo?["couponType"]) != nil){
+            updateCouponViewVisibility(show: shouldShow, showType: 1)
+        }else{
+            updateCouponViewVisibility(show: shouldShow)
+        }
+        
     }
     
     @objc private func refreshCouponNotification(_ notification: Notification) {
         refreshCouponView()
     }
     
-    func refreshCouponView(){
+    func refreshCouponView(type: Int = 0){
+
+        if(type == 1){
+            
+            if let model = QSLCountdownManager.shared.trialGood {
+                
+                let attr = NSMutableAttributedString()
+                
+                let firstText = "您有 "
+                let firstAttr = NSMutableAttributedString(string: firstText)
+                firstAttr.yy_font = UIFont.textM(10)
+                firstAttr.yy_color = UIColor.white
+                attr.append(firstAttr)
+                
+                let secondText = "免费专属试用"
+                let secondAttr = NSMutableAttributedString(string: secondText)
+                secondAttr.yy_font = UIFont.textB(14)
+                secondAttr.yy_color = UIColor.hexStringColor(hexString: "#E1E3A3")
+                attr.append(secondAttr)
+                
+                let thirdAttr = NSMutableAttributedString(string: " 福利未领取")
+                thirdAttr.yy_font = UIFont.textM(10)
+                thirdAttr.yy_color = UIColor.white
+                attr.append(thirdAttr)
+                
+                self.couponLabel.attributedText = attr
+                
+                
+                if(countDownType == 1){
+                    return
+                }
+                
+                couponView.image = UIImage(named: "home_trial_bg")
+                
+                couponLabel.snp.updateConstraints { make in
+                    make.left.equalTo(20.rpx)
+                }
+                
+                countdownLabel.snp.updateConstraints { make in
+                    make.left.equalTo(20.rpx)
+                }
+                
+                countDownType = 1
+                
+                // 开始首页商品倒计时
+                QSLCountdownManager.shared.startCountdown(type: .trial, seconds: 900000) { remainingSeconds in
+                   
+                } finishCallback: {
+                   
+                }
+                
+            }
+            return
+            
+        }
+        
         if let model = QSLCountdownManager.shared.selectGood {
+            
             let amount : Int = Int((model.originalAmount - model.amount) / 100)
             
             let attr = NSMutableAttributedString()
@@ -360,34 +423,39 @@ extension QSLHomeFriendView: QSLHomeFriendTableViewCellDelegate {
             
             self.couponLabel.attributedText = attr
             
-            if(self.countdownLabel.label.text != nil){
+            if(countDownType == 0){
                 return
             }
             
-            QSLCountdownManager.shared.updateHandler = { [weak self] timeString in
-                if let curVC = self?.rootViewController(){
-                    if(curVC.isKind(of: QSLHomeController.self)){
-                        self?.countdownLabel.updateCountdownText(timeString)
-                    }
-                }
+            countDownType = 0
+            
+            couponLabel.snp.updateConstraints { make in
+                make.left.equalTo(60.rpx)
             }
-            QSLCountdownManager.shared.finishHandler  = { [weak self] in
-                self?.updateCouponViewVisibility(show: false)
+            
+            countdownLabel.snp.updateConstraints { make in
+                make.left.equalTo(60.rpx)
             }
-            QSLCountdownManager.shared.startCountdown()
             
+            QSLCountdownManager.shared.startCountdown(type: .homeProduct, seconds: 900000) {[weak self] remainingSeconds in
+               
+            } finishCallback: {
+               
+            }
+
+            couponView.image = UIImage(named: "home_activity_bg")
         }
     }
     
-    private func updateCouponViewVisibility(show: Bool) {
+    private func updateCouponViewVisibility(show: Bool, showType:Int = 0) {
         if(couponView.isHidden == !show){
-            self.refreshCouponView()
+            self.refreshCouponView(type: showType)
             return
         }
         couponView.isHidden = !show
         
         if (show) {
-            self.refreshCouponView()
+            self.refreshCouponView(type: showType)
             
             couponViewTopConstraint?.update(offset: 8.rpx)
             friBgViewTopConstraint?.update(offset: 8.rpx) // 修改这里
@@ -637,3 +705,47 @@ extension QSLHomeFriendView: UITableViewDelegate, UITableViewDataSource {
     }
     
 }
+
+extension QSLHomeFriendView : QSLCountdownManagerDelegate{
+    func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int) {
+        // 处理倒计时更新
+        if type == .homeProduct && self.countDownType == 0{
+            let totalSeconds = remainingSeconds / 1000
+            let minutes = totalSeconds / 60
+            let seconds = totalSeconds % 60
+            var milliseconds = remainingSeconds % 1000
+            if(milliseconds <= 1){
+                milliseconds = 0
+            }
+            // 格式:MM:SS:SSS(例如 "14:59:500")
+            let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
+            self.countdownLabel.updateCountdownText(timeString)
+        }
+        
+        if type == .trial && self.countDownType == 1{
+            
+            let totalSeconds = remainingSeconds / 1000
+            let minutes = totalSeconds / 60
+            let seconds = totalSeconds % 60
+            var milliseconds = remainingSeconds % 1000
+            if(milliseconds <= 1){
+                milliseconds = 0
+            }
+            // 格式:MM:SS:SSS(例如 "14:59:500")
+            let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
+            self.countdownLabel.updateCountdownText(timeString)
+            
+        }
+    }
+    
+    func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType) {
+        // 处理倒计时结束
+        if type == .homeProduct && self.countDownType == 0{
+            self.updateCouponViewVisibility(show: false)
+        }
+        
+        if type == .trial && self.countDownType == 1{
+            self.updateCouponViewVisibility(show: false)
+        }
+    }
+}

+ 42 - 6
QuickSearchLocation/Classes/Pages/QSLVip/Controller/QSLActivityVipVC.swift

@@ -20,7 +20,7 @@ class QSLActivityVipVC: QSLBaseController {
         super.viewDidLoad()
         
         QSEventHandle.eventPush(eventName: QSLGravityConst.activity_vip_show)
-        
+        QSLCountdownManager.shared.addDelegate(self)
         self.initializeView()
         
         self.requestNetwork()
@@ -34,6 +34,10 @@ class QSLActivityVipVC: QSLBaseController {
 //        }
     }
     
+    deinit {
+        QSLCountdownManager.shared.removeDelegate(self)
+    }
+    
     @objc func privacyAction() {
         
         let vc = QSLWebViewController()
@@ -161,6 +165,16 @@ class QSLActivityVipVC: QSLBaseController {
         }
     }
     
+    func startTimer(){
+        // 开始首页商品倒计时
+        QSLCountdownManager.shared.startCountdown(type: .homeProduct, seconds: 900000) {[weak self] remainingSeconds in
+           
+        } finishCallback: {
+           
+        }
+
+    }
+    
     @objc func requestNetwork() {
         // 定义并赋值 itemListDict(根据条件变化)
         let itemListDict: [String: Any] = ["itemListType": 2]
@@ -191,11 +205,8 @@ class QSLActivityVipVC: QSLBaseController {
                 self.scrollView.contentSize = CGSize(width: 0.0, height: 415.rpx+CGFloat(height)+100+QSLConst.qsl_kTabbarBottom/2+12)
                 
                 QSLCountdownManager.shared.selectGood = self.goodList[0]
-
-                QSLCountdownManager.shared.updateHandler1 = { [weak self] timeString in
-                    self?.countdownLabel.updateCountdownText(timeString)
-                }
-                QSLCountdownManager.shared.startCountdown()
+                
+                self.startTimer()
 
                 NotificationCenter.default.post(
                     name: Notification.Name("QSLHomeUpdateCouponViewNoti"),
@@ -771,3 +782,28 @@ extension QSLActivityVipVC: UICollectionViewDelegate, UICollectionViewDataSource
     }
 
 }
+
+extension QSLActivityVipVC : QSLCountdownManagerDelegate{
+    func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int) {
+        // 处理倒计时更新
+        if type == .homeProduct {
+            let totalSeconds = remainingSeconds / 1000
+            let minutes = totalSeconds / 60
+            let seconds = totalSeconds % 60
+            var milliseconds = remainingSeconds % 1000
+            if(milliseconds <= 1){
+                milliseconds = 0
+            }
+            // 格式:MM:SS:SSS(例如 "14:59:500")
+            let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
+            self.countdownLabel.updateCountdownText(timeString)
+        }
+    }
+    
+    func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType) {
+        // 处理倒计时结束
+        if type == .homeProduct {
+            
+        }
+    }
+}

+ 1 - 0
QuickSearchLocation/Classes/Pages/QSLVip/Controller/QSLVipController.swift

@@ -785,6 +785,7 @@ extension QSLVipController {
                 self.goodList[0].isSelect = true
                 self.selectGood = self.goodList[0]
                 QSLCountdownManager.shared.selectGood = self.goodList[0]
+
                 self.mostCell.config(model: self.goodList[0])
         
                 let row = ceil(Double(self.goodList.count - 1) / 3.0)

+ 56 - 13
QuickSearchLocation/Classes/Pages/QSLVip/Controller/QSLVipTrialVC.swift

@@ -22,13 +22,25 @@ class QSLVipTrialVC: QSLBaseController {
     
     override func viewDidLoad() {
         super.viewDidLoad()
+        QSLCountdownManager.shared.addDelegate(self)
         setUI()
         requestNetwork()
     }
     
+    func startTimer(){
+        // 开始首页商品倒计时
+        QSLCountdownManager.shared.startCountdown(type: .trial, seconds: 900000) {[weak self] remainingSeconds in
+           
+        } finishCallback: {
+           
+        }
+    }
+    
+    //倒计时
+    //
     func requestNetwork() {
         // 网络请求时使用 itemListDict 作为参数(关键修复)
-        QSLNetwork().request(.wakeupTrialList(dict: [String: Any]())) {[weak self] response in
+        QSLNetwork().request(.wakeupTrialList(dict: ["itemListType": 2])) {[weak self] response in
             // 后续逻辑不变...
             let list = response.mapArray(QSLGoodModel.self, modelKey: "data>list")
  
@@ -37,20 +49,29 @@ class QSLVipTrialVC: QSLBaseController {
                 
                 self?.desLabel.text = self?.selectGood?.content ?? ""
                 
-                //初始化
-                if(QSLCountdownManager.shared.trialSeconds == -999){
-                    QSLCountdownManager.shared.trialSeconds = 900 * 1000
-                }
-                QSLCountdownManager.shared.updateTrialHandler = { [weak self] timeString in
-                    self?.countdownLabel.updateCountdownText(timeString)
+                if(QSLCountdownManager.shared.trialGood == nil){
+                    QSLCountdownManager.shared.trialGood = list[0]
+                    self?.startTimer()
+                    NotificationCenter.default.post(
+                        name: Notification.Name("QSLHomeUpdateCouponViewNoti"),
+                        object: nil,
+                        userInfo: ["showCoupon": true, "couponType":"1"]
+                    )
+                }else{
+                    QSLCountdownManager.shared.trialGood = list[0]
+                    if(!QSLCountdownManager.shared.isCountdownActive(for: .trial)){
+                        self?.countdownLabel.updateCountdownText("00 : 00 : 00")
+                    }else{
+                        NotificationCenter.default.post(
+                            name: Notification.Name("QSLHomeUpdateCouponViewNoti"),
+                            object: nil,
+                            userInfo: ["showCoupon": true, "couponType":"1"]
+                        )
+                    }
+
                 }
-                QSLCountdownManager.shared.startCountdown()
+                
 
-                NotificationCenter.default.post(
-                    name: Notification.Name("QSLHomeUpdateCouponViewNoti"),
-                    object: nil,
-                    userInfo: ["showCoupon": true, "couponType":"1"]
-                )
             }
         } fail: { code, error in
             self.view.toast(text: "加载商品列表失败")
@@ -468,3 +489,25 @@ class QSLVipTrialVC: QSLBaseController {
         return label
     }()
 }
+
+extension QSLVipTrialVC : QSLCountdownManagerDelegate{
+    func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int) {
+        // 处理倒计时更新
+        if type == .trial {
+            let totalSeconds = remainingSeconds / 1000
+            let minutes = totalSeconds / 60
+            let seconds = totalSeconds % 60
+            var milliseconds = remainingSeconds % 1000
+            if(milliseconds <= 1){
+                milliseconds = 0
+            }
+            // 格式:MM:SS:SSS(例如 "14:59:500")
+            let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
+            self.countdownLabel.updateCountdownText(timeString)
+        }
+    }
+    
+    func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType) {
+
+    }
+}

+ 22 - 0
QuickSearchLocation/Resources/Assets.xcassets/Home/home_trial_bg.imageset/Contents.json

@@ -0,0 +1,22 @@
+{
+  "images" : [
+    {
+      "idiom" : "universal",
+      "scale" : "1x"
+    },
+    {
+      "filename" : "顶部卡片.png",
+      "idiom" : "universal",
+      "scale" : "2x"
+    },
+    {
+      "filename" : "顶部卡片 (1).png",
+      "idiom" : "universal",
+      "scale" : "3x"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

BIN
QuickSearchLocation/Resources/Assets.xcassets/Home/home_trial_bg.imageset/顶部卡片 (1).png


BIN
QuickSearchLocation/Resources/Assets.xcassets/Home/home_trial_bg.imageset/顶部卡片.png