KMMemberCenterManager.swift 90 KB

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