123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- //
- // 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<String, Any>?,
- 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);
- }
- })
- }
- }
- }
|