KMMemberCenterManager.swift 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. //
  2. // KMMemberCenterManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/21.
  6. //
  7. import Cocoa
  8. enum KMMemberCenterErrorCodeType: Int, CaseIterable {
  9. case UNKNOWN = 0 //未知
  10. case SUCCESS = 200 //正确
  11. case EXCEPTION_MSG_TOKEN_IS_INVALID = 304 // 无效的token或者token已过期
  12. case EXCEPTION_MSG_DEVICE_NUM_MAX = 305 // 您的登录设备已达上限,请退出其他设备重新登录
  13. case EXCEPTION_MSG_USER_NOT_LOGIN = 306 // 用户没有登录,请登录后再访问
  14. case EMAIL_VERIFY_CODE_KEY_ERROR = 309 // email code error!
  15. case EMAIL_REGISTER_ERROR = 310 // Register error!
  16. case EXCEPTION_MSG_PASSWORD_ERROR = 311 // 密码错误,您可以检查大小写状态
  17. case EXCEPTION_MSG_CODE_ACTION_ERROR = 312 // code action error
  18. case EXCEPTION_MSG_CODE_SEND_ERROR = 313 // code send error
  19. case EXCEPTION_MAIL_CODE_SEND_ERROR = 314 // mail send error
  20. case VERIFY_CODE_SEND_TOO_QUICKLY = 315 // code send too quickly
  21. case EXCEPTION_MSG_EMAIL_EXIST = 316 // 该邮箱已有账号,请前往登录
  22. case EXCEPTION_MSG_EMAIL_NOT_REGISTER = 317 // 当前账号未注册,点击下一步我们将为您注册账号
  23. case EXCEPTION_MSG_EMAIL_LOGOUT_ING = 318 // 注销中,无法登录,若有问题可联系客服
  24. case EXCEPTION_MSG_EMAIL_INVALID = 319 // 请输入正确的邮箱格式
  25. case EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT = 320 // 密码不一致,请重新输入
  26. case EXCEPTION_MSG_PASSWORD_SIZE_MIN = 321 // 密码请包含6~24个字符
  27. case EXCEPTION_MSG_PASSWORD_SIZE_MAX = 322 // 密码请包含6~24个字符
  28. case EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG = 323 // please add auth config
  29. case EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS = 324 // 您目前还在订阅期内,暂时无法注销,请订阅期结束后再点击注销账号
  30. case EXCEPTION_TIME_TRANSFER_ERROR = 326 // 时间转换失败
  31. case EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY = 327 // 密码不能包含空格
  32. case EXCEPTION_MSG_CREATE_ORDER_PARAMETER = 328 // 支付方式异常
  33. case EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED = 329 // 当前事件暂不支持
  34. case EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES = 330 // 账号已在其他设备登录
  35. case EXCEPTION_MSG_ABNORMAL_USER_STATUS = 331 // 当前邮箱已被停用或者处于注销中状态
  36. case EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED = 332 // Appstore票据验证失败"
  37. case EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY = 333 //Appstore票据验证需要重试
  38. case EXCEPTION_MSG_REPEAT_SUBSCRIPTION = 334 // 用户已订阅该产品
  39. case EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED = 335 // 恢复购买暂不支持
  40. case EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED = 336 // 用户交易取消或失败
  41. case EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED = 337 // 当前订单末记录,请稍后重试
  42. case EXCEPTION_MSG_THE_SAME_USER = 338 //同一用户,无需处理
  43. case EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL = 339 //thirdPayld非法
  44. case EXCEPTION_MSG_ORDER_DOES_NOT_EXIST = 340 //订单不存在
  45. case EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL = 341 // 订单状态异常
  46. case EXCEPTION_MSG_TRANSACTION_ID_REPEAT = 342 // transactionld重复
  47. case EXCEPTION_TICKET_HAS_BEEN_BLOCKED = 343 // The ticket has been blocked
  48. case EXCEPTION_TICKET_HAS_BEEN_CREATED = 344 // 当前票据已创建订单
  49. case EXCEPTION_USER_IS_NOT_EDUCATION_USER = 345 // 用户不是教育用户
  50. case EXCEPTION_USER_HAS_EDUCATION = 346 // 用户近一年有教育购买订单
  51. case EXCEPTION_USER_CANNOT_BUY_PREMIUM = 347 // 用户已有标准版订阅,无法购买高级版
  52. case EXCEPTION_USER_CANNOT_BUY_VIP = 348 // 用户已有高级版订阅,无法购买标准版
  53. case EXCEPTION_USER_CANNOT_UPGRADE_VIP = 349 // 买断用户无法升级为标准版
  54. case EXCEPTION_COUPON_NOT_EXISTS = 350 // 优惠券不存在
  55. case EXCEPTION_COUPONTYPE_ERROR = 351 // 优惠券类型错误
  56. case EXCEPTION_COUPON_PRODUCT_ERROR = 352 // 优惠券关联产品错误
  57. case EXCEPTION_COUPON_INVALID = 353 // 优惠券已失效
  58. case EXCEPTION_PRODUCT_NOT_EXIST = 354 // 产品不存在
  59. case EXCEPTION_MSG_CONTACT_US = 355 //请联系我们获取最新折扣
  60. case EXCEPTION_MSG_APP_STORE_TRANSACTION_ID_VALIDATION_FAILED = 356 // 验证苹果交易失败
  61. case EXCEPTION_MSG_GOOGLE_STORE_TRANSACTION_ID_VALIDATION_FAILED = 357 // 验证谷歌交易失败
  62. case EXCEPTION_PRICE_ERROR = 358 // 价格错误
  63. case EXCEPTION_MSG_ORDER_CREATE_FAILED = 359 // 订单创建失败
  64. case USER_ALREADY_SUBSCRIBED_PLAN = 360 // 该用户已经是PDF Reader Pro订阅版会员,确定再赠送吗
  65. case USER_ALREADY_SUBSCRIBED_PACKAGE = 361 // 该用户已经是PDF Reader Pro永久版会员,无需再赠送哦
  66. case EXCEPTION_MSG_USER_NOT_EXIST = 400 //用户不存在
  67. case EXCEPTION_USER_ALREADY_LOTTERY = 364 // 用户已经抽过奖啦
  68. }
  69. @objc enum KMVerificationCodeType : Int {
  70. case login = 0 // 登录验证码
  71. case reset // 重置密码验证码
  72. case logout // 注销验证码
  73. }
  74. @objc enum KMBuyPlatformType : Int {
  75. case web = 0 // 官网
  76. case dmg // 重置密码验证码
  77. case macLite // Mac store免费版
  78. case exe
  79. case macPro // Mac store付费版
  80. case iOSLite // iOS免费版
  81. case iOSPro // iOS付费版
  82. case Android
  83. }
  84. typealias KMMemberCenterComplete = (_ success: Bool, _ result: KMMemberCenterResult?) -> Void
  85. typealias KMMemberProductComplete = (_ success: Bool, _ result: KMMemberProductResult?) -> Void
  86. typealias KMMemberRequestInfoComplete = (_ success: Bool, _ info: NSDictionary?) -> Void
  87. class KMMemberCenterManager: NSObject {
  88. static let manager = KMMemberCenterManager()
  89. private let configuration: KMMemberCenterConfig = KMMemberCenterConfig()
  90. // MARK: 判断有无网络
  91. func isConnectionAvailable() -> Bool {
  92. let reachability = Reachability(hostname: "www.apple.com")
  93. let state = reachability?.currentReachabilityStatus()
  94. if(state == NotReachable) {
  95. return false
  96. }
  97. return true
  98. }
  99. // MARK: 拼接URL(为Get类型)
  100. func constructURLString(baseURLString: String, parameters: [String: Any]) -> String? {
  101. // Ensure the base URL is valid
  102. guard var components = URLComponents(string: baseURLString) else {
  103. return nil
  104. }
  105. // Convert parameters to query items
  106. components.queryItems = parameters.map { key, value in
  107. URLQueryItem(name: key, value: "\(value)")
  108. }
  109. // Return the complete URL as a string
  110. return components.url?.absoluteString
  111. }
  112. // MARK: 登录模块
  113. /**
  114. @abstract 新会员系统重置密码
  115. @param email 邮箱
  116. @param verifyCode 验证码
  117. @param password 密码
  118. @param complete 回调
  119. */
  120. func resetPassword(email: String, verifyCode: String, password: String, _ complete: @escaping KMMemberCenterComplete) {
  121. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/memberResetPassword"
  122. // var escapedString = q
  123. // escapedString = String(data: escapedString.data(using: .utf8)!, encoding: .utf8)!
  124. let paraDict: [String: Any] = ["email": email,
  125. "verifyCode": verifyCode,
  126. "appId": "1",
  127. "password": password]
  128. let postData = try! JSONSerialization.data(withJSONObject: paraDict)
  129. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  130. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  131. request.httpMethod = "POST"
  132. request.httpBody = postData
  133. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  134. guard let data = data else {
  135. print(String(describing: error))
  136. return
  137. }
  138. let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
  139. if jsonObject != nil {
  140. let code: Int = jsonObject!["code"] as? Int ?? 0
  141. let result_bool: Bool = jsonObject!["result"] as? Bool ?? false
  142. let message: String = jsonObject!["msg"] as? String ?? ""
  143. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  144. if code == 200 {
  145. complete(true, result)
  146. } else {
  147. complete(false, result)
  148. }
  149. } else {
  150. complete(false, KMMemberCenterResult(code: 0, msg: "unknown error", result: false))
  151. }
  152. }
  153. task.resume()
  154. // let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/memberResetPassword"
  155. // let params: [String: Any] = ["email": email,
  156. // "verifyCode": verifyCode,
  157. // "appId": "1",
  158. // "password": password]
  159. // KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  160. //
  161. // } completion: { [weak self] (task, responseObject, error) in
  162. // var dic: NSDictionary = [:]
  163. // if error == nil, let responseObject = responseObject as? NSDictionary {
  164. // dic = responseObject
  165. // } else {
  166. // var info = responseObject
  167. // if let error = error {
  168. // if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  169. // info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  170. // }
  171. // }
  172. // dic = (info as? NSDictionary) ?? [:]
  173. // }
  174. //
  175. // let code: Int = dic["code"] as? Int ?? 0
  176. // let result_bool: Bool = dic["result"] as? Bool ?? false
  177. // let message: String = dic["msg"] as? String ?? ""
  178. //
  179. // let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  180. //// let error = NSError(domain: message, code: code)
  181. // if code == 200 {
  182. // complete(true, result)
  183. // } else {
  184. // complete(false, result)
  185. // }
  186. // }
  187. }
  188. /**
  189. @abstract 验证邮箱(重置密码输入邮箱点击下一步时校验邮箱是否注册)
  190. @param email 邮箱
  191. @param complete 回调
  192. */
  193. func emailVerification(email: String, _ complete: @escaping KMMemberCenterComplete) {
  194. let urlString = String(format: "%@/member-system-sso/auth/validEmail?email=%@", configuration.activityBaseURL(), email)
  195. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  196. } completion: { (task, responseObject, error) in
  197. var dic: NSDictionary = [:]
  198. if error == nil, let responseObject = responseObject as? NSDictionary {
  199. dic = responseObject
  200. } else {
  201. var info = responseObject
  202. if let error = error {
  203. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  204. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  205. }
  206. }
  207. dic = (info as? NSDictionary) ?? [:]
  208. }
  209. let code: Int = dic["code"] as? Int ?? 0
  210. let result_bool: Bool = dic["result"] as? Bool ?? false
  211. let message: String = dic["msg"] as? String ?? ""
  212. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  213. if code == 200 {
  214. complete(true, result)
  215. } else {
  216. complete(false, result)
  217. }
  218. }
  219. }
  220. /**
  221. @abstract 获取邮箱验证码
  222. @param action 验证码用途
  223. @param receiver 邮箱号
  224. @param complete 回调
  225. */
  226. func getVerificationCode(action: KMVerificationCodeType, receiver: String, _ complete: @escaping KMMemberCenterComplete) {
  227. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/getVerifyCode"
  228. var actionType = "member_login"
  229. if action == .reset {
  230. actionType = "member_reset_password"
  231. } else if action == .logout {
  232. actionType = "member_log_off"
  233. }
  234. let params: [String: Any] = ["action": actionType,
  235. "type": 0,
  236. "receiver": receiver,
  237. "appId": "1"]
  238. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  239. } completion: { (task, responseObject, error) in
  240. var dic: NSDictionary = [:]
  241. if error == nil, let responseObject = responseObject as? NSDictionary {
  242. dic = responseObject
  243. } else {
  244. var info = responseObject
  245. if let error = error {
  246. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  247. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  248. }
  249. }
  250. dic = (info as? NSDictionary) ?? [:]
  251. }
  252. let code: Int = dic["code"] as? Int ?? 0
  253. let result_bool: Bool = dic["result"] as? Bool ?? false
  254. let message: String = dic["msg"] as? String ?? ""
  255. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  256. if code == 200 {
  257. complete(true, result)
  258. } else {
  259. complete(false, result)
  260. }
  261. }
  262. }
  263. /**
  264. @abstract 实时检查邮件验证码
  265. @param type 验证码用途
  266. @param account 账户
  267. @param code 验证码
  268. @param complete 回调
  269. */
  270. func checkVerificationCode(type: KMVerificationCodeType, account: String, code: String, _ complete: @escaping KMMemberCenterComplete) {
  271. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/isEmailCodeValid"
  272. var actionType = "member_login"
  273. if type == .reset {
  274. actionType = "member_reset_password"
  275. } else if type == .logout {
  276. actionType = "user_log_off"
  277. }
  278. let params: [String: Any] = ["type": actionType,
  279. "account": account,
  280. "code": code,
  281. "appId": "1"]
  282. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  283. } completion: { [weak self] (task, responseObject, error) in
  284. var dic: NSDictionary = [:]
  285. if error == nil, let responseObject = responseObject as? NSDictionary {
  286. dic = responseObject
  287. } else {
  288. var info = responseObject
  289. if let error = error {
  290. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  291. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  292. }
  293. }
  294. dic = (info as? NSDictionary) ?? [:]
  295. }
  296. let code: Int = dic["code"] as? Int ?? 0
  297. let result_bool: Bool = dic["result"] as? Bool ?? false
  298. let message: String = dic["msg"] as? String ?? ""
  299. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  300. if code == 200 {
  301. complete(true, result)
  302. } else {
  303. complete(false, result)
  304. }
  305. }
  306. }
  307. /**
  308. @abstract 邮箱验证码登录 & 密码登录
  309. @param email 邮箱
  310. @param code 验证码/密码
  311. @param type 密码类型(验证码?密码?)
  312. @param complete 回调
  313. */
  314. func emailLogin(email: String, code: String, type: KMSignUpState, _ complete: @escaping KMMemberCenterComplete) {
  315. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/memberLogin"
  316. let uuid: String = GetHardwareUUID() ?? ""
  317. var params: [String: Any] = [:]
  318. if type == .verificationCode {
  319. params = ["email": email,
  320. "code": code,
  321. "appId": "1",
  322. "deviceSign": uuid,
  323. "deviceName": NSFullUserName(),
  324. "model": "mac"]
  325. } else {
  326. params = ["email": email,
  327. "password": code,
  328. "appId": "1",
  329. "deviceSign": uuid,
  330. "deviceName": NSFullUserName(),
  331. "model": "mac"]
  332. }
  333. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  334. // requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  335. } completion: { [weak self] (task, responseObject, error) in
  336. var dic: NSDictionary = [:]
  337. if error == nil, let responseObject = responseObject as? NSDictionary {
  338. dic = responseObject
  339. } else {
  340. var info = responseObject
  341. if let error = error {
  342. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  343. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  344. }
  345. }
  346. dic = (info as? NSDictionary) ?? [:]
  347. }
  348. let code: Int = dic["code"] as? Int ?? 0
  349. let result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  350. let message: String = dic["msg"] as? String ?? ""
  351. var refresh_token = ""
  352. var access_token = ""
  353. var token_type = ""
  354. var expires_in = ""
  355. var scope = ""
  356. if let token = result_dict["refresh_token"] { refresh_token = token as! String }
  357. if let token = result_dict["access_token"] { access_token = token as! String }
  358. if let token = result_dict["token_type"] { token_type = token as! String }
  359. if let token = result_dict["expires_in"] { expires_in = token as! String }
  360. let loginResult = KMMemberLoginResult(refreshToken: refresh_token, accessToken: access_token, tokenType: token_type, expiresIn: expires_in)
  361. let result = KMMemberCenterResult(loginResult: loginResult)
  362. result.code = code
  363. result.msg = message
  364. // let error = NSError(domain: message, code: code)
  365. if code == 200 {
  366. complete(true, result)
  367. } else {
  368. complete(false, result)
  369. }
  370. }
  371. }
  372. /**
  373. @abstract 获取用户登录设备列表
  374. @param email 邮箱
  375. @param complete 回调
  376. */
  377. func getUserDeviceList(email: String, _ complete: @escaping KMMemberCenterComplete) {
  378. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/getLoginDeviceList?email=\(email)&appId=1"
  379. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: [:]) { requestSerializer in
  380. } completion: { [weak self] (task, responseObject, error) in
  381. var dic: NSDictionary = [:]
  382. if error == nil, let responseObject = responseObject as? NSDictionary {
  383. dic = responseObject
  384. } else {
  385. var info = responseObject
  386. if let error = error {
  387. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  388. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  389. }
  390. }
  391. dic = (info as? NSDictionary) ?? [:]
  392. }
  393. let code: Int = dic["code"] as? Int ?? 0
  394. let result_dict: Dictionary = dic["result"] as? Dictionary ?? [:]
  395. let message: String = dic["msg"] as? String ?? ""
  396. let deviceList: [Dictionary] = result_dict["deviceList"] as? [Dictionary] ?? [[:]]
  397. var deviceLists: [KMMemberDeviceList] = []
  398. for dict in deviceList {
  399. var device_id = ""
  400. var productId = ""
  401. var uniqueSn = ""
  402. var deviceName = ""
  403. var id = ""
  404. var model = ""
  405. if let token = dict["device_id"] {
  406. device_id = token as? String ?? ""
  407. }
  408. if let token = dict["productId"] {
  409. productId = token as? String ?? ""
  410. }
  411. if let token = dict["uniqueSn"] {
  412. uniqueSn = token as? String ?? ""
  413. }
  414. if let token = dict["deviceName"] {
  415. deviceName = token as? String ?? ""
  416. }
  417. if let token = dict["model"] {
  418. model = token as? String ?? ""
  419. }
  420. if let token = dict["id"] {
  421. id = token as? String ?? ""
  422. }
  423. let list = KMMemberDeviceList(deviceId: device_id, productId: productId, uniqueSn: uniqueSn, appId: "", device_model:model, deviceName: deviceName)
  424. list.id = id
  425. deviceLists.append(list)
  426. }
  427. var maxDeviceNum = 0
  428. if let token = result_dict["maxDeviceNum"] {
  429. maxDeviceNum = token as! Int
  430. }
  431. let result = KMMemberCenterResult(deviceListResult: deviceLists, maxDeviceNum: maxDeviceNum)
  432. if code == 200 {
  433. complete(true, result)
  434. } else {
  435. complete(false, result)
  436. }
  437. }
  438. }
  439. /**
  440. @abstract 登出用户的指定设备
  441. @param deviceId 设备记录ID
  442. @param complete 回调
  443. */
  444. func logoutDevice(deviceId: String, complete: @escaping KMMemberCenterComplete) {
  445. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/logoutDevice?deviceId=\(deviceId)"
  446. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: [:]) { requestSerializer in
  447. } completion: { [weak self] (task, responseObject, error) in
  448. var dic: NSDictionary = [:]
  449. if error == nil, let responseObject = responseObject as? NSDictionary {
  450. dic = responseObject
  451. } else {
  452. var info = responseObject
  453. if let error = error {
  454. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  455. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  456. }
  457. }
  458. dic = (info as? NSDictionary) ?? [:]
  459. }
  460. let code: Int = dic["code"] as? Int ?? 0
  461. let resultStr: String = dic["result"] as? String ?? ""
  462. let message: String = dic["msg"] as? String ?? ""
  463. let result = KMMemberCenterResult(code: code, msg: message, resultStr: resultStr)
  464. if code == 200 {
  465. complete(true, result)
  466. } else {
  467. complete(false, result)
  468. }
  469. }
  470. }
  471. // MARK: 个人信息模块
  472. /**
  473. @abstract 获取老带新已邀请人数
  474. @param complete 回调
  475. */
  476. func getInviteNum(_ complete: @escaping KMMemberCenterComplete) {
  477. let token: String = KMMemberInfo.shared.access_token
  478. if token == "" {
  479. complete(false, nil)
  480. return
  481. }
  482. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/getInviteNum"
  483. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  484. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  485. } completion: { [weak self] (task, responseObject, error) in
  486. var dic: NSDictionary = [:]
  487. if error == nil, let responseObject = responseObject as? NSDictionary {
  488. dic = responseObject
  489. } else {
  490. var info = responseObject
  491. if let error = error {
  492. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  493. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  494. }
  495. }
  496. dic = (info as? NSDictionary) ?? [:]
  497. }
  498. let code: Int = dic["code"] as? Int ?? 0
  499. let resultInt: Int = dic["result"] as? Int ?? 0
  500. let message: String = dic["msg"] as? String ?? ""
  501. let result = KMMemberCenterResult(code: code, msg: message, resultInt: resultInt)
  502. if code == 200 {
  503. complete(true, result)
  504. } else {
  505. complete(false, result)
  506. }
  507. }
  508. }
  509. /**
  510. @abstract 获取用户个人信息
  511. @param complete 回调
  512. */
  513. func userInfo(_ complete: @escaping KMMemberCenterComplete) {
  514. let token: String = KMMemberInfo.shared.access_token
  515. if token == "" {
  516. complete(false, nil)
  517. return
  518. }
  519. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/info"
  520. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  521. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  522. } completion: { [weak self] (task, responseObject, error) in
  523. var dic: NSDictionary = [:]
  524. if error == nil, let responseObject = responseObject as? NSDictionary {
  525. dic = responseObject
  526. } else {
  527. var info = responseObject
  528. if let error = error {
  529. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  530. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  531. }
  532. }
  533. dic = (info as? NSDictionary) ?? [:]
  534. }
  535. let code: Int = dic["code"] as? Int ?? 0
  536. let result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  537. let message: String = dic["msg"] as? String ?? ""
  538. var userId = ""
  539. var email = ""
  540. var fullName = ""
  541. var activeVIP = KMMemberUserInfo()
  542. var aiPoint = 0
  543. var validFlag = ""
  544. var isHaveAIDiscount = ""
  545. var canTrail = ""
  546. if let token = result_dict["id"] { userId = token as! String }
  547. if let token = result_dict["email"] { email = token as! String }
  548. if let token = result_dict["fullName"] { fullName = token as! String }
  549. let activeVIP_dict: NSDictionary = result_dict["activeVIP"] as? NSDictionary ?? [:]
  550. var activeVIP_id = ""
  551. var activeVIP_userId = ""
  552. var activeVIP_status = 0
  553. var activeVIP_endDate = ""
  554. var activeVIP_payType = 0
  555. var activeVIP_productName = ""
  556. var activeVIP_levels = ""
  557. var activeVIP_platforms = ""
  558. var activeVIP_point = 0
  559. var activeVIP_maxDeviceNum = 0
  560. var activeVIP_paymentModel = ""
  561. var activeVIP_isAi = ""
  562. var activeVIP_cycle = 0
  563. if let token = activeVIP_dict["id"] as? String { activeVIP_id = token }
  564. if let token = activeVIP_dict["userId"] as? String { activeVIP_userId = token }
  565. if let token = activeVIP_dict["status"] as? Int { activeVIP_status = token }
  566. if let token = activeVIP_dict["endDate"] as? String {
  567. // 创建一个日期格式化器
  568. let dateFormatter = DateFormatter()
  569. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  570. // 将字符串转换为日期对象
  571. if let date = dateFormatter.date(from: token) {
  572. // 创建一个新的日期格式化器,用于只显示天
  573. let dayFormatter = DateFormatter()
  574. dayFormatter.dateFormat = "yyyy-MM-dd" // 只保留年月日部分
  575. activeVIP_endDate = dayFormatter.string(from: date)
  576. }
  577. }
  578. if let token = activeVIP_dict["payType"] as? Int { activeVIP_payType = token }
  579. if let token = activeVIP_dict["productName"] as? String { activeVIP_productName = token }
  580. if let token = activeVIP_dict["levels"] as? String { activeVIP_levels = token }
  581. if let token = activeVIP_dict["platforms"] as? String { activeVIP_platforms = token }
  582. if let token = activeVIP_dict["point"] as? Int { activeVIP_point = token }
  583. if let token = activeVIP_dict["maxDeviceNum"] as? Int { activeVIP_maxDeviceNum = token }
  584. if let token = activeVIP_dict["paymentModel"] as? String { activeVIP_paymentModel = token }
  585. if let token = activeVIP_dict["isAi"] as? String { activeVIP_isAi = token }
  586. if let token = activeVIP_dict["cycle"] as? Int { activeVIP_cycle = token }
  587. let userInfoActiveVIP = KMMemberUserInfoActiveVIP(id: activeVIP_id, userId: activeVIP_userId, status: activeVIP_status, endDate: activeVIP_endDate, payType: activeVIP_payType, productName: activeVIP_productName, levels: activeVIP_levels, platforms: activeVIP_platforms, point: activeVIP_point, maxDeviceNum: activeVIP_maxDeviceNum, paymentModel: activeVIP_paymentModel, isAi: activeVIP_isAi, cycle: activeVIP_cycle)
  588. let activeAI_dict: NSDictionary = result_dict["activeAI"] as? NSDictionary ?? [:]
  589. var activeAI_id = ""
  590. var activeAI_userId = ""
  591. var activeAI_status = 0
  592. var activeAI_endDate = ""
  593. var activeAI_payType = 0
  594. var activeAI_productName = ""
  595. var activeAI_levels = ""
  596. var activeAI_platforms = ""
  597. var activeAI_point = 0
  598. var activeAI_maxDeviceNum = 0
  599. var activeAI_paymentModel = ""
  600. var activeAI_isAi = ""
  601. var activeAI_cycle = 0
  602. if let token = activeAI_dict["id"] as? String { activeAI_id = token }
  603. if let token = activeAI_dict["userId"] as? String { activeAI_userId = token }
  604. if let token = activeAI_dict["status"] as? Int { activeAI_status = token }
  605. if let token = activeAI_dict["endDate"] as? String { activeAI_endDate = token }
  606. if let token = activeAI_dict["payType"] as? Int { activeAI_payType = token }
  607. if let token = activeAI_dict["productName"] as? String { activeAI_productName = token }
  608. if let token = activeAI_dict["levels"] as? String { activeAI_levels = token }
  609. if let token = activeAI_dict["platforms"] as? String { activeAI_platforms = token }
  610. if let token = activeAI_dict["point"] as? Int { activeAI_point = token }
  611. if let token = activeAI_dict["maxDeviceNum"] as? Int { activeAI_maxDeviceNum = token }
  612. if let token = activeAI_dict["paymentModel"] as? String { activeAI_paymentModel = token }
  613. if let token = activeAI_dict["isAi"] as? String { activeAI_isAi = token }
  614. if let token = activeAI_dict["cycle"] as? Int { activeAI_cycle = token }
  615. let userInfoActiveAI = KMMemberUserInfoActiveVIP(id: activeAI_id, userId: activeAI_userId, status: activeAI_status, endDate: activeAI_endDate, payType: activeAI_payType, productName: activeAI_productName, levels: activeAI_levels, platforms: activeAI_platforms, point: activeAI_point, maxDeviceNum: activeAI_maxDeviceNum, paymentModel: activeAI_paymentModel, isAi: activeAI_isAi, cycle: activeAI_cycle)
  616. if let token = result_dict["aiPoint"] { aiPoint = token as! Int }
  617. if let token = result_dict["validFlag"] { validFlag = token as! String }
  618. if let token = result_dict["isHaveAIDiscount"] { isHaveAIDiscount = token as! String }
  619. if let token = result_dict["canTrail"] { canTrail = token as! String }
  620. let userInfo = KMMemberUserInfo(id: userId, email: email, fullName: fullName, activeVIP: [userInfoActiveVIP], activeAI: [userInfoActiveAI], aiPoint: aiPoint, validFlag: validFlag, isHaveAIDiscount: isHaveAIDiscount, canTrail: canTrail)
  621. if let data = result_dict["avatarUrl"] as? String {
  622. userInfo.avatarUrl = data
  623. }
  624. let result = KMMemberCenterResult(code: code, msg: message, userInfo: userInfo)
  625. if code == 200 {
  626. complete(true, result)
  627. } else {
  628. complete(false, result)
  629. }
  630. }
  631. }
  632. /**
  633. @abstract 退出登录
  634. @param complete 回调
  635. */
  636. func userLogout(_ complete: @escaping KMMemberCenterComplete) {
  637. let token: String = KMMemberInfo.shared.access_token
  638. if token == "" {
  639. complete(false, nil)
  640. return
  641. }
  642. let uuid: String = GetHardwareUUID() ?? ""
  643. let urlString = String(format: "%@/member-system-sso/user/logout?deviceSign=%@&appId=1", configuration.activityBaseURL(),uuid)
  644. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  645. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  646. } completion: { [weak self] (task, responseObject, error) in
  647. var dic: NSDictionary = [:]
  648. if error == nil, let responseObject = responseObject as? NSDictionary {
  649. dic = responseObject
  650. } else {
  651. var info = responseObject
  652. if let error = error {
  653. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  654. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  655. }
  656. }
  657. dic = (info as? NSDictionary) ?? [:]
  658. }
  659. let code: Int = dic["code"] as? Int ?? 0
  660. let result_bool: Bool = dic["result"] as? Bool ?? false
  661. let message: String = dic["msg"] as? String ?? ""
  662. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  663. if code == 200 {
  664. complete(true, result)
  665. } else {
  666. complete(false, result)
  667. }
  668. }
  669. }
  670. /**
  671. @abstract 获取用户当前会员情况
  672. @param complete 回调
  673. */
  674. func userRightList(_ complete: @escaping KMMemberCenterComplete) {
  675. let token: String = KMMemberInfo.shared.access_token
  676. if token == "" {
  677. complete(false, nil)
  678. return
  679. }
  680. let urlString = configuration.activityBaseURL() + "/member-system-sso//user/rightList"
  681. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  682. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  683. } completion: { [weak self] (task, responseObject, error) in
  684. var dic: NSDictionary = [:]
  685. if error == nil, let responseObject = responseObject as? NSDictionary {
  686. dic = responseObject
  687. } else {
  688. var info = responseObject
  689. if let error = error {
  690. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  691. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  692. }
  693. }
  694. dic = (info as? NSDictionary) ?? [:]
  695. }
  696. let code: Int = dic["code"] as? Int ?? 0
  697. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  698. let message: String = dic["msg"] as? String ?? ""
  699. var results: [KMMemberUserInfoActiveVIP] = []
  700. for dict1 in result_Array {
  701. var activeVIP_id = ""
  702. var activeVIP_userId = ""
  703. var activeVIP_status = 0
  704. var activeVIP_endDate = ""
  705. var activeVIP_payType = 0
  706. var activeVIP_productName = ""
  707. var activeVIP_levels = ""
  708. var activeVIP_platforms = ""
  709. var activeVIP_point = 0
  710. var activeVIP_maxDeviceNum = 0
  711. var activeVIP_paymentModel = ""
  712. var activeVIP_isAi = ""
  713. if let token = dict1["id"] { activeVIP_id = token as! String }
  714. if let token = dict1["userId"] { activeVIP_userId = token as! String }
  715. if let token = dict1["status"] { activeVIP_status = token as! Int }
  716. if let token = dict1["endDate"] { activeVIP_endDate = token as! String }
  717. if let token = dict1["payType"] { activeVIP_payType = token as! Int }
  718. if let token = dict1["productName"] { activeVIP_productName = token as! String }
  719. if let token = dict1["levels"] { activeVIP_levels = token as! String }
  720. if let token = dict1["platforms"] { activeVIP_platforms = token as! String }
  721. if let token = dict1["point"] { activeVIP_point = token as! Int }
  722. if let token = dict1["maxDeviceNum"] { activeVIP_maxDeviceNum = token as! Int }
  723. if let token = dict1["paymentModel"] { activeVIP_paymentModel = token as! String }
  724. if let token = dict1["isAi"] { activeVIP_isAi = token as! String }
  725. let userInfoActiveVIP = KMMemberUserInfoActiveVIP(id: activeVIP_id, userId: activeVIP_userId, status: activeVIP_status, endDate: activeVIP_endDate, payType: activeVIP_payType, productName: activeVIP_productName, levels: activeVIP_levels, platforms: activeVIP_platforms, point: activeVIP_point, maxDeviceNum: activeVIP_maxDeviceNum, paymentModel: activeVIP_paymentModel, isAi: activeVIP_isAi)
  726. results.append(userInfoActiveVIP)
  727. }
  728. let result = KMMemberCenterResult(code: code, msg: message, activeVIPs: results)
  729. if code == 200 {
  730. complete(true, result)
  731. } else {
  732. complete(false, result)
  733. }
  734. }
  735. }
  736. /**
  737. @abstract 用户手动注销
  738. @param code 邮箱验证码
  739. @param complete 回调
  740. */
  741. func userLogOffForUser(code: String, _ complete: @escaping KMMemberCenterComplete) {
  742. let token: String = KMMemberInfo.shared.access_token
  743. if token == "" {
  744. complete(false, nil)
  745. return
  746. }
  747. let urlString = String(format: "%@/member-system-sso/user/logOffForUser?code=%@&appId=1", configuration.activityBaseURL(), code)
  748. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  749. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  750. } completion: { [weak self] (task, responseObject, error) in
  751. var dic: NSDictionary = [:]
  752. if error == nil, let responseObject = responseObject as? NSDictionary {
  753. dic = responseObject
  754. } else {
  755. var info = responseObject
  756. if let error = error {
  757. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  758. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  759. }
  760. }
  761. dic = (info as? NSDictionary) ?? [:]
  762. }
  763. let code: Int = dic["code"] as? Int ?? 0
  764. let message: String = dic["msg"] as? String ?? ""
  765. let result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  766. var currentTime = ""
  767. var logOffTime = ""
  768. if let token = result_dict["currentTime"] { currentTime = token as! String }
  769. if let token = result_dict["logOffTime"] { logOffTime = token as! String }
  770. let logOff = KMMemberLogOff(currentTime: currentTime, logOffTime: logOffTime)
  771. let result = KMMemberCenterResult(code: code, msg: message, logOff: logOff)
  772. if code == 200 {
  773. complete(true, result)
  774. } else {
  775. complete(false, result)
  776. }
  777. }
  778. }
  779. /**
  780. @abstract 撤销注销申请
  781. @param complete 回调
  782. */
  783. func userRevokeCancel(_ complete: @escaping KMMemberCenterComplete) {
  784. let token: String = KMMemberInfo.shared.access_token
  785. if token == "" {
  786. complete(false, nil)
  787. return
  788. }
  789. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/revokeCancel"
  790. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  791. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  792. } completion: { [weak self] (task, responseObject, error) in
  793. var dic: NSDictionary = [:]
  794. if error == nil, let responseObject = responseObject as? NSDictionary {
  795. dic = responseObject
  796. } else {
  797. var info = responseObject
  798. if let error = error {
  799. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  800. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  801. }
  802. }
  803. dic = (info as? NSDictionary) ?? [:]
  804. }
  805. let code: Int = dic["code"] as? Int ?? 0
  806. let message: String = dic["msg"] as? String ?? ""
  807. let result_Bool: Bool = dic["result"] as? Bool ?? false
  808. let result = KMMemberCenterResult(code: code, msg: message, result: result_Bool)
  809. if code == 200 {
  810. complete(true, result)
  811. } else {
  812. complete(false, result)
  813. }
  814. }
  815. }
  816. // MARK: 商品模块
  817. /**
  818. @abstract 获取上架中的产品
  819. @param isEducation 是否教育优惠
  820. @param complete 回调
  821. */
  822. func getListingProducts(isEducation: Int, _ complete: @escaping KMMemberProductComplete) {
  823. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getListingProducts"
  824. var platformId = "2"
  825. #if VERSION_FREE
  826. #if VERSION_DMG
  827. // DMG
  828. platformId = "2"
  829. #else
  830. // AppStore 免费版本
  831. #endif
  832. #else
  833. // AppStore 付费版
  834. platformId = "5"
  835. #endif
  836. let params: [String: Any] = ["isEducation": NSNumber(value: Int32(isEducation)),
  837. "platformId": platformId]
  838. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  839. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  840. } completion: { [weak self] (task, responseObject, error) in
  841. var dic: NSDictionary = [:]
  842. if error == nil, let responseObject = responseObject as? NSDictionary {
  843. dic = responseObject
  844. } else {
  845. var info = responseObject
  846. if let error = error {
  847. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  848. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  849. }
  850. }
  851. dic = (info as? NSDictionary) ?? [:]
  852. }
  853. let requestCode: Int = dic["code"] as? Int ?? 0
  854. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  855. let message: String = dic["msg"] as? String ?? ""
  856. var results: [KMListingProductsModel] = []
  857. for dict1 in result_Array {
  858. var id = ""
  859. var productName = ""
  860. var maxDeviceNum = 0
  861. var levels = ""
  862. var platforms = ""
  863. var productLineId = 0
  864. var paymentModel = 0
  865. var cycle = 0
  866. var code = ""
  867. var price = NSNumber(value: 0.0)
  868. var displayPrice = NSNumber(value: 0.0)
  869. var upgradePrice = NSNumber(value: 0.0)
  870. var cnyPrice = NSNumber(value: 0.0)
  871. var displayCnyPrice = NSNumber(value: 0.0)
  872. var priceString:String?
  873. var displayPriceString:String?
  874. var upgradePriceString:String?
  875. var cnyPriceString:String?
  876. var displayCnyPriceString:String?
  877. if let token = dict1["id"] { id = token as? String ?? id }
  878. if let token = dict1["productName"] { productName = token as? String ?? productName }
  879. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as? Int ?? maxDeviceNum }
  880. if let token = dict1["levels"] { levels = token as? String ?? levels }
  881. if let token = dict1["platforms"] { platforms = token as? String ?? platforms }
  882. if let token = dict1["productLineId"] { productLineId = token as? Int ?? productLineId }
  883. if let token = dict1["paymentModel"] { paymentModel = token as? Int ?? paymentModel }
  884. if let token = dict1["cycle"] { cycle = token as? Int ?? cycle }
  885. if let token = dict1["code"] { code = token as? String ?? code }
  886. if let token = dict1["price"] as? NSNumber {
  887. let string = KMFormatNSNumber(token, to: 2)
  888. priceString = string
  889. price = token
  890. }
  891. if let token = dict1["displayPrice"] as? NSNumber {
  892. let string = KMFormatNSNumber(token, to: 2)
  893. displayPriceString = string
  894. displayPrice = token
  895. }
  896. if let token = dict1["upgradePrice"] as? NSNumber {
  897. let string = KMFormatNSNumber(token, to: 2)
  898. upgradePriceString = string
  899. upgradePrice = token
  900. }
  901. if let token = dict1["cnyPrice"] as? NSNumber {
  902. let string = KMFormatNSNumber(token, to: 2)
  903. cnyPriceString = string
  904. cnyPrice = token
  905. }
  906. if let token = dict1["cnyDisplayPrice"] as? NSNumber {
  907. let string = KMFormatNSNumber(token, to: 2)
  908. displayCnyPriceString = string
  909. displayCnyPrice = token
  910. }
  911. let products = KMListingProductsModel(id: id, productName: productName, price: priceString, maxDeviceNum: maxDeviceNum, displayPrice: displayPriceString, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, cnyPrice: cnyPriceString, displayCnyPrice: displayCnyPriceString, upgradePrice: upgradePriceString, code: code)
  912. products.individualPriceString = priceString
  913. products.individualCnyPriceString = cnyPriceString
  914. results.append(products)
  915. }
  916. let result = KMMemberProductResult(code: requestCode, msg: message, listingProducts: results)
  917. if requestCode == 200 {
  918. complete(true, result)
  919. } else {
  920. complete(false, result)
  921. }
  922. }
  923. } else {
  924. print("Invalid URL")
  925. complete(false, nil)
  926. }
  927. }
  928. /**
  929. @abstract 获取已登录用户购买商品价格
  930. @param productId 购买的产品id
  931. @param isEducation 是否教育优惠
  932. @param complete 回调
  933. */
  934. func getProductPriceForBuy(productId: String, isEducation: Int, userId: String, _ complete: @escaping KMMemberProductComplete) {
  935. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getProductPriceForBuy"
  936. let params: [String: Any] = ["productId": productId,
  937. "isEducation": isEducation,
  938. "userId": userId]
  939. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  940. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  941. } completion: { [weak self] (task, responseObject, error) in
  942. var dic: NSDictionary = [:]
  943. if error == nil, let responseObject = responseObject as? NSDictionary {
  944. dic = responseObject
  945. } else {
  946. var info = responseObject
  947. if let error = error {
  948. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  949. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  950. }
  951. }
  952. dic = (info as? NSDictionary) ?? [:]
  953. }
  954. let requestCode: Int = dic["code"] as? Int ?? 0
  955. let result_Array: [NSDictionary] = [dic["result"]] as? [NSDictionary] ?? [[:]]
  956. let message: String = dic["msg"] as? String ?? ""
  957. var results: [KMListingProductsModel] = []
  958. for dict1 in result_Array {
  959. var id = ""
  960. var productName = ""
  961. var maxDeviceNum = 0
  962. var levels = ""
  963. var platforms = ""
  964. var productLineId = 0
  965. var paymentModel = 0
  966. var cycle = 0
  967. var code = ""
  968. var price = NSNumber(value: 0.0)
  969. var displayPrice = NSNumber(value: 0.0)
  970. var upgradePrice = NSNumber(value: 0.0)
  971. var cnyPrice = NSNumber(value: 0.0)
  972. var displayCnyPrice = NSNumber(value: 0.0)
  973. var priceString:String?
  974. var displayPriceString:String?
  975. var upgradePriceString:String?
  976. var cnyPriceString:String?
  977. var displayCnyPriceString:String?
  978. if let token = dict1["id"] { id = token as? String ?? id }
  979. if let token = dict1["productName"] { productName = token as? String ?? productName }
  980. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as? Int ?? maxDeviceNum }
  981. if let token = dict1["levels"] { levels = token as? String ?? levels }
  982. if let token = dict1["platforms"] { platforms = token as? String ?? platforms }
  983. if let token = dict1["productLineId"] { productLineId = token as? Int ?? productLineId }
  984. if let token = dict1["paymentModel"] { paymentModel = token as? Int ?? paymentModel }
  985. if let token = dict1["cycle"] { cycle = token as? Int ?? cycle }
  986. if let token = dict1["code"] { code = token as? String ?? code }
  987. if let token = dict1["price"] as? NSNumber {
  988. let string = KMFormatNSNumber(token, to: 2)
  989. priceString = string
  990. price = token
  991. }
  992. if let token = dict1["displayPrice"] as? NSNumber {
  993. let string = KMFormatNSNumber(token, to: 2)
  994. displayPriceString = string
  995. displayPrice = token
  996. }
  997. if let token = dict1["upgradePrice"] as? NSNumber {
  998. let string = KMFormatNSNumber(token, to: 2)
  999. upgradePriceString = string
  1000. upgradePrice = token
  1001. }
  1002. if let token = dict1["cnyPrice"] as? NSNumber {
  1003. let string = KMFormatNSNumber(token, to: 2)
  1004. cnyPriceString = string
  1005. cnyPrice = token
  1006. }
  1007. if let token = dict1["cnyDisplayPrice"] as? NSNumber {
  1008. let string = KMFormatNSNumber(token, to: 2)
  1009. displayCnyPriceString = string
  1010. displayCnyPrice = token
  1011. }
  1012. let products = KMListingProductsModel(id: id, productName: productName, price: priceString, maxDeviceNum: maxDeviceNum, displayPrice: displayPriceString, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, cnyPrice: cnyPriceString, displayCnyPrice: displayCnyPriceString, upgradePrice: upgradePriceString, code: code)
  1013. products.individualPriceString = priceString
  1014. products.individualCnyPriceString = cnyPriceString
  1015. results.append(products)
  1016. }
  1017. let result = KMMemberProductResult(code: requestCode, msg: message, listingProducts: results)
  1018. if requestCode == 200 {
  1019. complete(true, result)
  1020. } else {
  1021. complete(false, result)
  1022. }
  1023. }
  1024. } else {
  1025. print("Invalid URL")
  1026. complete(false, nil)
  1027. }
  1028. }
  1029. /**
  1030. @abstract 获取批量阶段购买价格
  1031. @param productId 购买的产品id
  1032. @param num 批量个数
  1033. @param complete 回调
  1034. */
  1035. func getBatchProductPrice(productId: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1036. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getBatchProductPrice"
  1037. let params: [String: Any] = ["productId": productId,
  1038. "num": NSNumber(value: Int32(num))]
  1039. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1040. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1041. } completion: { [weak self] (task, responseObject, error) in
  1042. var dic: NSDictionary = [:]
  1043. if error == nil, let responseObject = responseObject as? NSDictionary {
  1044. dic = responseObject
  1045. } else {
  1046. var info = responseObject
  1047. if let error = error {
  1048. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1049. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1050. }
  1051. }
  1052. dic = (info as? NSDictionary) ?? [:]
  1053. }
  1054. let requestCode: Int = dic["code"] as? Int ?? 0
  1055. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1056. let message: String = dic["msg"] as? String ?? ""
  1057. var productId = ""
  1058. var totalPrice = ""
  1059. var price = ""
  1060. var batchPrice = ""
  1061. var cnyPrice = ""
  1062. var cnyBatchPrice = ""
  1063. var cnyTotalPrice = ""
  1064. if let token = result["productId"] { productId = token as? String ?? productId}
  1065. if let token = result["totalPrice"] { totalPrice = token as? String ?? totalPrice}
  1066. if let token = result["price"] { price = token as? String ?? price}
  1067. if let token = result["batchPrice"] { batchPrice = token as? String ?? batchPrice}
  1068. if let token = result["cnyPrice"] { cnyPrice = token as? String ?? cnyPrice}
  1069. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as? String ?? cnyBatchPrice}
  1070. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as? String ?? cnyTotalPrice}
  1071. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1072. let result1 = KMMemberProductResult(code: requestCode, msg: message, batchProductPrice: products)
  1073. if requestCode == 200 {
  1074. complete(true, result1)
  1075. } else {
  1076. complete(false, result1)
  1077. }
  1078. }
  1079. } else {
  1080. print("Invalid URL")
  1081. }
  1082. }
  1083. /**
  1084. @abstract 验证商品优惠券,返回商品优惠后价格
  1085. @param productId 购买的产品id
  1086. @param userId 用户ID
  1087. @param code 优惠券
  1088. @param complete 回调
  1089. */
  1090. func checkCoupon(productId: String, userId: String, code: String, _ complete: @escaping KMMemberProductComplete) {
  1091. let token: String = KMMemberInfo.shared.access_token
  1092. if token == "" {
  1093. complete(false, nil)
  1094. return
  1095. }
  1096. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkCoupon"
  1097. let params: [String: Any] = ["productId": productId,
  1098. "userId": userId,
  1099. "code": code]
  1100. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1101. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1102. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1103. } completion: { [weak self] (task, responseObject, error) in
  1104. var dic: NSDictionary = [:]
  1105. if error == nil, let responseObject = responseObject as? NSDictionary {
  1106. dic = responseObject
  1107. } else {
  1108. var info = responseObject
  1109. if let error = error {
  1110. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1111. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1112. }
  1113. }
  1114. dic = (info as? NSDictionary) ?? [:]
  1115. }
  1116. let requestCode: Int = dic["code"] as? Int ?? 0
  1117. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1118. let message: String = dic["msg"] as? String ?? ""
  1119. var id = ""
  1120. var productName = ""
  1121. var maxDeviceNum = 0
  1122. var levels = ""
  1123. var platforms = ""
  1124. var productLineId = 0
  1125. var paymentModel = 0
  1126. var cycle = 0
  1127. var code = ""
  1128. var price = NSNumber(value: 0.0)
  1129. var displayPrice = NSNumber(value: 0.0)
  1130. var upgradePrice = NSNumber(value: 0.0)
  1131. var cnyPrice = NSNumber(value: 0.0)
  1132. var displayCnyPrice = NSNumber(value: 0.0)
  1133. var priceString:String?
  1134. var displayPriceString:String?
  1135. var upgradePriceString:String?
  1136. var cnyPriceString:String?
  1137. var displayCnyPriceString:String?
  1138. if let token = result["id"] { id = token as? String ?? id }
  1139. if let token = result["productName"] { productName = token as? String ?? productName }
  1140. if let token = result["maxDeviceNum"] { maxDeviceNum = token as? Int ?? maxDeviceNum }
  1141. if let token = result["levels"] { levels = token as? String ?? levels }
  1142. if let token = result["platforms"] { platforms = token as? String ?? platforms }
  1143. if let token = result["productLineId"] { productLineId = token as? Int ?? productLineId }
  1144. if let token = result["paymentModel"] { paymentModel = token as? Int ?? paymentModel }
  1145. if let token = result["cycle"] { cycle = token as? Int ?? cycle }
  1146. if let token = result["code"] { code = token as? String ?? code }
  1147. if let token = result["price"] as? NSNumber {
  1148. let string = KMFormatNSNumber(token, to: 2)
  1149. priceString = string
  1150. price = token
  1151. }
  1152. if let token = result["displayPrice"] as? NSNumber {
  1153. let string = KMFormatNSNumber(token, to: 2)
  1154. displayPriceString = string
  1155. displayPrice = token
  1156. }
  1157. if let token = result["upgradePrice"] as? NSNumber {
  1158. let string = KMFormatNSNumber(token, to: 2)
  1159. upgradePriceString = string
  1160. upgradePrice = token
  1161. }
  1162. if let token = result["cnyPrice"] as? NSNumber {
  1163. let string = KMFormatNSNumber(token, to: 2)
  1164. cnyPriceString = string
  1165. cnyPrice = token
  1166. }
  1167. if let token = result["cnyDisplayPrice"] as? NSNumber {
  1168. let string = KMFormatNSNumber(token, to: 2)
  1169. displayCnyPriceString = string
  1170. displayCnyPrice = token
  1171. }
  1172. let products = KMCheckCouponModel(id: id, productName: productName, price: priceString, maxDeviceNum: maxDeviceNum, displayPrice: displayPriceString, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, cnyPrice: cnyPriceString, displayCnyPrice: displayCnyPriceString, upgradePrice: upgradePriceString, code: code)
  1173. let result1 = KMMemberProductResult(code: requestCode, msg: message, checkCoupon: products)
  1174. if requestCode == 200 {
  1175. complete(true, result1)
  1176. } else {
  1177. complete(false, result1)
  1178. }
  1179. }
  1180. } else {
  1181. print("Invalid URL")
  1182. complete(false, nil)
  1183. }
  1184. }
  1185. /**
  1186. @abstract 检查邮箱教育优惠资格
  1187. @param email 邮箱
  1188. @param complete 回调
  1189. */
  1190. func checkEducation(email: String, _ complete: @escaping KMMemberProductComplete) {
  1191. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkEducation"
  1192. let params: [String: Any] = ["email": email]
  1193. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1194. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1195. } completion: { [weak self] (task, responseObject, error) in
  1196. var dic: NSDictionary = [:]
  1197. if error == nil, let responseObject = responseObject as? NSDictionary {
  1198. dic = responseObject
  1199. } else {
  1200. var info = responseObject
  1201. if let error = error {
  1202. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1203. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1204. }
  1205. }
  1206. dic = (info as? NSDictionary) ?? [:]
  1207. }
  1208. let requestCode: Int = dic["code"] as? Int ?? 0
  1209. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1210. let message: String = dic["msg"] as? String ?? ""
  1211. var productId = ""
  1212. var totalPrice = ""
  1213. var price = ""
  1214. var batchPrice = ""
  1215. var cnyPrice = ""
  1216. var cnyBatchPrice = ""
  1217. var cnyTotalPrice = ""
  1218. if let token = result["productId"] { productId = token as? String ?? productId}
  1219. if let token = result["totalPrice"] { totalPrice = token as? String ?? totalPrice}
  1220. if let token = result["price"] { price = token as? String ?? price}
  1221. if let token = result["batchPrice"] { batchPrice = token as? String ?? batchPrice}
  1222. if let token = result["cnyPrice"] { cnyPrice = token as? String ?? cnyPrice}
  1223. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as? String ?? cnyBatchPrice}
  1224. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as? String ?? cnyTotalPrice}
  1225. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1226. let result1 = KMMemberProductResult(code: requestCode, msg: message, batchProductPrice: products)
  1227. if requestCode == 200 {
  1228. complete(true, result1)
  1229. } else {
  1230. complete(false, result1)
  1231. }
  1232. }
  1233. } else {
  1234. print("Invalid URL")
  1235. complete(false, nil)
  1236. }
  1237. }
  1238. // MARK: 个人中心模块
  1239. // MARK: 订单模块
  1240. /**
  1241. @abstract 支付后查询订单状态
  1242. @param orderId 订单id
  1243. @param complete 回调
  1244. */
  1245. func getStateByOrderId(orderId: String, _ complete: @escaping KMMemberRequestInfoComplete) {
  1246. let token: String = KMMemberInfo.shared.access_token
  1247. if token == "" {
  1248. complete(false, nil)
  1249. return
  1250. }
  1251. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getStateByOrderId"
  1252. let params: [String: Any] = ["orderId": orderId]
  1253. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1254. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1255. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1256. } completion: { [weak self] (task, responseObject, error) in
  1257. var dic: NSDictionary = [:]
  1258. if error == nil, let responseObject = responseObject as? NSDictionary {
  1259. dic = responseObject
  1260. } else {
  1261. var info = responseObject
  1262. if let error = error {
  1263. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1264. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1265. }
  1266. }
  1267. dic = (info as? NSDictionary) ?? [:]
  1268. }
  1269. let requestCode: Int = dic["code"] as? Int ?? 0
  1270. let requestDic: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1271. let status:String = requestDic["status"] as? String ?? ""
  1272. if requestCode == 200{
  1273. complete(true, dic)
  1274. } else {
  1275. complete(false, dic)
  1276. }
  1277. }
  1278. } else {
  1279. print("Invalid URL")
  1280. complete(false, nil)
  1281. }
  1282. }
  1283. /**
  1284. @abstract 创建订单(买断)
  1285. @param productId 产品id
  1286. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1287. @param price 价格
  1288. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1289. @param couponCode 优惠卷
  1290. @param num 批量购买数量
  1291. @param complete 回调
  1292. */
  1293. func createOrder(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1294. let token: String = KMMemberInfo.shared.access_token
  1295. if token == "" {
  1296. complete(false, nil)
  1297. return
  1298. }
  1299. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createOrder"
  1300. let params: [String: Any] = ["productId": productId,
  1301. "paymentMethod": paymentMethod,
  1302. "price": price,
  1303. "discountFlag": discountFlag,
  1304. "couponCode": couponCode,
  1305. "num": num]
  1306. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1307. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1308. } completion: { [weak self] (task, responseObject, error) in
  1309. var dic: NSDictionary = [:]
  1310. if error == nil, let responseObject = responseObject as? NSDictionary {
  1311. dic = responseObject
  1312. } else {
  1313. var info = responseObject
  1314. if let error = error {
  1315. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1316. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1317. }
  1318. }
  1319. dic = (info as? NSDictionary) ?? [:]
  1320. }
  1321. let requestCode: Int = dic["code"] as? Int ?? 0
  1322. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1323. let message: String = dic["msg"] as? String ?? ""
  1324. var thirdOrderNo = ""
  1325. var orderId = ""
  1326. var payHref = ""
  1327. var qrCode = ""
  1328. if requestCode == 200 {
  1329. if let token = result["thirdOrderNo"] { thirdOrderNo = token as? String ?? thirdOrderNo}
  1330. if let token = result["orderId"] { orderId = token as? String ?? orderId}
  1331. if let token = result["payHref"] { payHref = token as? String ?? payHref}
  1332. if let token = result["qrcode"] { qrCode = token as? String ?? qrCode}
  1333. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode)
  1334. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: products)
  1335. complete(true, result1)
  1336. } else {
  1337. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode))
  1338. complete(false, result1)
  1339. }
  1340. }
  1341. }
  1342. /**
  1343. @abstract 创建订单(订阅)
  1344. @param productId 产品id
  1345. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1346. @param price 价格
  1347. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1348. @param couponCode 优惠卷
  1349. @param num 批量购买数量
  1350. @param complete 回调
  1351. */
  1352. func createSubscriber(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1353. let token: String = KMMemberInfo.shared.access_token
  1354. if token == "" {
  1355. complete(false, nil)
  1356. return
  1357. }
  1358. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createSubscription"
  1359. let params: [String: Any] = ["productId": productId,
  1360. "paymentMethod": paymentMethod,
  1361. "price": price,
  1362. "discountFlag": discountFlag,
  1363. "couponCode": couponCode,
  1364. "num": num]
  1365. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1366. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1367. } completion: { [weak self] (task, responseObject, error) in
  1368. var dic: NSDictionary = [:]
  1369. if error == nil, let responseObject = responseObject as? NSDictionary {
  1370. dic = responseObject
  1371. } else {
  1372. var info = responseObject
  1373. if let error = error {
  1374. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1375. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1376. }
  1377. }
  1378. dic = (info as? NSDictionary) ?? [:]
  1379. }
  1380. let requestCode: Int = dic["code"] as? Int ?? 0
  1381. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1382. let message: String = dic["msg"] as? String ?? ""
  1383. var thirdOrderNo = ""
  1384. var orderId = ""
  1385. var payHref = ""
  1386. var qrCode = ""
  1387. if requestCode == 200 {
  1388. if let token = result["thirdOrderNo"] { thirdOrderNo = token as? String ?? thirdOrderNo}
  1389. if let token = result["orderId"] { orderId = token as? String ?? orderId}
  1390. if let token = result["payHref"] { payHref = token as? String ?? payHref}
  1391. if let token = result["qrcode"] { qrCode = token as? String ?? qrCode}
  1392. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode)
  1393. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: products)
  1394. complete(true, result1)
  1395. } else {
  1396. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode))
  1397. complete(false, result1)
  1398. }
  1399. }
  1400. }
  1401. /**
  1402. @abstract 获取订单列表
  1403. @param orderStatus 0全部1待支付
  1404. @param complete 回调
  1405. */
  1406. func getOrderListByStatus(orderStatus: Int, _ complete: @escaping KMMemberRequestInfoComplete) {
  1407. let token: String = KMMemberInfo.shared.access_token
  1408. if token == "" {
  1409. complete(false, nil)
  1410. return
  1411. }
  1412. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getOrderListByStatus"
  1413. let params: [String: Any] = ["orderStatus": orderStatus]
  1414. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1415. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1416. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1417. } completion: { [weak self] (task, responseObject, error) in
  1418. var dic: NSDictionary = [:]
  1419. if error == nil, let responseObject = responseObject as? NSDictionary {
  1420. dic = responseObject
  1421. } else {
  1422. var info = responseObject
  1423. if let error = error {
  1424. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1425. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1426. }
  1427. }
  1428. dic = (info as? NSDictionary) ?? [:]
  1429. }
  1430. let requestCode: Int = dic["code"] as? Int ?? 0
  1431. if requestCode == 200 {
  1432. complete(true, dic)
  1433. } else {
  1434. complete(false, dic)
  1435. }
  1436. // var thirdOrderNo = ""
  1437. // var orderId = ""
  1438. // var payHref = ""
  1439. // if code == 200 {
  1440. // if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1441. // if let token = result["orderId"] { orderId = token as! String }
  1442. // if let token = result["payHref"] { payHref = token as! String }
  1443. // let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1444. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1445. // complete(true, result1)
  1446. // } else {
  1447. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1448. // complete(false, result1)
  1449. // }
  1450. }
  1451. } else {
  1452. complete(false, nil)
  1453. }
  1454. }
  1455. /**
  1456. @abstract 关闭订单
  1457. @param orderId 订单id
  1458. @param complete 回调
  1459. */
  1460. func closeByOrderId(orderId: String, _ complete: @escaping KMMemberRequestInfoComplete) {
  1461. let token: String = KMMemberInfo.shared.access_token
  1462. if token == "" {
  1463. complete(false, nil)
  1464. return
  1465. }
  1466. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/closeOrder"
  1467. let params: [String: Any] = ["orderId": orderId]
  1468. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1469. KMRequestServer.requestServer.request(urlString: urlNewString, method: .post, params: nil) { requestSerializer in
  1470. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1471. } completion: { [weak self] (task, responseObject, error) in
  1472. var dic: NSDictionary = [:]
  1473. if error == nil, let responseObject = responseObject as? NSDictionary {
  1474. dic = responseObject
  1475. } else {
  1476. var info = responseObject
  1477. if let error = error {
  1478. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1479. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1480. }
  1481. }
  1482. dic = (info as? NSDictionary) ?? [:]
  1483. }
  1484. let requestCode: Int = dic["code"] as? Int ?? 0
  1485. if requestCode == 200 {
  1486. complete(true, dic)
  1487. } else {
  1488. complete(false, dic)
  1489. }
  1490. }
  1491. } else {
  1492. complete(false, nil)
  1493. }
  1494. }
  1495. // MARK: 苹果支付
  1496. /**
  1497. @abstract APPstore 权益校验
  1498. @param applePayProductId
  1499. @param transactionId
  1500. @param productCode
  1501. @param complete 回调
  1502. */
  1503. func appStoreEquityVerification(applePayProductId: String, transactionId: String, productCode: String, _ complete: @escaping KMMemberProductComplete) {
  1504. let token: String = KMMemberInfo.shared.access_token
  1505. if token == "" {
  1506. complete(false, nil)
  1507. return
  1508. }
  1509. let urlString = configuration.activityBaseURL() + "/member-system-website/applePay/appStoreEquityVerification"
  1510. let bundleID = Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") ?? ""
  1511. let params: [String: Any] = ["appBundleId": bundleID,
  1512. "applePayProductId": applePayProductId,
  1513. "transactionId": transactionId,
  1514. "productCode": productCode]
  1515. let postData = try! JSONSerialization.data(withJSONObject: params)
  1516. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  1517. request.addValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1518. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  1519. request.httpMethod = "POST"
  1520. request.httpBody = postData
  1521. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  1522. guard let data = data else {
  1523. print(String(describing: error))
  1524. return
  1525. }
  1526. let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
  1527. if jsonObject != nil {
  1528. let code: Int = jsonObject!["code"] as? Int ?? 0
  1529. let result: NSDictionary = jsonObject!["result"] as? NSDictionary ?? [:]
  1530. let message: String = jsonObject!["msg"] as? String ?? ""
  1531. let result1 = KMMemberProductResult(code: code, msg: message, result: "")
  1532. if code == 200 {
  1533. complete(true, result1)
  1534. } else {
  1535. complete(false, result1)
  1536. }
  1537. } else {
  1538. complete(false, KMMemberProductResult(code: 0, msg: "", result: ""))
  1539. }
  1540. }
  1541. task.resume()
  1542. }
  1543. // MARK: 评估模块
  1544. // MARK: 活动模块
  1545. // MARK: 翻译模块
  1546. // MARK: AI模块
  1547. // MARK: 会员赠送模块
  1548. // MARK: Error Code
  1549. static func showAlert(code: KMMemberCenterErrorCodeType?, message: String?, callback: ((NSApplication.ModalResponse?, Any ...)->Void)?) {
  1550. let string = self.getCodeMessage(code: code, message: message)
  1551. KMMainThreadExecute {
  1552. let alert = NSAlert()
  1553. alert.messageText = NSLocalizedString(string, tableName: "MemberCenterLocalizable", comment: "")
  1554. let resp = alert.runModal()
  1555. callback?(resp)
  1556. }
  1557. }
  1558. static func getCodeMessage(code: KMMemberCenterErrorCodeType?, message: String?) -> String {
  1559. var string = self.typeOfMessage(type: code)
  1560. if string.isEmpty {
  1561. string = message ?? ""
  1562. }
  1563. return string
  1564. }
  1565. static func typeOfMessage(type: KMMemberCenterErrorCodeType?) -> String {
  1566. var result: String = ""
  1567. switch type {
  1568. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  1569. result = "Invalid token or token has expired."
  1570. break;
  1571. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  1572. result = "Your device logged-in has been limited. Please log out from other devices first."
  1573. break;
  1574. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  1575. result = "The user has not logged in. Please login before accessing."
  1576. break;
  1577. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  1578. result = "Email code error!"
  1579. break;
  1580. case .EMAIL_REGISTER_ERROR:
  1581. result = "Register error!"
  1582. break;
  1583. case .EXCEPTION_MSG_PASSWORD_ERROR:
  1584. result = "The password is wrong, you can check the case status."
  1585. break;
  1586. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  1587. result = "Code action error."
  1588. break;
  1589. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  1590. result = "Code send error."
  1591. break;
  1592. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  1593. result = "Mail send error."
  1594. break;
  1595. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  1596. result = "Code send too quickly!"
  1597. break;
  1598. case .EXCEPTION_MSG_EMAIL_EXIST:
  1599. result = "This email is already associated with an account. please log in."
  1600. break;
  1601. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  1602. result = "The current account is not registered. Please sign up using the verification code directly."
  1603. break;
  1604. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  1605. result = "The account is being removed and unable to log in. If you have any questions, please get in touch with customer service."
  1606. break;
  1607. case .EXCEPTION_MSG_EMAIL_INVALID:
  1608. result = "Please enter the correct email format."
  1609. break;
  1610. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  1611. result = "Password is inconsistent, please re-enter."
  1612. break;
  1613. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  1614. result = "The password must contain 6 to 24 characters."
  1615. break;
  1616. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  1617. result = "The password must contain 6 to 24 characters."
  1618. break;
  1619. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  1620. result = "Please add auth config"
  1621. break;
  1622. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  1623. result = "You are still subscribing and cannot remove your account at the moment. Please wait until the subscription period is over."
  1624. break;
  1625. case .EXCEPTION_TIME_TRANSFER_ERROR:
  1626. result = "Time conversion failed."
  1627. break;
  1628. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY:
  1629. result = "Password cannot contain spaces."
  1630. break;
  1631. case .EXCEPTION_MSG_CREATE_ORDER_PARAMETER: // 支付方式异常
  1632. result = "Abnormal payment method."
  1633. case .EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED: // 当前事件暂不支持
  1634. result = "The current event is not supported yet."
  1635. case .EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES: // 账号已在其他设备登录
  1636. result = "This account has been logged in on other devices."
  1637. case .EXCEPTION_MSG_ABNORMAL_USER_STATUS: // 当前邮箱已被停用或者处于注销中状态
  1638. result = "The current email address has been deactivated or is in the account cancellation state."
  1639. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED: // Appstore票据验证失败"
  1640. result = "Ticket verification of AppStore failed."
  1641. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY: //Appstore票据验证需要重试
  1642. result = "Ticket verification of AppStore needs to be retried."
  1643. case .EXCEPTION_MSG_REPEAT_SUBSCRIPTION: // 用户已订阅该产品
  1644. result = "The user has subscribed to this product."
  1645. case .EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED: // 恢复购买暂不支持
  1646. result = "Restoring purchases is not supported yet."
  1647. case .EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED: // 用户交易取消或失败
  1648. result = "The user's transaction is canceled or failed."
  1649. case .EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED: // 当前订单末记录,请稍后重试
  1650. result = "The current order is not recorded, please try again later."
  1651. case .EXCEPTION_MSG_THE_SAME_USER: //同一用户,无需处理
  1652. result = "Same user, no need to process."
  1653. case .EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL: //thirdPayld非法
  1654. result = "Third-party payment is illegal."
  1655. case .EXCEPTION_MSG_ORDER_DOES_NOT_EXIST: //订单不存在
  1656. result = "Order does not exist."
  1657. case .EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL: // 订单状态异常
  1658. result = "Order status abnormal."
  1659. case .EXCEPTION_MSG_TRANSACTION_ID_REPEAT: // transactionld重复
  1660. result = "Transaction duplicate."
  1661. case .EXCEPTION_TICKET_HAS_BEEN_BLOCKED: // The ticket has been blocked
  1662. result = "The ticket has been blocked."
  1663. case .EXCEPTION_TICKET_HAS_BEEN_CREATED: // 当前票据已创建订单
  1664. result = "The current ticket has created an order."
  1665. case .EXCEPTION_USER_IS_NOT_EDUCATION_USER: // 用户不是教育用户
  1666. result = "The user is not an educational user."
  1667. case .EXCEPTION_USER_HAS_EDUCATION: // 用户近一年有教育购买订单
  1668. result = "The user has education purchase orders in the past year."
  1669. case .EXCEPTION_USER_CANNOT_BUY_PREMIUM: // 用户已有标准版订阅,无法购买高级版
  1670. result = "The user already has a PDF Reader Pro Standard Plan and cannot purchase the Advance Plan."
  1671. case .EXCEPTION_USER_CANNOT_BUY_VIP: // 用户已有高级版订阅,无法购买标准版
  1672. result = "The user already has a PDF Reader Pro Advance Plan and cannot purchase the Standard Plan."
  1673. case .EXCEPTION_USER_CANNOT_UPGRADE_VIP: // 买断用户无法升级为标准版
  1674. result = "The Permanent Plan user cannot upgrade to the Standard Plan."
  1675. case .EXCEPTION_COUPON_NOT_EXISTS: // 优惠券不存在
  1676. result = "The coupon code does not exist."
  1677. case .EXCEPTION_COUPONTYPE_ERROR: // 优惠券类型错误
  1678. result = "Wrong coupon code type."
  1679. case .EXCEPTION_COUPON_PRODUCT_ERROR: // 优惠券关联产品错误
  1680. result = "Coupon-associated product error."
  1681. case .EXCEPTION_COUPON_INVALID: // 优惠券已失效
  1682. result = "The coupon code has expired."
  1683. case .EXCEPTION_PRODUCT_NOT_EXIST: // 产品不存在
  1684. result = "The product does not exist."
  1685. case .EXCEPTION_MSG_CONTACT_US: //请联系我们获取最新折扣
  1686. result = "Please get in touch with us for the latest discounts."
  1687. case .EXCEPTION_MSG_APP_STORE_TRANSACTION_ID_VALIDATION_FAILED: // 验证苹果交易失败
  1688. result = "Verification of Apple's Transaction failed."
  1689. case .EXCEPTION_MSG_GOOGLE_STORE_TRANSACTION_ID_VALIDATION_FAILED: // 验证谷歌交易失败
  1690. result = "Verification of Google's Transaction failed."
  1691. case .EXCEPTION_PRICE_ERROR: // 价格错误
  1692. result = "Price error."
  1693. case .EXCEPTION_MSG_ORDER_CREATE_FAILED: // 订单创建失败
  1694. result = "Order creation failed."
  1695. case .USER_ALREADY_SUBSCRIBED_PLAN: // 该用户已经是PDF Reader Pro订阅版会员,确定再赠送吗
  1696. result = "This user is already a subscription member of PDF Reader Pro. Are you sure you want to share benefits?"
  1697. case .USER_ALREADY_SUBSCRIBED_PACKAGE: // 该用户已经是PDF Reader Pro永久版会员,无需再赠送哦
  1698. result = "This user is already a permanent member of PDF Reader Pro. You don't need to share benefits anymore."
  1699. case .EXCEPTION_MSG_USER_NOT_EXIST: //用户不存在
  1700. result = "User does not exist."
  1701. case .EXCEPTION_USER_ALREADY_LOTTERY: // 用户已经抽过奖啦
  1702. result = "The user already has a prize draw!"
  1703. case .UNKNOWN:
  1704. result = "UNKNOWN"
  1705. break;
  1706. case .SUCCESS:
  1707. result = "SUCCESS"
  1708. break;
  1709. default:
  1710. result = "Network error"
  1711. }
  1712. return result
  1713. }
  1714. }