// // KMOEMRequestServerManager.swift // Cisdem PDFMaster // // Created by wanjun on 2024/3/11. // import Cocoa import AFNetworking @objcMembers class OEMResultWrapper: NSObject { var success: Bool = false var data: NSDictionary = [:] init(success: Bool, data: NSDictionary) { self.success = success self.data = data } } @objc enum KMOEMRequestState : Int { case register = 0 // APP序列码激活 case registerAI // AI序列码激活 case update // 检查更新 case install // 首次安装 } @objcMembers class KMOEMRequestServerManager: NSObject { static let defaultManager = KMOEMRequestServerManager() typealias KMRequestServerComplete = (_ wrapper: OEMResultWrapper) -> Void // MARK: AI Action (public) func oemRequest(state: KMOEMRequestState, regCode: String = "", complete: @escaping KMRequestServerComplete) { if state == .register || state == .registerAI { //注册码注册 oemRegister(regCode: regCode, state: state, complete: complete) } else if state == .update { // 检查更新 oemUpdate(complete: complete) } else if state == .install { // 首次安装 oemInstall(complete: complete) } } // MARK: Private private func uuid() -> String { return GetHardwareUUID()! } func version() -> String { return "6.5.0" } private func versionAI() -> String { return "2.0.0" } /** @abstract 注册码注册 @param regCode 注册码 @param complete 返回 */ private func oemRegister(regCode: String, state: KMOEMRequestState, complete: @escaping KMRequestServerComplete) { var productId = 71 var version = version() if state == .registerAI { productId = 20001 version = versionAI() } let urlString = "https://www.cisdem.com/API/register_success/" let params: [String: Any] = ["product_id": productId, "device_id": uuid(), "reg_code": regCode, "version": version] KMOEMRequestServer.requestServer.oemRequest(urlString: urlString, params: params) { formData in } requestSerializer: { requestSerializer in requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent") } completion: { task, responseObject, error in if responseObject != nil { let code: Int = responseObject!["code"] as? Int ?? 06005 let message: String = responseObject!["msg"] as? String ?? "unknown error" if code == 400 { complete(OEMResultWrapper(success: true, data: ["code":code, "msg":message])) } else { complete(OEMResultWrapper(success: false, data: ["code":code, "msg":message])) } } else { complete(OEMResultWrapper(success: false, data: ["code": "06005", "msg": "unknown error"])) } } } /** @abstract 检测更新 @param complete 返回 */ private func oemUpdate(complete: @escaping KMRequestServerComplete) { var urlString = "https://www.Cisdem.com/API/update_information/" let params: [String: Any] = ["product_id": 71, "device_id": uuid(), "version": version()] KMOEMRequestServer.requestServer.oemRequest(urlString: urlString, params: params) { formData in } requestSerializer: { requestSerializer in requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent") } completion: { task, responseObject, error in if responseObject != nil { let code: Int = responseObject!["code"] as? Int ?? 06005 let message: String = responseObject!["msg"] as? String ?? "unknown error" let version: String = responseObject!["version"] as? String ?? "0.0.0" let publish_time: String = responseObject!["publish_time"] as? String ?? "" let update_content: String = responseObject!["update_content"] as? String ?? "" let download_url: String = responseObject!["download_url"] as? String ?? "" if code == 400 { complete(OEMResultWrapper(success: true, data: ["code":code, "msg":message, "version":version])) } else { complete(OEMResultWrapper(success: false, data: ["code":code, "msg":message, "version":version])) } } else { complete(OEMResultWrapper(success: false, data: ["code": "06005", "msg": "unknown error", "version":"0.0.0"])) } } } /** @abstract 首次安装 @param complete 返回 */ private func oemInstall(complete: @escaping KMRequestServerComplete) { let urlString = "https://www.cisdem.com/API/install_success/" let params: [String: Any] = ["product_id": 71, "device_id": uuid(), "version": version()] KMOEMRequestServer.requestServer.oemRequest(urlString: urlString, params: params) { formData in } requestSerializer: { requestSerializer in requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent") } completion: { task, responseObject, error in if responseObject != nil { let code: Int = responseObject!["code"] as? Int ?? 06005 let message: String = responseObject!["msg"] as? String ?? "unknown error" if code == 400 { complete(OEMResultWrapper(success: true, data: ["code":code, "msg":message])) } else { complete(OEMResultWrapper(success: false, data: ["code":code, "msg":message])) } } else { complete(OEMResultWrapper(success: false, data: ["code": "06005", "msg": "unknown error"])) } } } } typealias KMOEMHttpRequestServerComplete = (_ task: URLSessionDataTask?, _ responseObject: AnyObject?, _ error: NSError?) -> Void class KMOEMRequestServer { var sessionManager: AFHTTPSessionManager! static let requestServer = KMOEMRequestServer() var task: URLSessionTask? init() { let configuration: URLSessionConfiguration = URLSessionConfiguration.default sessionManager = AFHTTPSessionManager.init(sessionConfiguration: configuration) sessionManager.securityPolicy = AFSecurityPolicy.default() } func oemRequest(urlString: String, params: Dictionary?, body: ((_ formData:AFMultipartFormData) -> Void)?, requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?, completion: KMHttpRequestServerComplete?) -> Void { sessionManager.requestSerializer.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type") sessionManager.requestSerializer.timeoutInterval = 60 sessionManager.responseSerializer = AFJSONResponseSerializer() sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"] if (requestSerializer != nil) { requestSerializer!(sessionManager.requestSerializer); } if (body != nil) { self.task = sessionManager.get(urlString, parameters: params, headers: nil, progress: { progress in }, success: { task, responseObject in let responseObject = responseObject as AnyObject if (completion != nil) { completion!(task,responseObject,nil); } }, failure: { task, error in if (completion != nil) { completion!(task, nil, error as NSError); } }) } else { self.task = sessionManager.get(urlString, parameters: params, headers: nil, progress: { progress in }, success: { task, responseObject in if (completion != nil) { let responseObject = responseObject as AnyObject completion!(task,responseObject,nil); } }, failure: { task, error in if (completion != nil) { completion!(task, nil, error as NSError); } }) } } }