KMMemberCenterManager.swift 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. var upgradecnyPriceString:String?
  878. if let token = dict1["id"] { id = token as? String ?? id }
  879. if let token = dict1["productName"] { productName = token as? String ?? productName }
  880. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as? Int ?? maxDeviceNum }
  881. if let token = dict1["levels"] { levels = token as? String ?? levels }
  882. if let token = dict1["platforms"] { platforms = token as? String ?? platforms }
  883. if let token = dict1["productLineId"] { productLineId = token as? Int ?? productLineId }
  884. if let token = dict1["paymentModel"] { paymentModel = token as? Int ?? paymentModel }
  885. if let token = dict1["cycle"] { cycle = token as? Int ?? cycle }
  886. if let token = dict1["code"] { code = token as? String ?? code }
  887. if let token = dict1["price"] as? NSNumber {
  888. let string = KMFormatNSNumber(token, to: 2)
  889. priceString = string
  890. price = token
  891. }
  892. if let token = dict1["displayPrice"] as? NSNumber {
  893. let string = KMFormatNSNumber(token, to: 2)
  894. displayPriceString = string
  895. displayPrice = token
  896. }
  897. if let token = dict1["cnyUpgradePrice"] as? NSNumber {
  898. let string = KMFormatNSNumber(token, to: 2)
  899. upgradecnyPriceString = string
  900. }
  901. if let token = dict1["upgradePrice"] as? NSNumber {
  902. let string = KMFormatNSNumber(token, to: 2)
  903. upgradePriceString = string
  904. upgradePrice = token
  905. }
  906. if let token = dict1["cnyPrice"] as? NSNumber {
  907. let string = KMFormatNSNumber(token, to: 2)
  908. cnyPriceString = string
  909. cnyPrice = token
  910. }
  911. if let token = dict1["cnyDisplayPrice"] as? NSNumber {
  912. let string = KMFormatNSNumber(token, to: 2)
  913. displayCnyPriceString = string
  914. displayCnyPrice = token
  915. }
  916. 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)
  917. products.individualPriceString = priceString
  918. products.individualCnyPriceString = cnyPriceString
  919. products.cnyUpgradePriceString = upgradecnyPriceString
  920. results.append(products)
  921. }
  922. let result = KMMemberProductResult(code: requestCode, msg: message, listingProducts: results)
  923. if requestCode == 200 {
  924. complete(true, result)
  925. } else {
  926. complete(false, result)
  927. }
  928. }
  929. } else {
  930. print("Invalid URL")
  931. complete(false, nil)
  932. }
  933. }
  934. /**
  935. @abstract 获取已登录用户购买商品价格
  936. @param productId 购买的产品id
  937. @param isEducation 是否教育优惠
  938. @param complete 回调
  939. */
  940. func getProductPriceForBuy(productId: String, isEducation: Int, userId: String, _ complete: @escaping KMMemberProductComplete) {
  941. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getProductPriceForBuy"
  942. let params: [String: Any] = ["productId": productId,
  943. "isEducation": isEducation,
  944. "userId": userId]
  945. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  946. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  947. } completion: { [weak self] (task, responseObject, error) in
  948. var dic: NSDictionary = [:]
  949. if error == nil, let responseObject = responseObject as? NSDictionary {
  950. dic = responseObject
  951. } else {
  952. var info = responseObject
  953. if let error = error {
  954. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  955. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  956. }
  957. }
  958. dic = (info as? NSDictionary) ?? [:]
  959. }
  960. let requestCode: Int = dic["code"] as? Int ?? 0
  961. let result_Array: [NSDictionary] = [dic["result"]] as? [NSDictionary] ?? [[:]]
  962. let message: String = dic["msg"] as? String ?? ""
  963. var results: [KMListingProductsModel] = []
  964. for dict1 in result_Array {
  965. var id = ""
  966. var productName = ""
  967. var maxDeviceNum = 0
  968. var levels = ""
  969. var platforms = ""
  970. var productLineId = 0
  971. var paymentModel = 0
  972. var cycle = 0
  973. var code = ""
  974. var price = NSNumber(value: 0.0)
  975. var displayPrice = NSNumber(value: 0.0)
  976. var upgradePrice = NSNumber(value: 0.0)
  977. var cnyPrice = NSNumber(value: 0.0)
  978. var displayCnyPrice = NSNumber(value: 0.0)
  979. var priceString:String?
  980. var displayPriceString:String?
  981. var upgradePriceString:String?
  982. var cnyPriceString:String?
  983. var displayCnyPriceString:String?
  984. var upgradecnyPriceString:String?
  985. if let token = dict1["id"] { id = token as? String ?? id }
  986. if let token = dict1["productName"] { productName = token as? String ?? productName }
  987. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as? Int ?? maxDeviceNum }
  988. if let token = dict1["levels"] { levels = token as? String ?? levels }
  989. if let token = dict1["platforms"] { platforms = token as? String ?? platforms }
  990. if let token = dict1["productLineId"] { productLineId = token as? Int ?? productLineId }
  991. if let token = dict1["paymentModel"] { paymentModel = token as? Int ?? paymentModel }
  992. if let token = dict1["cycle"] { cycle = token as? Int ?? cycle }
  993. if let token = dict1["code"] { code = token as? String ?? code }
  994. if let token = dict1["price"] as? NSNumber {
  995. let string = KMFormatNSNumber(token, to: 2)
  996. priceString = string
  997. price = token
  998. }
  999. if let token = dict1["displayPrice"] as? NSNumber {
  1000. let string = KMFormatNSNumber(token, to: 2)
  1001. displayPriceString = string
  1002. displayPrice = token
  1003. }
  1004. if let token = dict1["upgradePrice"] as? NSNumber {
  1005. let string = KMFormatNSNumber(token, to: 2)
  1006. upgradePriceString = string
  1007. upgradePrice = token
  1008. }
  1009. if let token = dict1["cnyPrice"] as? NSNumber {
  1010. let string = KMFormatNSNumber(token, to: 2)
  1011. cnyPriceString = string
  1012. cnyPrice = token
  1013. }
  1014. if let token = dict1["cnyDisplayPrice"] as? NSNumber {
  1015. let string = KMFormatNSNumber(token, to: 2)
  1016. displayCnyPriceString = string
  1017. displayCnyPrice = token
  1018. }
  1019. if let token = dict1["cnyUpgradePrice"] as? NSNumber {
  1020. let string = KMFormatNSNumber(token, to: 2)
  1021. upgradecnyPriceString = string
  1022. }
  1023. 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)
  1024. products.cnyUpgradePriceString = upgradecnyPriceString
  1025. products.individualPriceString = priceString
  1026. products.individualCnyPriceString = cnyPriceString
  1027. results.append(products)
  1028. }
  1029. let result = KMMemberProductResult(code: requestCode, msg: message, listingProducts: results)
  1030. if requestCode == 200 {
  1031. complete(true, result)
  1032. } else {
  1033. complete(false, result)
  1034. }
  1035. }
  1036. } else {
  1037. print("Invalid URL")
  1038. complete(false, nil)
  1039. }
  1040. }
  1041. /**
  1042. @abstract 获取批量阶段购买价格
  1043. @param productId 购买的产品id
  1044. @param num 批量个数
  1045. @param complete 回调
  1046. */
  1047. func getBatchProductPrice(productId: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1048. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getBatchProductPrice"
  1049. let params: [String: Any] = ["productId": productId,
  1050. "num": NSNumber(value: Int32(num))]
  1051. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1052. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1053. } completion: { [weak self] (task, responseObject, error) in
  1054. var dic: NSDictionary = [:]
  1055. if error == nil, let responseObject = responseObject as? NSDictionary {
  1056. dic = responseObject
  1057. } else {
  1058. var info = responseObject
  1059. if let error = error {
  1060. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1061. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1062. }
  1063. }
  1064. dic = (info as? NSDictionary) ?? [:]
  1065. }
  1066. let requestCode: Int = dic["code"] as? Int ?? 0
  1067. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1068. let message: String = dic["msg"] as? String ?? ""
  1069. var productId = ""
  1070. var totalPrice = ""
  1071. var price = ""
  1072. var batchPrice = ""
  1073. var cnyPrice = ""
  1074. var cnyBatchPrice = ""
  1075. var cnyTotalPrice = ""
  1076. if let token = result["productId"] { productId = token as? String ?? productId}
  1077. if let token = result["totalPrice"] { totalPrice = token as? String ?? totalPrice}
  1078. if let token = result["price"] { price = token as? String ?? price}
  1079. if let token = result["batchPrice"] { batchPrice = token as? String ?? batchPrice}
  1080. if let token = result["cnyPrice"] { cnyPrice = token as? String ?? cnyPrice}
  1081. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as? String ?? cnyBatchPrice}
  1082. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as? String ?? cnyTotalPrice}
  1083. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1084. let result1 = KMMemberProductResult(code: requestCode, msg: message, batchProductPrice: products)
  1085. if requestCode == 200 {
  1086. complete(true, result1)
  1087. } else {
  1088. complete(false, result1)
  1089. }
  1090. }
  1091. } else {
  1092. print("Invalid URL")
  1093. }
  1094. }
  1095. /**
  1096. @abstract 验证商品优惠券,返回商品优惠后价格
  1097. @param productId 购买的产品id
  1098. @param userId 用户ID
  1099. @param code 优惠券
  1100. @param complete 回调
  1101. */
  1102. func checkCoupon(productId: String, userId: String, code: String, _ complete: @escaping KMMemberProductComplete) {
  1103. let token: String = KMMemberInfo.shared.access_token
  1104. if token == "" {
  1105. complete(false, nil)
  1106. return
  1107. }
  1108. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkCoupon"
  1109. let params: [String: Any] = ["productId": productId,
  1110. "userId": userId,
  1111. "code": code]
  1112. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1113. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1114. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1115. } completion: { [weak self] (task, responseObject, error) in
  1116. var dic: NSDictionary = [:]
  1117. if error == nil, let responseObject = responseObject as? NSDictionary {
  1118. dic = responseObject
  1119. } else {
  1120. var info = responseObject
  1121. if let error = error {
  1122. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1123. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1124. }
  1125. }
  1126. dic = (info as? NSDictionary) ?? [:]
  1127. }
  1128. let requestCode: Int = dic["code"] as? Int ?? 0
  1129. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1130. let message: String = dic["msg"] as? String ?? ""
  1131. var id = ""
  1132. var productName = ""
  1133. var maxDeviceNum = 0
  1134. var levels = ""
  1135. var platforms = ""
  1136. var productLineId = 0
  1137. var paymentModel = 0
  1138. var cycle = 0
  1139. var code = ""
  1140. var price = NSNumber(value: 0.0)
  1141. var displayPrice = NSNumber(value: 0.0)
  1142. var upgradePrice = NSNumber(value: 0.0)
  1143. var cnyPrice = NSNumber(value: 0.0)
  1144. var displayCnyPrice = NSNumber(value: 0.0)
  1145. var priceString:String?
  1146. var displayPriceString:String?
  1147. var upgradePriceString:String?
  1148. var cnyPriceString:String?
  1149. var displayCnyPriceString:String?
  1150. var upgradecnyPriceString:String?
  1151. if let token = result["id"] { id = token as? String ?? id }
  1152. if let token = result["productName"] { productName = token as? String ?? productName }
  1153. if let token = result["maxDeviceNum"] { maxDeviceNum = token as? Int ?? maxDeviceNum }
  1154. if let token = result["levels"] { levels = token as? String ?? levels }
  1155. if let token = result["platforms"] { platforms = token as? String ?? platforms }
  1156. if let token = result["productLineId"] { productLineId = token as? Int ?? productLineId }
  1157. if let token = result["paymentModel"] { paymentModel = token as? Int ?? paymentModel }
  1158. if let token = result["cycle"] { cycle = token as? Int ?? cycle }
  1159. if let token = result["code"] { code = token as? String ?? code }
  1160. if let token = result["price"] as? NSNumber {
  1161. let string = KMFormatNSNumber(token, to: 2)
  1162. priceString = string
  1163. price = token
  1164. }
  1165. if let token = result["displayPrice"] as? NSNumber {
  1166. let string = KMFormatNSNumber(token, to: 2)
  1167. displayPriceString = string
  1168. displayPrice = token
  1169. }
  1170. if let token = result["upgradePrice"] as? NSNumber {
  1171. let string = KMFormatNSNumber(token, to: 2)
  1172. upgradePriceString = string
  1173. upgradePrice = token
  1174. }
  1175. if let token = result["cnyPrice"] as? NSNumber {
  1176. let string = KMFormatNSNumber(token, to: 2)
  1177. cnyPriceString = string
  1178. cnyPrice = token
  1179. }
  1180. if let token = result["cnyDisplayPrice"] as? NSNumber {
  1181. let string = KMFormatNSNumber(token, to: 2)
  1182. displayCnyPriceString = string
  1183. displayCnyPrice = token
  1184. }
  1185. if let token = result["cnyUpgradePrice"] as? NSNumber {
  1186. let string = KMFormatNSNumber(token, to: 2)
  1187. upgradecnyPriceString = string
  1188. }
  1189. 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)
  1190. products.upgradecnyPriceString = upgradecnyPriceString
  1191. let result1 = KMMemberProductResult(code: requestCode, msg: message, checkCoupon: products)
  1192. if requestCode == 200 {
  1193. complete(true, result1)
  1194. } else {
  1195. complete(false, result1)
  1196. }
  1197. }
  1198. } else {
  1199. print("Invalid URL")
  1200. complete(false, nil)
  1201. }
  1202. }
  1203. /**
  1204. @abstract 检查邮箱教育优惠资格
  1205. @param email 邮箱
  1206. @param complete 回调
  1207. */
  1208. func checkEducation(email: String, _ complete: @escaping KMMemberProductComplete) {
  1209. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkEducation"
  1210. let params: [String: Any] = ["email": email]
  1211. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1212. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1213. } completion: { [weak self] (task, responseObject, error) in
  1214. var dic: NSDictionary = [:]
  1215. if error == nil, let responseObject = responseObject as? NSDictionary {
  1216. dic = responseObject
  1217. } else {
  1218. var info = responseObject
  1219. if let error = error {
  1220. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1221. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1222. }
  1223. }
  1224. dic = (info as? NSDictionary) ?? [:]
  1225. }
  1226. let requestCode: Int = dic["code"] as? Int ?? 0
  1227. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1228. let message: String = dic["msg"] as? String ?? ""
  1229. var productId = ""
  1230. var totalPrice = ""
  1231. var price = ""
  1232. var batchPrice = ""
  1233. var cnyPrice = ""
  1234. var cnyBatchPrice = ""
  1235. var cnyTotalPrice = ""
  1236. if let token = result["productId"] { productId = token as? String ?? productId}
  1237. if let token = result["totalPrice"] { totalPrice = token as? String ?? totalPrice}
  1238. if let token = result["price"] { price = token as? String ?? price}
  1239. if let token = result["batchPrice"] { batchPrice = token as? String ?? batchPrice}
  1240. if let token = result["cnyPrice"] { cnyPrice = token as? String ?? cnyPrice}
  1241. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as? String ?? cnyBatchPrice}
  1242. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as? String ?? cnyTotalPrice}
  1243. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1244. let result1 = KMMemberProductResult(code: requestCode, msg: message, batchProductPrice: products)
  1245. if requestCode == 200 {
  1246. complete(true, result1)
  1247. } else {
  1248. complete(false, result1)
  1249. }
  1250. }
  1251. } else {
  1252. print("Invalid URL")
  1253. complete(false, nil)
  1254. }
  1255. }
  1256. // MARK: 个人中心模块
  1257. // MARK: 订单模块
  1258. /**
  1259. @abstract 支付后查询订单状态
  1260. @param orderId 订单id
  1261. @param complete 回调
  1262. */
  1263. func getStateByOrderId(orderId: String, _ complete: @escaping KMMemberRequestInfoComplete) {
  1264. let token: String = KMMemberInfo.shared.access_token
  1265. if token == "" {
  1266. complete(false, nil)
  1267. return
  1268. }
  1269. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getStateByOrderId"
  1270. let params: [String: Any] = ["orderId": orderId]
  1271. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1272. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1273. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1274. } completion: { [weak self] (task, responseObject, error) in
  1275. var dic: NSDictionary = [:]
  1276. if error == nil, let responseObject = responseObject as? NSDictionary {
  1277. dic = responseObject
  1278. } else {
  1279. var info = responseObject
  1280. if let error = error {
  1281. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1282. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1283. }
  1284. }
  1285. dic = (info as? NSDictionary) ?? [:]
  1286. }
  1287. let requestCode: Int = dic["code"] as? Int ?? 0
  1288. let requestDic: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1289. let status:String = requestDic["status"] as? String ?? ""
  1290. if requestCode == 200{
  1291. complete(true, dic)
  1292. } else {
  1293. complete(false, dic)
  1294. }
  1295. }
  1296. } else {
  1297. print("Invalid URL")
  1298. complete(false, nil)
  1299. }
  1300. }
  1301. /**
  1302. @abstract 创建订单(买断)
  1303. @param productId 产品id
  1304. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1305. @param price 价格
  1306. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1307. @param couponCode 优惠卷
  1308. @param num 批量购买数量
  1309. @param complete 回调
  1310. */
  1311. func createOrder(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1312. let token: String = KMMemberInfo.shared.access_token
  1313. if token == "" {
  1314. complete(false, nil)
  1315. return
  1316. }
  1317. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createOrder"
  1318. let params: [String: Any] = ["productId": productId,
  1319. "paymentMethod": paymentMethod,
  1320. "price": price,
  1321. "discountFlag": discountFlag,
  1322. "couponCode": couponCode,
  1323. "num": num]
  1324. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1325. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1326. } completion: { [weak self] (task, responseObject, error) in
  1327. var dic: NSDictionary = [:]
  1328. if error == nil, let responseObject = responseObject as? NSDictionary {
  1329. dic = responseObject
  1330. } else {
  1331. var info = responseObject
  1332. if let error = error {
  1333. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1334. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1335. }
  1336. }
  1337. dic = (info as? NSDictionary) ?? [:]
  1338. }
  1339. let requestCode: Int = dic["code"] as? Int ?? 0
  1340. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1341. let message: String = dic["msg"] as? String ?? ""
  1342. var thirdOrderNo = ""
  1343. var orderId = ""
  1344. var payHref = ""
  1345. var qrCode = ""
  1346. if requestCode == 200 {
  1347. if let token = result["thirdOrderNo"] { thirdOrderNo = token as? String ?? thirdOrderNo}
  1348. if let token = result["orderId"] { orderId = token as? String ?? orderId}
  1349. if let token = result["payHref"] { payHref = token as? String ?? payHref}
  1350. if let token = result["qrcode"] { qrCode = token as? String ?? qrCode}
  1351. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode)
  1352. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: products)
  1353. complete(true, result1)
  1354. } else {
  1355. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode))
  1356. complete(false, result1)
  1357. }
  1358. }
  1359. }
  1360. /**
  1361. @abstract 创建订单(订阅)
  1362. @param productId 产品id
  1363. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1364. @param price 价格
  1365. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1366. @param couponCode 优惠卷
  1367. @param num 批量购买数量
  1368. @param complete 回调
  1369. */
  1370. func createSubscriber(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1371. let token: String = KMMemberInfo.shared.access_token
  1372. if token == "" {
  1373. complete(false, nil)
  1374. return
  1375. }
  1376. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createSubscription"
  1377. let params: [String: Any] = ["productId": productId,
  1378. "paymentMethod": paymentMethod,
  1379. "price": price,
  1380. "discountFlag": discountFlag,
  1381. "couponCode": couponCode,
  1382. "num": num]
  1383. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1384. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1385. } completion: { [weak self] (task, responseObject, error) in
  1386. var dic: NSDictionary = [:]
  1387. if error == nil, let responseObject = responseObject as? NSDictionary {
  1388. dic = responseObject
  1389. } else {
  1390. var info = responseObject
  1391. if let error = error {
  1392. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1393. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1394. }
  1395. }
  1396. dic = (info as? NSDictionary) ?? [:]
  1397. }
  1398. let requestCode: Int = dic["code"] as? Int ?? 0
  1399. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1400. let message: String = dic["msg"] as? String ?? ""
  1401. var thirdOrderNo = ""
  1402. var orderId = ""
  1403. var payHref = ""
  1404. var qrCode = ""
  1405. if requestCode == 200 {
  1406. if let token = result["thirdOrderNo"] { thirdOrderNo = token as? String ?? thirdOrderNo}
  1407. if let token = result["orderId"] { orderId = token as? String ?? orderId}
  1408. if let token = result["payHref"] { payHref = token as? String ?? payHref}
  1409. if let token = result["qrcode"] { qrCode = token as? String ?? qrCode}
  1410. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode)
  1411. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: products)
  1412. complete(true, result1)
  1413. } else {
  1414. let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode))
  1415. complete(false, result1)
  1416. }
  1417. }
  1418. }
  1419. /**
  1420. @abstract 获取订单列表
  1421. @param orderStatus 0全部1待支付
  1422. @param complete 回调
  1423. */
  1424. func getOrderListByStatus(orderStatus: Int, _ complete: @escaping KMMemberRequestInfoComplete) {
  1425. let token: String = KMMemberInfo.shared.access_token
  1426. if token == "" {
  1427. complete(false, nil)
  1428. return
  1429. }
  1430. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getOrderListByStatus"
  1431. let params: [String: Any] = ["orderStatus": orderStatus]
  1432. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1433. KMRequestServer.requestServer.request(urlString: urlNewString, method: .get, params: nil) { requestSerializer in
  1434. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1435. } completion: { [weak self] (task, responseObject, error) in
  1436. var dic: NSDictionary = [:]
  1437. if error == nil, let responseObject = responseObject as? NSDictionary {
  1438. dic = responseObject
  1439. } else {
  1440. var info = responseObject
  1441. if let error = error {
  1442. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1443. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1444. }
  1445. }
  1446. dic = (info as? NSDictionary) ?? [:]
  1447. }
  1448. let requestCode: Int = dic["code"] as? Int ?? 0
  1449. if requestCode == 200 {
  1450. complete(true, dic)
  1451. } else {
  1452. complete(false, dic)
  1453. }
  1454. // var thirdOrderNo = ""
  1455. // var orderId = ""
  1456. // var payHref = ""
  1457. // if code == 200 {
  1458. // if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1459. // if let token = result["orderId"] { orderId = token as! String }
  1460. // if let token = result["payHref"] { payHref = token as! String }
  1461. // let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1462. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1463. // complete(true, result1)
  1464. // } else {
  1465. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1466. // complete(false, result1)
  1467. // }
  1468. }
  1469. } else {
  1470. complete(false, nil)
  1471. }
  1472. }
  1473. /**
  1474. @abstract 关闭订单
  1475. @param orderId 订单id
  1476. @param complete 回调
  1477. */
  1478. func closeByOrderId(orderId: String, _ complete: @escaping KMMemberRequestInfoComplete) {
  1479. let token: String = KMMemberInfo.shared.access_token
  1480. if token == "" {
  1481. complete(false, nil)
  1482. return
  1483. }
  1484. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/closeOrder"
  1485. let params: [String: Any] = ["orderId": orderId]
  1486. if let urlNewString = constructURLString(baseURLString: urlString, parameters: params) {
  1487. KMRequestServer.requestServer.request(urlString: urlNewString, method: .post, params: nil) { requestSerializer in
  1488. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1489. } completion: { [weak self] (task, responseObject, error) in
  1490. var dic: NSDictionary = [:]
  1491. if error == nil, let responseObject = responseObject as? NSDictionary {
  1492. dic = responseObject
  1493. } else {
  1494. var info = responseObject
  1495. if let error = error {
  1496. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1497. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1498. }
  1499. }
  1500. dic = (info as? NSDictionary) ?? [:]
  1501. }
  1502. let requestCode: Int = dic["code"] as? Int ?? 0
  1503. if requestCode == 200 {
  1504. complete(true, dic)
  1505. } else {
  1506. complete(false, dic)
  1507. }
  1508. }
  1509. } else {
  1510. complete(false, nil)
  1511. }
  1512. }
  1513. // MARK: 苹果支付
  1514. /**
  1515. @abstract APPstore 权益校验
  1516. @param applePayProductId
  1517. @param transactionId
  1518. @param productCode
  1519. @param complete 回调
  1520. */
  1521. func appStoreEquityVerification(applePayProductId: String, transactionId: String, productCode: String, _ complete: @escaping KMMemberProductComplete) {
  1522. let token: String = KMMemberInfo.shared.access_token
  1523. if token == "" {
  1524. complete(false, nil)
  1525. return
  1526. }
  1527. let urlString = configuration.activityBaseURL() + "/member-system-website/applePay/appStoreEquityVerification"
  1528. let bundleID = Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") ?? ""
  1529. let params: [String: Any] = ["appBundleId": bundleID,
  1530. "applePayProductId": applePayProductId,
  1531. "transactionId": transactionId,
  1532. "productCode": productCode]
  1533. let postData = try! JSONSerialization.data(withJSONObject: params)
  1534. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  1535. request.addValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1536. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  1537. request.httpMethod = "POST"
  1538. request.httpBody = postData
  1539. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  1540. guard let data = data else {
  1541. print(String(describing: error))
  1542. return
  1543. }
  1544. let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
  1545. if jsonObject != nil {
  1546. let code: Int = jsonObject!["code"] as? Int ?? 0
  1547. let result: NSDictionary = jsonObject!["result"] as? NSDictionary ?? [:]
  1548. let message: String = jsonObject!["msg"] as? String ?? ""
  1549. let result1 = KMMemberProductResult(code: code, msg: message, result: "")
  1550. if code == 200 {
  1551. complete(true, result1)
  1552. } else {
  1553. complete(false, result1)
  1554. }
  1555. } else {
  1556. complete(false, KMMemberProductResult(code: 0, msg: "", result: ""))
  1557. }
  1558. }
  1559. task.resume()
  1560. }
  1561. // MARK: 评估模块
  1562. // MARK: 活动模块
  1563. // MARK: 翻译模块
  1564. // MARK: AI模块
  1565. // MARK: 会员赠送模块
  1566. // MARK: Error Code
  1567. static func quickShowAlert(result: KMMemberProductResult?, callback: ((NSApplication.ModalResponse?, Any ...)->Void)?) {
  1568. guard let data = result else {
  1569. return
  1570. }
  1571. showAlert(code: .init(rawValue: data.code ?? 0), message: data.msg, callback: callback)
  1572. }
  1573. static func showAlert(code: KMMemberCenterErrorCodeType?, message: String?, callback: ((NSApplication.ModalResponse?, Any ...)->Void)?) {
  1574. let string = self.getCodeMessage(code: code, message: message)
  1575. KMMainThreadExecute {
  1576. let alert = NSAlert()
  1577. alert.messageText = NSLocalizedString(string, tableName: "MemberCenterLocalizable", comment: "")
  1578. let resp = alert.runModal()
  1579. callback?(resp)
  1580. }
  1581. }
  1582. static func getCodeMessage(code: KMMemberCenterErrorCodeType?, message: String?) -> String {
  1583. var string = self.typeOfMessage(type: code)
  1584. if string.isEmpty {
  1585. string = message ?? ""
  1586. }
  1587. return string
  1588. }
  1589. static func typeOfMessage(type: KMMemberCenterErrorCodeType?) -> String {
  1590. var result: String = ""
  1591. switch type {
  1592. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  1593. result = "Invalid token or token has expired."
  1594. break;
  1595. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  1596. result = "Your device logged-in has been limited. Please log out from other devices first."
  1597. break;
  1598. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  1599. result = "The user has not logged in. Please login before accessing."
  1600. break;
  1601. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  1602. result = "Email code error!"
  1603. break;
  1604. case .EMAIL_REGISTER_ERROR:
  1605. result = "Register error!"
  1606. break;
  1607. case .EXCEPTION_MSG_PASSWORD_ERROR:
  1608. result = "The password is wrong, you can check the case status."
  1609. break;
  1610. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  1611. result = "Code action error."
  1612. break;
  1613. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  1614. result = "Code send error."
  1615. break;
  1616. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  1617. result = "Mail send error."
  1618. break;
  1619. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  1620. result = "Code send too quickly!"
  1621. break;
  1622. case .EXCEPTION_MSG_EMAIL_EXIST:
  1623. result = "This email is already associated with an account. please log in."
  1624. break;
  1625. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  1626. result = "The current account is not registered. Please sign up using the verification code directly."
  1627. break;
  1628. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  1629. result = "The account is being removed and unable to log in. If you have any questions, please get in touch with customer service."
  1630. break;
  1631. case .EXCEPTION_MSG_EMAIL_INVALID:
  1632. result = "Please enter the correct email format."
  1633. break;
  1634. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  1635. result = "Password is inconsistent, please re-enter."
  1636. break;
  1637. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  1638. result = "The password must contain 6 to 24 characters."
  1639. break;
  1640. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  1641. result = "The password must contain 6 to 24 characters."
  1642. break;
  1643. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  1644. result = "Please add auth config"
  1645. break;
  1646. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  1647. result = "You are still subscribing and cannot remove your account at the moment. Please wait until the subscription period is over."
  1648. break;
  1649. case .EXCEPTION_TIME_TRANSFER_ERROR:
  1650. result = "Time conversion failed."
  1651. break;
  1652. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY:
  1653. result = "Password cannot contain spaces."
  1654. break;
  1655. case .EXCEPTION_MSG_CREATE_ORDER_PARAMETER: // 支付方式异常
  1656. result = "Abnormal payment method."
  1657. case .EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED: // 当前事件暂不支持
  1658. result = "The current event is not supported yet."
  1659. case .EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES: // 账号已在其他设备登录
  1660. result = "This account has been logged in on other devices."
  1661. case .EXCEPTION_MSG_ABNORMAL_USER_STATUS: // 当前邮箱已被停用或者处于注销中状态
  1662. result = "The current email address has been deactivated or is in the account cancellation state."
  1663. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED: // Appstore票据验证失败"
  1664. result = "Ticket verification of AppStore failed."
  1665. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY: //Appstore票据验证需要重试
  1666. result = "Ticket verification of AppStore needs to be retried."
  1667. case .EXCEPTION_MSG_REPEAT_SUBSCRIPTION: // 用户已订阅该产品
  1668. result = "The user has subscribed to this product."
  1669. case .EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED: // 恢复购买暂不支持
  1670. result = "Restoring purchases is not supported yet."
  1671. case .EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED: // 用户交易取消或失败
  1672. result = "The user's transaction is canceled or failed."
  1673. case .EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED: // 当前订单末记录,请稍后重试
  1674. result = "The current order is not recorded, please try again later."
  1675. case .EXCEPTION_MSG_THE_SAME_USER: //同一用户,无需处理
  1676. result = "Same user, no need to process."
  1677. case .EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL: //thirdPayld非法
  1678. result = "Third-party payment is illegal."
  1679. case .EXCEPTION_MSG_ORDER_DOES_NOT_EXIST: //订单不存在
  1680. result = "Order does not exist."
  1681. case .EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL: // 订单状态异常
  1682. result = "Order status abnormal."
  1683. case .EXCEPTION_MSG_TRANSACTION_ID_REPEAT: // transactionld重复
  1684. result = "Transaction duplicate."
  1685. case .EXCEPTION_TICKET_HAS_BEEN_BLOCKED: // The ticket has been blocked
  1686. result = "The ticket has been blocked."
  1687. case .EXCEPTION_TICKET_HAS_BEEN_CREATED: // 当前票据已创建订单
  1688. result = "The current ticket has created an order."
  1689. case .EXCEPTION_USER_IS_NOT_EDUCATION_USER: // 用户不是教育用户
  1690. result = "The user is not an educational user."
  1691. case .EXCEPTION_USER_HAS_EDUCATION: // 用户近一年有教育购买订单
  1692. result = "The user has education purchase orders in the past year."
  1693. case .EXCEPTION_USER_CANNOT_BUY_PREMIUM: // 用户已有标准版订阅,无法购买高级版
  1694. result = "The user already has a PDF Reader Pro Standard Plan and cannot purchase the Advance Plan."
  1695. case .EXCEPTION_USER_CANNOT_BUY_VIP: // 用户已有高级版订阅,无法购买标准版
  1696. result = "The user already has a PDF Reader Pro Advance Plan and cannot purchase the Standard Plan."
  1697. case .EXCEPTION_USER_CANNOT_UPGRADE_VIP: // 买断用户无法升级为标准版
  1698. result = "The Permanent Plan user cannot upgrade to the Standard Plan."
  1699. case .EXCEPTION_COUPON_NOT_EXISTS: // 优惠券不存在
  1700. result = "The coupon code does not exist."
  1701. case .EXCEPTION_COUPONTYPE_ERROR: // 优惠券类型错误
  1702. result = "Wrong coupon code type."
  1703. case .EXCEPTION_COUPON_PRODUCT_ERROR: // 优惠券关联产品错误
  1704. result = "Coupon-associated product error."
  1705. case .EXCEPTION_COUPON_INVALID: // 优惠券已失效
  1706. result = "The coupon code has expired."
  1707. case .EXCEPTION_PRODUCT_NOT_EXIST: // 产品不存在
  1708. result = "The product does not exist."
  1709. case .EXCEPTION_MSG_CONTACT_US: //请联系我们获取最新折扣
  1710. result = "Please get in touch with us for the latest discounts."
  1711. case .EXCEPTION_MSG_APP_STORE_TRANSACTION_ID_VALIDATION_FAILED: // 验证苹果交易失败
  1712. result = "Verification of Apple's Transaction failed."
  1713. case .EXCEPTION_MSG_GOOGLE_STORE_TRANSACTION_ID_VALIDATION_FAILED: // 验证谷歌交易失败
  1714. result = "Verification of Google's Transaction failed."
  1715. case .EXCEPTION_PRICE_ERROR: // 价格错误
  1716. result = "Price error."
  1717. case .EXCEPTION_MSG_ORDER_CREATE_FAILED: // 订单创建失败
  1718. result = "Order creation failed."
  1719. case .USER_ALREADY_SUBSCRIBED_PLAN: // 该用户已经是PDF Reader Pro订阅版会员,确定再赠送吗
  1720. result = "This user is already a subscription member of PDF Reader Pro. Are you sure you want to share benefits?"
  1721. case .USER_ALREADY_SUBSCRIBED_PACKAGE: // 该用户已经是PDF Reader Pro永久版会员,无需再赠送哦
  1722. result = "This user is already a permanent member of PDF Reader Pro. You don't need to share benefits anymore."
  1723. case .EXCEPTION_MSG_USER_NOT_EXIST: //用户不存在
  1724. result = "User does not exist."
  1725. case .EXCEPTION_USER_ALREADY_LOTTERY: // 用户已经抽过奖啦
  1726. result = "The user already has a prize draw!"
  1727. case .UNKNOWN:
  1728. result = "UNKNOWN"
  1729. break;
  1730. case .SUCCESS:
  1731. result = "SUCCESS"
  1732. break;
  1733. default:
  1734. result = "Network error"
  1735. }
  1736. return result
  1737. }
  1738. }