// // KMRequestServerManager.swift // PDF Reader Pro // // Created by lizhe on 2023/2/22. // import Cocoa enum KMRequestServerErrorCodeType: Int, CaseIterable { case unknown = 0 //未知 case correct = 200 //正确 case EXCEPTION_MSG_IMAGE_CODE_ERROR = 301 case EXCEPTION_MSG_USER_NOT_FOUND = 302 case EXCEPTION_MSG_TOKEN_IS_INVALID = 304 case EXCEPTION_MSG_DEVICE_NUM_MAX = 305 case EXCEPTION_MSG_USER_NOT_LOGIN = 306 case EXCEPTION_MSG_GET_CODE_FAIL = 307 case EXCEPTION_MSG_GET_TOKEN_FAIL = 308 case EMAIL_VERIFY_CODE_KEY_ERROR = 309 case EMAIL_REGISTER_ERROR = 310 case EXCEPTION_MSG_PASSWORD_ERROR = 311 case EXCEPTION_MSG_CODE_ACTION_ERROR = 312 case EXCEPTION_MSG_CODE_SEND_ERROR = 313 case EXCEPTION_MAIL_CODE_SEND_ERROR = 314 case VERIFY_CODE_SEND_TOO_QUICKLY = 315 case EXCEPTION_MSG_EMAIL_EXIST = 316 case EXCEPTION_MSG_EMAIL_NOT_REGISTER = 317 case EXCEPTION_MSG_EMAIL_LOGOUT_ING = 318 case EXCEPTION_MSG_EMAIL_INVALID = 319 case EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT = 320 case EXCEPTION_MSG_PASSWORD_SIZE_MIN = 321 case EXCEPTION_MSG_PASSWORD_SIZE_MAX = 322 case EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG = 323 case EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS = 324 case EXCEPTION_MSG_USER_TRAIL_IN_PROGRESS = 325 case EXCEPTION_TIME_TRANSFER_ERROR = 326 case EXCEPTION_MSG_PASSWORD_CONTAIN_EMRTY = 327 case EXCEPTION_MSG_CREATE_ORDER_PARAMETER = 328//,"支付方式异常"), case EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED = 329 //, "当前事件暂不支持"), case EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES = 330//, "账号已在其他设备登录"), case EXCEPTION_MSG_ABNORMAL_USER_STATUS = 331 //,"当前邮箱已被停用或者处于注销中状态"), case EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED = 332 //"AppStore票据验证失败"), case EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY = 333// "AppStore票据验证需要重试"), case EXCEPTION_MSG_REPEAT_SUBSCRIPTION = 334 //"用户已订阅该产品" case EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED = 335 //"恢复购买暂不支持"), case EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED = 336 //"用户交易取消或失败") case EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED = 337 //"当前订单未记录,请稍后重试"), case EXCEPTION_MSG_THE_SAME_USER = 338//"同一用户,无需处理"), case EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL = 339//"thirdPayId非法"), case EXCEPTION_MSG_ORDER_DOES_NOT_EXIST = 340// "订单不存在"), case EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL = 341// "订单状态异常"), case EXCEPTION_MSG_TRANSACTION_ID_REPEAT = 342 //"transactionId重复"), case EMAIL_VERIFY_CODE_KEY_ERROR2 = 700 static func typeOfMessage(type: KMRequestServerErrorCodeType?) -> String { var result: String = "" switch type { case .EXCEPTION_MSG_IMAGE_CODE_ERROR: result = "Graphic verification code error" case .EXCEPTION_MSG_USER_NOT_FOUND: result = "Email address is not registered" case .EXCEPTION_MSG_TOKEN_IS_INVALID: result = "Invalid token or token has expired" case .EXCEPTION_MSG_DEVICE_NUM_MAX: result = "You have reached the maximum number of devices, please log out of other devices and log in again." case .EXCEPTION_MSG_USER_NOT_LOGIN: result = "User is not logged in. Please log in and try again." case .EXCEPTION_MSG_GET_CODE_FAIL: result = "Failed to obtain authorization code" case .EXCEPTION_MSG_GET_TOKEN_FAIL: result = "Failed to obtain token" case .EMAIL_VERIFY_CODE_KEY_ERROR: result = "Email code error" case .EMAIL_REGISTER_ERROR: result = "Register error" case .EXCEPTION_MSG_PASSWORD_ERROR: result = "Wrong password. You can check whether the letter is uppercase or lowercase." case .EXCEPTION_MSG_CODE_ACTION_ERROR: result = "Incorrect code" case .EXCEPTION_MSG_CODE_SEND_ERROR: result = "Code send error" case .EXCEPTION_MAIL_CODE_SEND_ERROR: result = "Mail send error" case .VERIFY_CODE_SEND_TOO_QUICKLY: result = "Enter an existing verification code" //result = "Too many codes sent, please try again later." case .EXCEPTION_MSG_EMAIL_EXIST: result = "This email is already registered, please log in." case .EXCEPTION_MSG_EMAIL_NOT_REGISTER: result = "This account is not registered, click Sign Up to create a new account." case .EXCEPTION_MSG_EMAIL_LOGOUT_ING: result = "Your account is currently being canceled and cannot be logged in. If you have any questions, please contact customer service." case .EXCEPTION_MSG_EMAIL_INVALID: result = "Please enter the correct email format" case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT: result = "Wrong password. You can check whether the letter is uppercase or lowercase." //result = "Passwords are different. Please re-enter them." case .EXCEPTION_MSG_PASSWORD_SIZE_MIN: result = "Password must be 6-24 characters" //result = "At least 6 characters" case .EXCEPTION_MSG_PASSWORD_SIZE_MAX: result = "Password must be 6-24 characters" //result = "Up to 24 characters" case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG: result = "Please add auth config" case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS: result = "You are still in the subscription and cannot cancel account. Please click Cancel Account after the subscription period ends." case .EXCEPTION_MSG_USER_TRAIL_IN_PROGRESS: result = "You are still in the subscription and cannot cancel account. Please click Cancel Account after the trial expired." case .EXCEPTION_TIME_TRANSFER_ERROR: result = "Time conversion failure" case .EMAIL_VERIFY_CODE_KEY_ERROR2: result = "Email code error" case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMRTY: result = "Spaces are not allowed in password" //result = "The password cannot contain Spaces" case .EXCEPTION_MSG_CREATE_ORDER_PARAMETER: result = "Abnormal payment method" case .EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED: result = "The current event is not supported" case .EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES: result = "Your account has been logged into another device." case .EXCEPTION_MSG_ABNORMAL_USER_STATUS: result = "This email account is no longer available or canceled." case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED: result = "AppStore receipt verification failed." case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY: result = "Please re-verify the receipt." case .EXCEPTION_MSG_REPEAT_SUBSCRIPTION: result = "Users have already subscribed to PDF Reader Pro." case .EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED: result = "Restoring purchases may not work due to the temporary problem with the App Store system." case .EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED: result = "Payment Cancelled or Failed." case .EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED: result = "The current order is not recorded, please try again later." case .EXCEPTION_MSG_THE_SAME_USER: result = "Same user, no processing required." case .EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL: result = "ThirdPayID illegal" case .EXCEPTION_MSG_ORDER_DOES_NOT_EXIST: result = "This order does not exist." case .EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL: result = "This order status is abnormal." case .EXCEPTION_MSG_TRANSACTION_ID_REPEAT: result = "Duplicate TransactionID" case .unknown: result = "" case .correct: result = "" default: result = "Network error" } return result } } enum KMVerifyCodeType: String, CaseIterable { case unknown = "" // case register = "user_register" // 注册 case logOff = "user_log_off" //注销 case resetPassword = "user_reset_password" //重置密码 } enum KMVerifyEmailType: String, CaseIterable { case login = "1" // 登录 case register = "2" //注册 } struct Result { var result: NSDictionary = [:] var message: String = "" var code : Int = 200 var error: NSError? } typealias KMRequestServerComplete = (_ success: Bool, _ result: Result?) -> Void typealias KMRequestServerDateComplete = (_ success: Bool, _ result: NSDictionary?) -> Void class KMRequestServerManager: NSObject { static let manager = KMRequestServerManager() var loginAlert: NSAlert? = nil } //MARK: - 会员 extension KMRequestServerManager { //MARK: Private Method private func uuid() -> String { return GetHardwareUUID()! } } //MARK: - AI extension KMRequestServerManager { } //MARK: - 数据处理 extension KMRequestServerManager { func dealData(responseObject: NSDictionary?, error: NSError?) -> Result { var dic: NSDictionary = [:] if (error == nil && responseObject != nil) { dic = responseObject! } else { var info = responseObject if (error != nil) { let data = error!.userInfo["com.alamofire.serialization.response.error.data"] if (data != nil) { info = try?JSONSerialization.jsonObject(with: data as? Data ?? Data(), options: JSONSerialization.ReadingOptions.mutableLeaves) as? NSDictionary } dic = info ?? [:] } } let result: NSDictionary = dic["result"] as? NSDictionary ?? [:] var code: Int = dic["code"] as? Int ?? 0 let message: String = dic["msg"] as? String ?? "unknown error" let error = NSError(domain: message, code: code) if result.count == 0 && responseObject != nil && code == 0 { code = 200 } if code == 318 { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: { let alert = NSAlert() alert.messageText = NSLocalizedString("Unable to Log in", comment: "") 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: "") alert.addButton(withTitle: NSLocalizedString("Contact Us", comment: "")) alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) var window = NSWindow.currentWindow() if window != nil { alert.beginSheetModal(for: window) { result in if (result == .alertFirstButtonReturn) { // KMMailHelper.newEmail(withContacts: "support@pdfreaderpro.com", andSubjects: "") // NSWorkspace.shared.launchApplication("Mailto:support@pdfreaderpro.com") // NSWorkspace.shared.open(URL(string: "mailto:support@pdfreaderpro.com")!) NSWorkspace.shared.open(URL(string: "https://www.pdfreaderpro.com/contact")!) // let service = NSSharingService(named: NSSharingService.Name.composeEmail) // service?.recipients = ["support@pdfreaderpro.com"] // service?.subject = "Test Mail" // service?.perform(withItems: ["Test Mail body"]) } else if result == .alertSecondButtonReturn { return } } } }) } if code == 304 { } if code == 330 { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [unowned self] in if loginAlert == nil { loginAlert = NSAlert() loginAlert?.messageText = NSLocalizedString("Please Log in again", comment: "") 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: "") loginAlert?.addButton(withTitle: NSLocalizedString("Change Password", comment: "")) loginAlert?.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) var window = NSWindow.currentWindow() if window != nil { loginAlert?.beginSheetModal(for: window) { result in if (result == .alertFirstButtonReturn) { } else if result == .alertSecondButtonReturn { self.loginAlert = nil return } self.loginAlert = nil } } else { self.loginAlert = nil } } }) } if code == 343 { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [unowned self] in var window = NSWindow.currentWindow() }) } // if code == 500 { // let alert = NSAlert() // alert.messageText = NSLocalizedString(message, comment: "") // alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) // if NSApp.mainWindow != nil { // alert.beginSheetModal(for: NSApp.mainWindow!) // } else { // alert.runModal() // } // } KMPrint(dic) return Result(result: result,message: message,code: code, error: error) } }