CNLiveShortVideoViewModel.swift 3.11 KB
//
//  CNLiveShortVideoViewModel.swift
//  metaCode
//
//  Created by open on 2024/1/8.
//

import Foundation
import CNLiveRequestBastKit

typealias resultShortVideoBlock = (_ result: Any,_ respStatue :CNLiveMineResponseType) -> Void
class CNLiveShortVideoViewModel: NSObject {
    
    /// 这是测试请求短视频数据
    static func requstWithShortVideo(reultShortVideoBlock: @escaping resultShortVideoBlock) {
        
        let param = NSMutableDictionary()
        param.setValue("10511059", forKey: "creatorId")
        param.setValue(UserInfoManager.shared.userInfo.uid, forKey: "userId")
        param.setValue("3", forKey: "type")
        param.setValue("1", forKey: "page")
        param.setValue("10", forKey: "pageSize")
        let custom_param = NSMutableDictionary()
        custom_param.setValue(APPId_wjj, forKey: "appId")
        custom_param.setValue(APPKey_wjj, forKey: "appKey")
        CNLiveNetworking.setAllowRequestDefaultArgument(true)
        CNLiveNetworking.setupShowDataResult(false)
        CNLiveNetworking.setupDSRequest(true)
        CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
        CNLiveNetworking.requestNetwork(with: .GET, urlString: APPBaseCMSClassUrl+"/ugcApi/ugcVideoList", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
            hiddenLoading()
            if (error == nil) {
                let resp = result as! NSDictionary
                let errorCode = "\(resp["errorCode"] ?? "0")"
                if (errorCode == "0") {
                    if resp["data"] is NSNull {
                        reultShortVideoBlock(NSNull(), .failed)
                    } else {
                        let resp_dict = resp["data"] as! NSDictionary
                        let array = resp_dict["list"] as! NSArray
                        let tempArray = NSMutableArray()
                        let tempUrlArray = NSMutableArray()
                        var index = 0
                        for item in array {
                            let listModel = dictionaryToModel(item as! [String : Any], ShortVideoHomeTestModel.self,"") as! ShortVideoHomeTestModel
                            if (index > 2) {
                                listModel.isMoney = true
                            }
                            tempArray.add(listModel)
                            tempUrlArray.add(listModel.videoUrl! as String)
                            index += 1
                        }
                    reultShortVideoBlock([tempArray, tempUrlArray], .success)
                    }
                } else {
                    showMessage(message: resp["errorMessage"] as! String)
                    reultShortVideoBlock(NSNull(),.failed)
                }
            } else {
                if CNLiveNetworking.isNetworking() {
                    showMessage(message: "请求失败")
                    reultShortVideoBlock(NSNull(),.failed)
                } else {
                    showMessage(message: "似乎与网络断开链接")
                    reultShortVideoBlock(NSNull(),.noNetwork)
                }
            }
        }
    }
    
}