KMMemberCenterManager.swift 89 KB

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