KMOEMRequestServerManager.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // KMOEMRequestServerManager.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by wanjun on 2024/3/11.
  6. //
  7. import Cocoa
  8. import AFNetworking
  9. @objcMembers
  10. class OEMResultWrapper: NSObject {
  11. var success: Bool = false
  12. var data: NSDictionary = [:]
  13. init(success: Bool, data: NSDictionary) {
  14. self.success = success
  15. self.data = data
  16. }
  17. }
  18. @objc enum KMOEMRequestState : Int {
  19. case register = 0 // APP序列码激活
  20. case registerAI // AI序列码激活
  21. case update // 检查更新
  22. case install // 首次安装
  23. }
  24. @objcMembers
  25. class KMOEMRequestServerManager: NSObject {
  26. static let defaultManager = KMOEMRequestServerManager()
  27. typealias KMRequestServerComplete = (_ wrapper: OEMResultWrapper) -> Void
  28. // MARK: AI Action (public)
  29. func oemRequest(state: KMOEMRequestState, regCode: String = "", complete: @escaping KMRequestServerComplete) {
  30. if state == .register || state == .registerAI { //注册码注册
  31. oemRegister(regCode: regCode, state: state, complete: complete)
  32. } else if state == .update { // 检查更新
  33. oemUpdate(complete: complete)
  34. } else if state == .install { // 首次安装
  35. oemInstall(complete: complete)
  36. }
  37. }
  38. // MARK: Private
  39. private func uuid() -> String {
  40. return GetHardwareUUID()!
  41. }
  42. func version() -> String {
  43. return "6.5.0"
  44. }
  45. private func versionAI() -> String {
  46. return "2.0.0"
  47. }
  48. /**
  49. @abstract 注册码注册
  50. @param regCode 注册码
  51. @param complete 返回
  52. */
  53. private func oemRegister(regCode: String, state: KMOEMRequestState, complete: @escaping KMRequestServerComplete) {
  54. var productId = 71
  55. var version = version()
  56. if state == .registerAI {
  57. productId = 20001
  58. version = versionAI()
  59. }
  60. let urlString = "https://www.cisdem.com/API/register_success/"
  61. let params: [String: Any] = ["product_id": productId,
  62. "device_id": uuid(),
  63. "reg_code": regCode,
  64. "version": version]
  65. KMOEMRequestServer.requestServer.oemRequest(urlString: urlString, params: params) { formData in
  66. } requestSerializer: { requestSerializer in
  67. requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  68. } completion: { task, responseObject, error in
  69. if responseObject != nil {
  70. let code: Int = responseObject!["code"] as? Int ?? 06005
  71. let message: String = responseObject!["msg"] as? String ?? "unknown error"
  72. if code == 400 {
  73. complete(OEMResultWrapper(success: true, data: ["code":code, "msg":message]))
  74. } else {
  75. complete(OEMResultWrapper(success: false, data: ["code":code, "msg":message]))
  76. }
  77. } else {
  78. complete(OEMResultWrapper(success: false, data: ["code": "06005", "msg": "unknown error"]))
  79. }
  80. }
  81. }
  82. /**
  83. @abstract 检测更新
  84. @param complete 返回
  85. */
  86. private func oemUpdate(complete: @escaping KMRequestServerComplete) {
  87. var urlString = "https://www.Cisdem.com/API/update_information/"
  88. let params: [String: Any] = ["product_id": 71,
  89. "device_id": uuid(),
  90. "version": version()]
  91. KMOEMRequestServer.requestServer.oemRequest(urlString: urlString, params: params) { formData in
  92. } requestSerializer: { requestSerializer in
  93. requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  94. } completion: { task, responseObject, error in
  95. if responseObject != nil {
  96. let code: Int = responseObject!["code"] as? Int ?? 06005
  97. let message: String = responseObject!["msg"] as? String ?? "unknown error"
  98. let version: String = responseObject!["version"] as? String ?? "0.0.0"
  99. let publish_time: String = responseObject!["publish_time"] as? String ?? ""
  100. let update_content: String = responseObject!["update_content"] as? String ?? ""
  101. let download_url: String = responseObject!["download_url"] as? String ?? ""
  102. if code == 400 {
  103. complete(OEMResultWrapper(success: true, data: ["code":code, "msg":message, "version":version]))
  104. } else {
  105. complete(OEMResultWrapper(success: false, data: ["code":code, "msg":message, "version":version]))
  106. }
  107. } else {
  108. complete(OEMResultWrapper(success: false, data: ["code": "06005", "msg": "unknown error", "version":"0.0.0"]))
  109. }
  110. }
  111. }
  112. /**
  113. @abstract 首次安装
  114. @param complete 返回
  115. */
  116. private func oemInstall(complete: @escaping KMRequestServerComplete) {
  117. let urlString = "https://www.cisdem.com/API/install_success/"
  118. let params: [String: Any] = ["product_id": 71,
  119. "device_id": uuid(),
  120. "version": version()]
  121. KMOEMRequestServer.requestServer.oemRequest(urlString: urlString, params: params) { formData in
  122. } requestSerializer: { requestSerializer in
  123. requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  124. } completion: { task, responseObject, error in
  125. if responseObject != nil {
  126. let code: Int = responseObject!["code"] as? Int ?? 06005
  127. let message: String = responseObject!["msg"] as? String ?? "unknown error"
  128. if code == 400 {
  129. complete(OEMResultWrapper(success: true, data: ["code":code, "msg":message]))
  130. } else {
  131. complete(OEMResultWrapper(success: false, data: ["code":code, "msg":message]))
  132. }
  133. } else {
  134. complete(OEMResultWrapper(success: false, data: ["code": "06005", "msg": "unknown error"]))
  135. }
  136. }
  137. }
  138. }
  139. typealias KMOEMHttpRequestServerComplete = (_ task: URLSessionDataTask?, _ responseObject: AnyObject?, _ error: NSError?) -> Void
  140. class KMOEMRequestServer {
  141. var sessionManager: AFHTTPSessionManager!
  142. static let requestServer = KMOEMRequestServer()
  143. var task: URLSessionTask?
  144. init() {
  145. let configuration: URLSessionConfiguration = URLSessionConfiguration.default
  146. sessionManager = AFHTTPSessionManager.init(sessionConfiguration: configuration)
  147. sessionManager.securityPolicy = AFSecurityPolicy.default()
  148. }
  149. func oemRequest(urlString: String,
  150. params: Dictionary<String, Any>?,
  151. body: ((_ formData:AFMultipartFormData) -> Void)?,
  152. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  153. completion: KMHttpRequestServerComplete?) -> Void {
  154. sessionManager.requestSerializer.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
  155. sessionManager.requestSerializer.timeoutInterval = 60
  156. sessionManager.responseSerializer = AFJSONResponseSerializer()
  157. sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
  158. if (requestSerializer != nil) {
  159. requestSerializer!(sessionManager.requestSerializer);
  160. }
  161. if (body != nil) {
  162. self.task = sessionManager.get(urlString, parameters: params, headers: nil, progress: { progress in
  163. }, success: { task, responseObject in
  164. let responseObject = responseObject as AnyObject
  165. if (completion != nil) {
  166. completion!(task,responseObject,nil);
  167. }
  168. }, failure: { task, error in
  169. if (completion != nil) {
  170. completion!(task, nil, error as NSError);
  171. }
  172. })
  173. } else {
  174. self.task = sessionManager.get(urlString, parameters: params, headers: nil, progress: { progress in
  175. }, success: { task, responseObject in
  176. if (completion != nil) {
  177. let responseObject = responseObject as AnyObject
  178. completion!(task,responseObject,nil);
  179. }
  180. }, failure: { task, error in
  181. if (completion != nil) {
  182. completion!(task, nil, error as NSError);
  183. }
  184. })
  185. }
  186. }
  187. }