KMRequestServerManager.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // KMRequestServerManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/2/22.
  6. //
  7. import Cocoa
  8. enum KMRequestServerErrorCodeType: Int, CaseIterable {
  9. case unknown = 0 //未知
  10. case correct = 200 //正确
  11. case EXCEPTION_MSG_IMAGE_CODE_ERROR = 301
  12. case EXCEPTION_MSG_USER_NOT_FOUND = 302
  13. case EXCEPTION_MSG_TOKEN_IS_INVALID = 304
  14. case EXCEPTION_MSG_DEVICE_NUM_MAX = 305
  15. case EXCEPTION_MSG_USER_NOT_LOGIN = 306
  16. case EXCEPTION_MSG_GET_CODE_FAIL = 307
  17. case EXCEPTION_MSG_GET_TOKEN_FAIL = 308
  18. case EMAIL_VERIFY_CODE_KEY_ERROR = 309
  19. case EMAIL_REGISTER_ERROR = 310
  20. case EXCEPTION_MSG_PASSWORD_ERROR = 311
  21. case EXCEPTION_MSG_CODE_ACTION_ERROR = 312
  22. case EXCEPTION_MSG_CODE_SEND_ERROR = 313
  23. case EXCEPTION_MAIL_CODE_SEND_ERROR = 314
  24. case VERIFY_CODE_SEND_TOO_QUICKLY = 315
  25. case EXCEPTION_MSG_EMAIL_EXIST = 316
  26. case EXCEPTION_MSG_EMAIL_NOT_REGISTER = 317
  27. case EXCEPTION_MSG_EMAIL_LOGOUT_ING = 318
  28. case EXCEPTION_MSG_EMAIL_INVALID = 319
  29. case EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT = 320
  30. case EXCEPTION_MSG_PASSWORD_SIZE_MIN = 321
  31. case EXCEPTION_MSG_PASSWORD_SIZE_MAX = 322
  32. case EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG = 323
  33. case EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS = 324
  34. case EXCEPTION_MSG_USER_TRAIL_IN_PROGRESS = 325
  35. case EXCEPTION_TIME_TRANSFER_ERROR = 326
  36. case EXCEPTION_MSG_PASSWORD_CONTAIN_EMRTY = 327
  37. case EXCEPTION_MSG_CREATE_ORDER_PARAMETER = 328//,"支付方式异常"),
  38. case EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED = 329 //, "当前事件暂不支持"),
  39. case EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES = 330//, "账号已在其他设备登录"),
  40. case EXCEPTION_MSG_ABNORMAL_USER_STATUS = 331 //,"当前邮箱已被停用或者处于注销中状态"),
  41. case EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED = 332 //"AppStore票据验证失败"),
  42. case EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY = 333// "AppStore票据验证需要重试"),
  43. case EXCEPTION_MSG_REPEAT_SUBSCRIPTION = 334 //"用户已订阅该产品"
  44. case EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED = 335 //"恢复购买暂不支持"),
  45. case EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED = 336 //"用户交易取消或失败")
  46. case EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED = 337 //"当前订单未记录,请稍后重试"),
  47. case EXCEPTION_MSG_THE_SAME_USER = 338//"同一用户,无需处理"),
  48. case EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL = 339//"thirdPayId非法"),
  49. case EXCEPTION_MSG_ORDER_DOES_NOT_EXIST = 340// "订单不存在"),
  50. case EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL = 341// "订单状态异常"),
  51. case EXCEPTION_MSG_TRANSACTION_ID_REPEAT = 342 //"transactionId重复"),
  52. case EMAIL_VERIFY_CODE_KEY_ERROR2 = 700
  53. static func typeOfMessage(type: KMRequestServerErrorCodeType?) -> String {
  54. var result: String = ""
  55. switch type {
  56. case .EXCEPTION_MSG_IMAGE_CODE_ERROR:
  57. result = "Graphic verification code error"
  58. case .EXCEPTION_MSG_USER_NOT_FOUND:
  59. result = "Email address is not registered"
  60. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  61. result = "Invalid token or token has expired"
  62. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  63. result = "You have reached the maximum number of devices, please log out of other devices and log in again."
  64. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  65. result = "User is not logged in. Please log in and try again."
  66. case .EXCEPTION_MSG_GET_CODE_FAIL:
  67. result = "Failed to obtain authorization code"
  68. case .EXCEPTION_MSG_GET_TOKEN_FAIL:
  69. result = "Failed to obtain token"
  70. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  71. result = "Email code error"
  72. case .EMAIL_REGISTER_ERROR:
  73. result = "Register error"
  74. case .EXCEPTION_MSG_PASSWORD_ERROR:
  75. result = "Wrong password. You can check whether the letter is uppercase or lowercase."
  76. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  77. result = "Incorrect code"
  78. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  79. result = "Code send error"
  80. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  81. result = "Mail send error"
  82. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  83. result = "Enter an existing verification code" //result = "Too many codes sent, please try again later."
  84. case .EXCEPTION_MSG_EMAIL_EXIST:
  85. result = "This email is already registered, please log in."
  86. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  87. result = "This account is not registered, click Sign Up to create a new account."
  88. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  89. result = "Your account is currently being canceled and cannot be logged in. If you have any questions, please contact customer service."
  90. case .EXCEPTION_MSG_EMAIL_INVALID:
  91. result = "Please enter the correct email format"
  92. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  93. result = "Wrong password. You can check whether the letter is uppercase or lowercase." //result = "Passwords are different. Please re-enter them."
  94. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  95. result = "Password must be 6-24 characters" //result = "At least 6 characters"
  96. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  97. result = "Password must be 6-24 characters" //result = "Up to 24 characters"
  98. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  99. result = "Please add auth config"
  100. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  101. result = "You are still in the subscription and cannot cancel account. Please click Cancel Account after the subscription period ends."
  102. case .EXCEPTION_MSG_USER_TRAIL_IN_PROGRESS:
  103. result = "You are still in the subscription and cannot cancel account. Please click Cancel Account after the trial expired."
  104. case .EXCEPTION_TIME_TRANSFER_ERROR:
  105. result = "Time conversion failure"
  106. case .EMAIL_VERIFY_CODE_KEY_ERROR2:
  107. result = "Email code error"
  108. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMRTY:
  109. result = "Spaces are not allowed in password" //result = "The password cannot contain Spaces"
  110. case .EXCEPTION_MSG_CREATE_ORDER_PARAMETER:
  111. result = "Abnormal payment method"
  112. case .EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED:
  113. result = "The current event is not supported"
  114. case .EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES:
  115. result = "Your account has been logged into another device."
  116. case .EXCEPTION_MSG_ABNORMAL_USER_STATUS:
  117. result = "This email account is no longer available or canceled."
  118. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED:
  119. result = "AppStore receipt verification failed."
  120. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY:
  121. result = "Please re-verify the receipt."
  122. case .EXCEPTION_MSG_REPEAT_SUBSCRIPTION:
  123. result = "Users have already subscribed to PDF Reader Pro."
  124. case .EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED:
  125. result = "Restoring purchases may not work due to the temporary problem with the App Store system."
  126. case .EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED:
  127. result = "Payment Cancelled or Failed."
  128. case .EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED:
  129. result = "The current order is not recorded, please try again later."
  130. case .EXCEPTION_MSG_THE_SAME_USER:
  131. result = "Same user, no processing required."
  132. case .EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL:
  133. result = "ThirdPayID illegal"
  134. case .EXCEPTION_MSG_ORDER_DOES_NOT_EXIST:
  135. result = "This order does not exist."
  136. case .EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL:
  137. result = "This order status is abnormal."
  138. case .EXCEPTION_MSG_TRANSACTION_ID_REPEAT:
  139. result = "Duplicate TransactionID"
  140. case .unknown:
  141. result = ""
  142. case .correct:
  143. result = ""
  144. default:
  145. result = "Network error"
  146. }
  147. return result
  148. }
  149. }
  150. enum KMVerifyCodeType: String, CaseIterable {
  151. case unknown = "" //
  152. case register = "user_register" // 注册
  153. case logOff = "user_log_off" //注销
  154. case resetPassword = "user_reset_password" //重置密码
  155. }
  156. enum KMVerifyEmailType: String, CaseIterable {
  157. case login = "1" // 登录
  158. case register = "2" //注册
  159. }
  160. struct Result {
  161. var result: NSDictionary = [:]
  162. var message: String = ""
  163. var code : Int = 200
  164. var error: NSError?
  165. }
  166. typealias KMRequestServerComplete = (_ success: Bool, _ result: Result?) -> Void
  167. typealias KMRequestServerDateComplete = (_ success: Bool, _ result: NSDictionary?) -> Void
  168. class KMRequestServerManager: NSObject {
  169. static let manager = KMRequestServerManager()
  170. var loginAlert: NSAlert? = nil
  171. }
  172. //MARK: - 会员
  173. extension KMRequestServerManager {
  174. //MARK: Private Method
  175. private func uuid() -> String {
  176. return GetHardwareUUID()!
  177. }
  178. }
  179. //MARK: - AI
  180. extension KMRequestServerManager {
  181. }
  182. //MARK: - 数据处理
  183. extension KMRequestServerManager {
  184. func dealData(responseObject: NSDictionary?, error: NSError?) -> Result {
  185. var dic: NSDictionary = [:]
  186. if (error == nil && responseObject != nil) {
  187. dic = responseObject!
  188. } else {
  189. var info = responseObject
  190. if (error != nil) {
  191. let data = error!.userInfo["com.alamofire.serialization.response.error.data"]
  192. if (data != nil) {
  193. info = try?JSONSerialization.jsonObject(with: data as? Data ?? Data(), options: JSONSerialization.ReadingOptions.mutableLeaves) as? NSDictionary
  194. }
  195. dic = info ?? [:]
  196. }
  197. }
  198. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  199. var code: Int = dic["code"] as? Int ?? 0
  200. let message: String = dic["msg"] as? String ?? "unknown error"
  201. let error = NSError(domain: message, code: code)
  202. if result.count == 0 && responseObject != nil && code == 0 {
  203. code = 200
  204. }
  205. if code == 318 {
  206. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  207. let alert = NSAlert()
  208. alert.messageText = NSLocalizedString("Unable to Log in", comment: "")
  209. alert.informativeText = NSLocalizedString("Your account is currently being canceled and cannot be logged in. If you have any questions, please contact us at support@pdfreaderpro.com", comment: "")
  210. alert.addButton(withTitle: NSLocalizedString("Contact Us", comment: ""))
  211. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  212. var window = NSWindow.currentWindow()
  213. if window != nil {
  214. alert.beginSheetModal(for: window) { result in
  215. if (result == .alertFirstButtonReturn) {
  216. // KMMailHelper.newEmail(withContacts: "support@pdfreaderpro.com", andSubjects: "")
  217. // NSWorkspace.shared.launchApplication("Mailto:support@pdfreaderpro.com")
  218. // NSWorkspace.shared.open(URL(string: "mailto:support@pdfreaderpro.com")!)
  219. NSWorkspace.shared.open(URL(string: "https://www.pdfreaderpro.com/contact")!)
  220. // let service = NSSharingService(named: NSSharingService.Name.composeEmail)
  221. // service?.recipients = ["support@pdfreaderpro.com"]
  222. // service?.subject = "Test Mail"
  223. // service?.perform(withItems: ["Test Mail body"])
  224. } else if result == .alertSecondButtonReturn {
  225. return
  226. }
  227. }
  228. }
  229. })
  230. }
  231. if code == 304 {
  232. }
  233. if code == 330 {
  234. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [unowned self] in
  235. if loginAlert == nil {
  236. loginAlert = NSAlert()
  237. loginAlert?.messageText = NSLocalizedString("Please Log in again", comment: "")
  238. loginAlert?.informativeText = NSLocalizedString("Your account has been logged into another device. If it was not done by you, please change your password as soon as possible.", comment: "")
  239. loginAlert?.addButton(withTitle: NSLocalizedString("Change Password", comment: ""))
  240. loginAlert?.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  241. var window = NSWindow.currentWindow()
  242. if window != nil {
  243. loginAlert?.beginSheetModal(for: window) { result in
  244. if (result == .alertFirstButtonReturn) {
  245. } else if result == .alertSecondButtonReturn {
  246. self.loginAlert = nil
  247. return
  248. }
  249. self.loginAlert = nil
  250. }
  251. } else {
  252. self.loginAlert = nil
  253. }
  254. }
  255. })
  256. }
  257. if code == 343 {
  258. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [unowned self] in
  259. var window = NSWindow.currentWindow()
  260. })
  261. }
  262. // if code == 500 {
  263. // let alert = NSAlert()
  264. // alert.messageText = NSLocalizedString(message, comment: "")
  265. // alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  266. // if NSApp.mainWindow != nil {
  267. // alert.beginSheetModal(for: NSApp.mainWindow!)
  268. // } else {
  269. // alert.runModal()
  270. // }
  271. // }
  272. KMPrint(dic)
  273. return Result(result: result,message: message,code: code, error: error)
  274. }
  275. }