CNLiveVitaeViewModel.swift 5.13 KB
//
//  CNLiveVitaeViewModel.swift
//  metaCode
//
//  Created by zx on 2024/7/12.
//  数字简历

import Foundation
import CNLiveRequestBastKit

class CNLiveVitaeViewModel:NSObject{
    
    /*
     查看简历
     参数 vsid 实名对象id
     参数 type 1简历信息 2官方认证 3证书 4工作经历 4获奖情况
     */
    static func getVitae(vsid:String,type:NSInteger,resultBlock: @escaping downSlideResultBlock) {
        let param = NSMutableDictionary()
        param.setValue(vsid, forKey: "vsid")
        param.setValue("\(type)", forKey: "type")
        CNLiveNetworking.setAllowRequestDefaultArgument(true)
        CNLiveNetworking.setupShowDataResult(false)
        CNLiveNetworking.setupDSRequest(true)
        let custom_param = NSMutableDictionary()
        custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId, forKey: "appId")
        custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey, forKey: "appKey")
        CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
        
        CNLiveNetworking.requestNetwork(with: .GET, urlString: APPBaseMCUrl+"/userApi/getVitae", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
            hiddenLoading()
            if (error == nil) {
                let resp = result as! NSDictionary
                if resp["errorCode"] as! Int == 0{
                    let dataDic = resp["data"] as! NSDictionary
                    let shopDetailModel =  newDictionaryToModel(dataDic as! [String : Any], CNLiveVitaeModel.self)
                    resultBlock(shopDetailModel,.success)
                }else{
                    resultBlock(NSNull(),.failed)
                }
            }else{
                resultBlock(NSNull(),.failed)
            }
        }

    }
    /*
     保存简历/更新简历
     参数 vsid 实名对象id
     参数 type 1简历信息 2官方认证 3证书 4工作经历 4获奖情况
     */
    static func saveVitae(city:String,icon:String,isSave:Bool,id:NSInteger,phone:String,profession:String,professionId:NSInteger,selfIntroduction:String,showAddress:Bool,showAge:Bool,showAwards:Bool,showPhone:Bool,updateTime:String,vsid:String,workTime:String,resultBlock: @escaping downSlideResultBlock) {
        let values = NSMutableDictionary()
        values.addEntries(from: ["city":city])
        values.addEntries(from: ["icon":icon])
        //简历为nil保存简历时 不传id;  更新简历时id传简历的vitaeid
        if !isSave{
            values.addEntries(from: ["id":id])
        }
        values.addEntries(from: ["phone":phone])
        values.addEntries(from: ["profession":profession])
        values.addEntries(from: ["professionId":professionId])
        values.addEntries(from: ["selfIntroduction":selfIntroduction])
        values.addEntries(from: ["showAddress":showAddress])
        values.addEntries(from: ["showAge":showAge])
        values.addEntries(from: ["showAwards":showAwards])
        values.addEntries(from: ["showPhone":showPhone])
        values.addEntries(from: ["updateTime":updateTime])
        values.addEntries(from: ["vsid":vsid])
        values.addEntries(from: ["workTime":workTime])
        do {
            let request = URLRequest.init(url: URL.init(string: APPBaseMCUrl+"/userApi/saveVitae")!) as! NSMutableURLRequest
            request.httpMethod = "POST"
            request.timeoutInterval = 10
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            let data = try JSONSerialization.data(withJSONObject: values, options: .prettyPrinted)
            request.httpBody = data
            showLoading(message: "")
            let custom_param = NSMutableDictionary()
            custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId, forKey: "appId")
            custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey, forKey: "appKey")
            CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
            CNLiveNetworking.setAllowRequestDefaultArgument(false)
            CNLiveNetworking.setupShowDataResult(false)
            CNLiveNetworking.setBuildCustomUrlRequest(request as URLRequest) { task, result, error in
                hiddenLoading()
                if (error == nil) {
                    let resp = result as! NSDictionary
                    let errorCode = "\(resp["errorCode"] ?? "0")"
                    if (errorCode == "0") {
                        resultBlock(true,.success)
                    } else {
                        showMessage(message: resp["errorMessage"] as! String)
                        resultBlock(NSNull(),.failed)
                    }
                } else {
                    if CNLiveNetworking.isNetworking() {
                        showMessage(message: "上传失败")
                        resultBlock(NSNull(),.failed)
                    } else {
                        showMessage(message: "似乎与网络断开链接")
                        resultBlock(NSNull(),.noNetwork)
                    }
                }
            }
        } catch {
            showMessage(message: "上传失败")
            resultBlock(NSNull(),.failed)
        }

    }
    
}