KMMemberCenterManager.swift 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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 showAlert(code: KMMemberCenterErrorCodeType?, message: String?, callback: ((NSApplication.ModalResponse?, Any ...)->Void)?) {
  1568. let string = self.getCodeMessage(code: code, message: message)
  1569. KMMainThreadExecute {
  1570. let alert = NSAlert()
  1571. alert.messageText = NSLocalizedString(string, tableName: "MemberCenterLocalizable", comment: "")
  1572. let resp = alert.runModal()
  1573. callback?(resp)
  1574. }
  1575. }
  1576. static func getCodeMessage(code: KMMemberCenterErrorCodeType?, message: String?) -> String {
  1577. var string = self.typeOfMessage(type: code)
  1578. if string.isEmpty {
  1579. string = message ?? ""
  1580. }
  1581. return string
  1582. }
  1583. static func typeOfMessage(type: KMMemberCenterErrorCodeType?) -> String {
  1584. var result: String = ""
  1585. switch type {
  1586. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  1587. result = "Invalid token or token has expired."
  1588. break;
  1589. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  1590. result = "Your device logged-in has been limited. Please log out from other devices first."
  1591. break;
  1592. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  1593. result = "The user has not logged in. Please login before accessing."
  1594. break;
  1595. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  1596. result = "Email code error!"
  1597. break;
  1598. case .EMAIL_REGISTER_ERROR:
  1599. result = "Register error!"
  1600. break;
  1601. case .EXCEPTION_MSG_PASSWORD_ERROR:
  1602. result = "The password is wrong, you can check the case status."
  1603. break;
  1604. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  1605. result = "Code action error."
  1606. break;
  1607. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  1608. result = "Code send error."
  1609. break;
  1610. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  1611. result = "Mail send error."
  1612. break;
  1613. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  1614. result = "Code send too quickly!"
  1615. break;
  1616. case .EXCEPTION_MSG_EMAIL_EXIST:
  1617. result = "This email is already associated with an account. please log in."
  1618. break;
  1619. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  1620. result = "The current account is not registered. Please sign up using the verification code directly."
  1621. break;
  1622. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  1623. result = "The account is being removed and unable to log in. If you have any questions, please get in touch with customer service."
  1624. break;
  1625. case .EXCEPTION_MSG_EMAIL_INVALID:
  1626. result = "Please enter the correct email format."
  1627. break;
  1628. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  1629. result = "Password is inconsistent, please re-enter."
  1630. break;
  1631. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  1632. result = "The password must contain 6 to 24 characters."
  1633. break;
  1634. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  1635. result = "The password must contain 6 to 24 characters."
  1636. break;
  1637. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  1638. result = "Please add auth config"
  1639. break;
  1640. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  1641. result = "You are still subscribing and cannot remove your account at the moment. Please wait until the subscription period is over."
  1642. break;
  1643. case .EXCEPTION_TIME_TRANSFER_ERROR:
  1644. result = "Time conversion failed."
  1645. break;
  1646. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY:
  1647. result = "Password cannot contain spaces."
  1648. break;
  1649. case .EXCEPTION_MSG_CREATE_ORDER_PARAMETER: // 支付方式异常
  1650. result = "Abnormal payment method."
  1651. case .EXCEPTION_MSG_EVENT_IS_CURRENTLY_NOT_SUPPORTED: // 当前事件暂不支持
  1652. result = "The current event is not supported yet."
  1653. case .EXCEPTION_MSG_THE_ACCOUNT_IS_ALREADY_LOGGED_IN_ON_OTHER_DEVICES: // 账号已在其他设备登录
  1654. result = "This account has been logged in on other devices."
  1655. case .EXCEPTION_MSG_ABNORMAL_USER_STATUS: // 当前邮箱已被停用或者处于注销中状态
  1656. result = "The current email address has been deactivated or is in the account cancellation state."
  1657. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_FAILED: // Appstore票据验证失败"
  1658. result = "Ticket verification of AppStore failed."
  1659. case .EXCEPTION_MSG_APP_STORE_TICKET_VALIDATION_NEED_TO_RETRY: //Appstore票据验证需要重试
  1660. result = "Ticket verification of AppStore needs to be retried."
  1661. case .EXCEPTION_MSG_REPEAT_SUBSCRIPTION: // 用户已订阅该产品
  1662. result = "The user has subscribed to this product."
  1663. case .EXCEPTION_MSG_RESTORING_PURCHASES_IS_NOT_CURRENTLY_SUPPORTED: // 恢复购买暂不支持
  1664. result = "Restoring purchases is not supported yet."
  1665. case .EXCEPTION_MSG_USER_TRANSACTION_CANCELED_OR_FAILED: // 用户交易取消或失败
  1666. result = "The user's transaction is canceled or failed."
  1667. case .EXCEPTION_MSG_CURRENT_ORDER_NOT_RECORDED: // 当前订单末记录,请稍后重试
  1668. result = "The current order is not recorded, please try again later."
  1669. case .EXCEPTION_MSG_THE_SAME_USER: //同一用户,无需处理
  1670. result = "Same user, no need to process."
  1671. case .EXCEPTION_MSG_THIRD_PAY_ID_ILLEGAL: //thirdPayld非法
  1672. result = "Third-party payment is illegal."
  1673. case .EXCEPTION_MSG_ORDER_DOES_NOT_EXIST: //订单不存在
  1674. result = "Order does not exist."
  1675. case .EXCEPTION_MSG_THE_ORDER_STATUS_IS_ABNORMAL: // 订单状态异常
  1676. result = "Order status abnormal."
  1677. case .EXCEPTION_MSG_TRANSACTION_ID_REPEAT: // transactionld重复
  1678. result = "Transaction duplicate."
  1679. case .EXCEPTION_TICKET_HAS_BEEN_BLOCKED: // The ticket has been blocked
  1680. result = "The ticket has been blocked."
  1681. case .EXCEPTION_TICKET_HAS_BEEN_CREATED: // 当前票据已创建订单
  1682. result = "The current ticket has created an order."
  1683. case .EXCEPTION_USER_IS_NOT_EDUCATION_USER: // 用户不是教育用户
  1684. result = "The user is not an educational user."
  1685. case .EXCEPTION_USER_HAS_EDUCATION: // 用户近一年有教育购买订单
  1686. result = "The user has education purchase orders in the past year."
  1687. case .EXCEPTION_USER_CANNOT_BUY_PREMIUM: // 用户已有标准版订阅,无法购买高级版
  1688. result = "The user already has a PDF Reader Pro Standard Plan and cannot purchase the Advance Plan."
  1689. case .EXCEPTION_USER_CANNOT_BUY_VIP: // 用户已有高级版订阅,无法购买标准版
  1690. result = "The user already has a PDF Reader Pro Advance Plan and cannot purchase the Standard Plan."
  1691. case .EXCEPTION_USER_CANNOT_UPGRADE_VIP: // 买断用户无法升级为标准版
  1692. result = "The Permanent Plan user cannot upgrade to the Standard Plan."
  1693. case .EXCEPTION_COUPON_NOT_EXISTS: // 优惠券不存在
  1694. result = "The coupon code does not exist."
  1695. case .EXCEPTION_COUPONTYPE_ERROR: // 优惠券类型错误
  1696. result = "Wrong coupon code type."
  1697. case .EXCEPTION_COUPON_PRODUCT_ERROR: // 优惠券关联产品错误
  1698. result = "Coupon-associated product error."
  1699. case .EXCEPTION_COUPON_INVALID: // 优惠券已失效
  1700. result = "The coupon code has expired."
  1701. case .EXCEPTION_PRODUCT_NOT_EXIST: // 产品不存在
  1702. result = "The product does not exist."
  1703. case .EXCEPTION_MSG_CONTACT_US: //请联系我们获取最新折扣
  1704. result = "Please get in touch with us for the latest discounts."
  1705. case .EXCEPTION_MSG_APP_STORE_TRANSACTION_ID_VALIDATION_FAILED: // 验证苹果交易失败
  1706. result = "Verification of Apple's Transaction failed."
  1707. case .EXCEPTION_MSG_GOOGLE_STORE_TRANSACTION_ID_VALIDATION_FAILED: // 验证谷歌交易失败
  1708. result = "Verification of Google's Transaction failed."
  1709. case .EXCEPTION_PRICE_ERROR: // 价格错误
  1710. result = "Price error."
  1711. case .EXCEPTION_MSG_ORDER_CREATE_FAILED: // 订单创建失败
  1712. result = "Order creation failed."
  1713. case .USER_ALREADY_SUBSCRIBED_PLAN: // 该用户已经是PDF Reader Pro订阅版会员,确定再赠送吗
  1714. result = "This user is already a subscription member of PDF Reader Pro. Are you sure you want to share benefits?"
  1715. case .USER_ALREADY_SUBSCRIBED_PACKAGE: // 该用户已经是PDF Reader Pro永久版会员,无需再赠送哦
  1716. result = "This user is already a permanent member of PDF Reader Pro. You don't need to share benefits anymore."
  1717. case .EXCEPTION_MSG_USER_NOT_EXIST: //用户不存在
  1718. result = "User does not exist."
  1719. case .EXCEPTION_USER_ALREADY_LOTTERY: // 用户已经抽过奖啦
  1720. result = "The user already has a prize draw!"
  1721. case .UNKNOWN:
  1722. result = "UNKNOWN"
  1723. break;
  1724. case .SUCCESS:
  1725. result = "SUCCESS"
  1726. break;
  1727. default:
  1728. result = "Network error"
  1729. }
  1730. return result
  1731. }
  1732. }