KMMemberCenterManager.swift 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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. }
  33. @objc enum KMVerificationCodeType : Int {
  34. case login = 0 // 登录验证码
  35. case reset // 重置密码验证码
  36. case logout // 注销验证码
  37. }
  38. @objc enum KMBuyPlatformType : Int {
  39. case web = 0 // 官网
  40. case dmg // 重置密码验证码
  41. case macLite // Mac store免费版
  42. case exe
  43. case macPro // Mac store付费版
  44. case iOSLite // iOS免费版
  45. case iOSPro // iOS付费版
  46. case Android
  47. }
  48. typealias KMMemberCenterComplete = (_ success: Bool, _ result: KMMemberCenterResult?) -> Void
  49. typealias KMMemberProductComplete = (_ success: Bool, _ result: KMMemberProductResult?) -> Void
  50. class KMMemberCenterManager: NSObject {
  51. static let manager = KMMemberCenterManager()
  52. private let configuration: KMMemberCenterConfig = KMMemberCenterConfig()
  53. // MARK: 登录模块
  54. /**
  55. @abstract 新会员系统重置密码
  56. @param email 邮箱
  57. @param verifyCode 验证码
  58. @param password 密码
  59. @param complete 回调
  60. */
  61. func resetPassword(email: String, verifyCode: String, password: String, _ complete: @escaping KMMemberCenterComplete) {
  62. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/memberResetPassword"
  63. // var escapedString = q
  64. // escapedString = String(data: escapedString.data(using: .utf8)!, encoding: .utf8)!
  65. let paraDict: [String: Any] = ["email": email,
  66. "verifyCode": verifyCode,
  67. "appId": "1",
  68. "password": password]
  69. let postData = try! JSONSerialization.data(withJSONObject: paraDict)
  70. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  71. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  72. request.httpMethod = "POST"
  73. request.httpBody = postData
  74. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  75. guard let data = data else {
  76. print(String(describing: error))
  77. return
  78. }
  79. let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
  80. if jsonObject != nil {
  81. let code: Int = jsonObject!["code"] as? Int ?? 0
  82. let result_bool: Bool = jsonObject!["result"] as? Bool ?? false
  83. let message: String = jsonObject!["msg"] as? String ?? ""
  84. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  85. if code == 200 {
  86. complete(true, result)
  87. } else {
  88. complete(false, result)
  89. }
  90. } else {
  91. complete(false, KMMemberCenterResult(code: 0, msg: "unknown error", result: false))
  92. }
  93. }
  94. task.resume()
  95. // let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/memberResetPassword"
  96. // let params: [String: Any] = ["email": email,
  97. // "verifyCode": verifyCode,
  98. // "appId": "1",
  99. // "password": password]
  100. // KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  101. //
  102. // } completion: { [weak self] (task, responseObject, error) in
  103. // var dic: NSDictionary = [:]
  104. // if error == nil, let responseObject = responseObject as? NSDictionary {
  105. // dic = responseObject
  106. // } else {
  107. // var info = responseObject
  108. // if let error = error {
  109. // if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  110. // info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  111. // }
  112. // }
  113. // dic = (info as? NSDictionary) ?? [:]
  114. // }
  115. //
  116. // let code: Int = dic["code"] as? Int ?? 0
  117. // let result_bool: Bool = dic["result"] as? Bool ?? false
  118. // let message: String = dic["msg"] as? String ?? ""
  119. //
  120. // let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  121. //// let error = NSError(domain: message, code: code)
  122. // if code == 200 {
  123. // complete(true, result)
  124. // } else {
  125. // complete(false, result)
  126. // }
  127. // }
  128. }
  129. /**
  130. @abstract 验证邮箱(重置密码输入邮箱点击下一步时校验邮箱是否注册)
  131. @param email 邮箱
  132. @param complete 回调
  133. */
  134. func emailVerification(email: String, _ complete: @escaping KMMemberCenterComplete) {
  135. let urlString = String(format: "%@/member-system-sso/auth/validEmail?email=%@", configuration.activityBaseURL(), email)
  136. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  137. } completion: { (task, responseObject, error) in
  138. var dic: NSDictionary = [:]
  139. if error == nil, let responseObject = responseObject as? NSDictionary {
  140. dic = responseObject
  141. } else {
  142. var info = responseObject
  143. if let error = error {
  144. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  145. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  146. }
  147. }
  148. dic = (info as? NSDictionary) ?? [:]
  149. }
  150. let code: Int = dic["code"] as? Int ?? 0
  151. let result_bool: Bool = dic["result"] as? Bool ?? false
  152. let message: String = dic["msg"] as? String ?? ""
  153. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  154. if code == 200 {
  155. complete(true, result)
  156. } else {
  157. complete(false, result)
  158. }
  159. }
  160. }
  161. /**
  162. @abstract 获取邮箱验证码
  163. @param action 验证码用途
  164. @param receiver 邮箱号
  165. @param complete 回调
  166. */
  167. func getVerificationCode(action: KMVerificationCodeType, receiver: String, _ complete: @escaping KMMemberCenterComplete) {
  168. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/getVerifyCode"
  169. var actionType = "member_login"
  170. if action == .reset {
  171. actionType = "member_reset_password"
  172. } else if action == .logout {
  173. actionType = "user_log_off"
  174. }
  175. let params: [String: Any] = ["action": actionType,
  176. "type": 0,
  177. "receiver": receiver,
  178. "appId": "1"]
  179. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  180. } completion: { (task, responseObject, error) in
  181. var dic: NSDictionary = [:]
  182. if error == nil, let responseObject = responseObject as? NSDictionary {
  183. dic = responseObject
  184. } else {
  185. var info = responseObject
  186. if let error = error {
  187. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  188. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  189. }
  190. }
  191. dic = (info as? NSDictionary) ?? [:]
  192. }
  193. let code: Int = dic["code"] as? Int ?? 0
  194. let result_bool: Bool = dic["result"] as? Bool ?? false
  195. let message: String = dic["msg"] as? String ?? ""
  196. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  197. if code == 200 {
  198. complete(true, result)
  199. } else {
  200. complete(false, result)
  201. }
  202. }
  203. }
  204. /**
  205. @abstract 实时检查邮件验证码
  206. @param type 验证码用途
  207. @param account 账户
  208. @param code 验证码
  209. @param complete 回调
  210. */
  211. func checkVerificationCode(type: KMVerificationCodeType, account: String, code: String, _ complete: @escaping KMMemberCenterComplete) {
  212. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/isEmailCodeValid"
  213. var actionType = "member_login"
  214. if type == .reset {
  215. actionType = "member_reset_password"
  216. } else if type == .logout {
  217. actionType = "user_log_off"
  218. }
  219. let params: [String: Any] = ["type": actionType,
  220. "account": account,
  221. "code": code,
  222. "appId": "1"]
  223. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  224. } completion: { [weak self] (task, responseObject, error) in
  225. var dic: NSDictionary = [:]
  226. if error == nil, let responseObject = responseObject as? NSDictionary {
  227. dic = responseObject
  228. } else {
  229. var info = responseObject
  230. if let error = error {
  231. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  232. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  233. }
  234. }
  235. dic = (info as? NSDictionary) ?? [:]
  236. }
  237. let code: Int = dic["code"] as? Int ?? 0
  238. let result_bool: Bool = dic["result"] as? Bool ?? false
  239. let message: String = dic["msg"] as? String ?? ""
  240. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  241. if code == 200 {
  242. complete(true, result)
  243. } else {
  244. complete(false, result)
  245. }
  246. }
  247. }
  248. /**
  249. @abstract 邮箱验证码登录 & 密码登录
  250. @param email 邮箱
  251. @param code 验证码/密码
  252. @param type 密码类型(验证码?密码?)
  253. @param complete 回调
  254. */
  255. func emailLogin(email: String, code: String, type: KMSignUpState, _ complete: @escaping KMMemberCenterComplete) {
  256. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/memberLogin"
  257. let uuid: String = GetHardwareUUID() ?? ""
  258. var params: [String: Any] = [:]
  259. if type == .verificationCode {
  260. params = ["email": email,
  261. "code": code,
  262. "appId": "1",
  263. "deviceSign": uuid,
  264. "deviceName": NSFullUserName(),
  265. "model": "mac"]
  266. } else {
  267. params = ["email": email,
  268. "password": code,
  269. "appId": "1",
  270. "deviceSign": uuid,
  271. "deviceName": NSFullUserName(),
  272. "model": "mac"]
  273. }
  274. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  275. // requestSerializer.setValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  276. } completion: { [weak self] (task, responseObject, error) in
  277. var dic: NSDictionary = [:]
  278. if error == nil, let responseObject = responseObject as? NSDictionary {
  279. dic = responseObject
  280. } else {
  281. var info = responseObject
  282. if let error = error {
  283. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  284. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  285. }
  286. }
  287. dic = (info as? NSDictionary) ?? [:]
  288. }
  289. let code: Int = dic["code"] as? Int ?? 0
  290. let result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  291. let message: String = dic["msg"] as? String ?? ""
  292. var refresh_token = ""
  293. var access_token = ""
  294. var token_type = ""
  295. var expires_in = ""
  296. var scope = ""
  297. if let token = result_dict["refresh_token"] { refresh_token = token as! String }
  298. if let token = result_dict["access_token"] { access_token = token as! String }
  299. if let token = result_dict["token_type"] { token_type = token as! String }
  300. if let token = result_dict["expires_in"] { expires_in = token as! String }
  301. let loginResult = KMMemberLoginResult(refreshToken: refresh_token, accessToken: access_token, tokenType: token_type, expiresIn: expires_in)
  302. let result = KMMemberCenterResult(loginResult: loginResult)
  303. result.code = code
  304. result.msg = message
  305. // let error = NSError(domain: message, code: code)
  306. if code == 200 {
  307. complete(true, result)
  308. } else {
  309. complete(false, result)
  310. }
  311. }
  312. }
  313. /**
  314. @abstract 获取用户登录设备列表
  315. @param email 邮箱
  316. @param complete 回调
  317. */
  318. func getUserDeviceList(email: String, _ complete: @escaping KMMemberCenterComplete) {
  319. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/getLoginDeviceList"
  320. let params: [String: Any] = ["email": email,
  321. "appId": "1"]
  322. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  323. } completion: { [weak self] (task, responseObject, error) in
  324. var dic: NSDictionary = [:]
  325. if error == nil, let responseObject = responseObject as? NSDictionary {
  326. dic = responseObject
  327. } else {
  328. var info = responseObject
  329. if let error = error {
  330. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  331. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  332. }
  333. }
  334. dic = (info as? NSDictionary) ?? [:]
  335. }
  336. let code: Int = dic["code"] as? Int ?? 0
  337. let result_dict: Dictionary = dic["result"] as? Dictionary ?? [:]
  338. let message: String = dic["msg"] as? String ?? ""
  339. let deviceList: [Dictionary] = result_dict["deviceList"] as? [Dictionary] ?? [[:]]
  340. var deviceLists: [KMMemberDeviceList] = []
  341. for dict in deviceList {
  342. var device_id = ""
  343. var productId = ""
  344. var uniqueSn = ""
  345. var deviceName = ""
  346. if let token = dict["device_id"] { device_id = token as! String }
  347. if let token = dict["productId"] { productId = token as! String }
  348. if let token = dict["uniqueSn"] { uniqueSn = token as! String }
  349. if let token = dict["deviceName"] { deviceName = token as! String }
  350. let list = KMMemberDeviceList(deviceId: device_id, productId: productId, uniqueSn: uniqueSn, appId: deviceName)
  351. deviceLists.append(list)
  352. }
  353. var maxDeviceNum = 0
  354. if let token = result_dict["maxDeviceNum"] {
  355. maxDeviceNum = token as! Int
  356. }
  357. let result = KMMemberCenterResult(deviceListResult: deviceLists, maxDeviceNum: maxDeviceNum)
  358. if code == 200 {
  359. complete(true, result)
  360. } else {
  361. complete(false, result)
  362. }
  363. }
  364. }
  365. /**
  366. @abstract 登出用户的指定设备
  367. @param deviceId 设备记录ID
  368. @param complete 回调
  369. */
  370. func logoutDevice(deviceId: String, complete: @escaping KMMemberCenterComplete) {
  371. let urlString = configuration.activityBaseURL() + "/member-system-sso/auth/logoutDevice"
  372. let params: [String: Any] = ["deviceId": deviceId]
  373. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  374. } completion: { [weak self] (task, responseObject, error) in
  375. var dic: NSDictionary = [:]
  376. if error == nil, let responseObject = responseObject as? NSDictionary {
  377. dic = responseObject
  378. } else {
  379. var info = responseObject
  380. if let error = error {
  381. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  382. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  383. }
  384. }
  385. dic = (info as? NSDictionary) ?? [:]
  386. }
  387. let code: Int = dic["code"] as? Int ?? 0
  388. let resultStr: String = dic["result"] as? String ?? ""
  389. let message: String = dic["msg"] as? String ?? ""
  390. let result = KMMemberCenterResult(code: code, msg: message, resultStr: resultStr)
  391. if code == 200 {
  392. complete(true, result)
  393. } else {
  394. complete(false, result)
  395. }
  396. }
  397. }
  398. // MARK: 个人信息模块
  399. /**
  400. @abstract 获取老带新已邀请人数
  401. @param complete 回调
  402. */
  403. func getInviteNum(_ complete: @escaping KMMemberCenterComplete) {
  404. let token: String = KMMemberInfo.shared.access_token
  405. if token == "" {
  406. return
  407. }
  408. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/getInviteNum"
  409. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  410. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  411. } completion: { [weak self] (task, responseObject, error) in
  412. var dic: NSDictionary = [:]
  413. if error == nil, let responseObject = responseObject as? NSDictionary {
  414. dic = responseObject
  415. } else {
  416. var info = responseObject
  417. if let error = error {
  418. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  419. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  420. }
  421. }
  422. dic = (info as? NSDictionary) ?? [:]
  423. }
  424. let code: Int = dic["code"] as? Int ?? 0
  425. let resultInt: Int = dic["result"] as? Int ?? 0
  426. let message: String = dic["msg"] as? String ?? ""
  427. let result = KMMemberCenterResult(code: code, msg: message, resultInt: resultInt)
  428. if code == 200 {
  429. complete(true, result)
  430. } else {
  431. complete(false, result)
  432. }
  433. }
  434. }
  435. /**
  436. @abstract 获取用户个人信息
  437. @param complete 回调
  438. */
  439. func userInfo(_ complete: @escaping KMMemberCenterComplete) {
  440. let token: String = KMMemberInfo.shared.access_token
  441. if token == "" {
  442. return
  443. }
  444. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/info"
  445. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  446. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  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 result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  462. let message: String = dic["msg"] as? String ?? ""
  463. var userId = ""
  464. var email = ""
  465. var fullName = ""
  466. var activeVIP = KMMemberUserInfo()
  467. var aiPoint = 0
  468. var validFlag = ""
  469. var isHaveAIDiscount = ""
  470. var canTrail = ""
  471. if let token = result_dict["id"] { userId = token as! String }
  472. if let token = result_dict["email"] { email = token as! String }
  473. if let token = result_dict["fullName"] { fullName = token as! String }
  474. let activeVIP_dict: NSDictionary = result_dict["activeVIP"] as? NSDictionary ?? [:]
  475. var activeVIP_id = ""
  476. var activeVIP_userId = ""
  477. var activeVIP_status = 0
  478. var activeVIP_endDate = ""
  479. var activeVIP_payType = 0
  480. var activeVIP_productName = ""
  481. var activeVIP_levels = ""
  482. var activeVIP_platforms = ""
  483. var activeVIP_point = 0
  484. var activeVIP_maxDeviceNum = 0
  485. var activeVIP_paymentModel = ""
  486. var activeVIP_isAi = ""
  487. var activeVIP_cycle = 0
  488. if let token = activeVIP_dict["id"] as? String { activeVIP_id = token }
  489. if let token = activeVIP_dict["userId"] as? String { activeVIP_userId = token }
  490. if let token = activeVIP_dict["status"] as? Int { activeVIP_status = token }
  491. if let token = activeVIP_dict["endDate"] as? String { activeVIP_endDate = token }
  492. if let token = activeVIP_dict["payType"] as? Int { activeVIP_payType = token }
  493. if let token = activeVIP_dict["productName"] as? String { activeVIP_productName = token }
  494. if let token = activeVIP_dict["levels"] as? String { activeVIP_levels = token }
  495. if let token = activeVIP_dict["platforms"] as? String { activeVIP_platforms = token }
  496. if let token = activeVIP_dict["point"] as? Int { activeVIP_point = token }
  497. if let token = activeVIP_dict["maxDeviceNum"] as? Int { activeVIP_maxDeviceNum = token }
  498. if let token = activeVIP_dict["paymentModel"] as? String { activeVIP_paymentModel = token }
  499. if let token = activeVIP_dict["isAi"] as? String { activeVIP_isAi = token }
  500. if let token = activeVIP_dict["cycle"] as? Int { activeVIP_cycle = token }
  501. 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)
  502. let activeAI_dict: NSDictionary = result_dict["activeAI"] as? NSDictionary ?? [:]
  503. var activeAI_id = ""
  504. var activeAI_userId = ""
  505. var activeAI_status = 0
  506. var activeAI_endDate = ""
  507. var activeAI_payType = 0
  508. var activeAI_productName = ""
  509. var activeAI_levels = ""
  510. var activeAI_platforms = ""
  511. var activeAI_point = 0
  512. var activeAI_maxDeviceNum = 0
  513. var activeAI_paymentModel = ""
  514. var activeAI_isAi = ""
  515. var activeAI_cycle = 0
  516. if let token = activeAI_dict["id"] as? String { activeAI_id = token }
  517. if let token = activeAI_dict["userId"] as? String { activeAI_userId = token }
  518. if let token = activeAI_dict["status"] as? Int { activeAI_status = token }
  519. if let token = activeAI_dict["endDate"] as? String { activeAI_endDate = token }
  520. if let token = activeAI_dict["payType"] as? Int { activeAI_payType = token }
  521. if let token = activeAI_dict["productName"] as? String { activeAI_productName = token }
  522. if let token = activeAI_dict["levels"] as? String { activeAI_levels = token }
  523. if let token = activeAI_dict["platforms"] as? String { activeAI_platforms = token }
  524. if let token = activeAI_dict["point"] as? Int { activeAI_point = token }
  525. if let token = activeAI_dict["maxDeviceNum"] as? Int { activeAI_maxDeviceNum = token }
  526. if let token = activeAI_dict["paymentModel"] as? String { activeAI_paymentModel = token }
  527. if let token = activeAI_dict["isAi"] as? String { activeAI_isAi = token }
  528. if let token = activeAI_dict["cycle"] as? Int { activeAI_cycle = token }
  529. 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)
  530. if let token = result_dict["aiPoint"] { aiPoint = token as! Int }
  531. if let token = result_dict["validFlag"] { validFlag = token as! String }
  532. if let token = result_dict["isHaveAIDiscount"] { isHaveAIDiscount = token as! String }
  533. if let token = result_dict["canTrail"] { canTrail = token as! String }
  534. let userInfo = KMMemberUserInfo(id: userId, email: email, fullName: fullName, activeVIP: [userInfoActiveVIP], activeAI: [userInfoActiveAI], aiPoint: aiPoint, validFlag: validFlag, isHaveAIDiscount: isHaveAIDiscount, canTrail: canTrail)
  535. let result = KMMemberCenterResult(code: code, msg: message, userInfo: userInfo)
  536. // let error = NSError(domain: message, code: code)
  537. if code == 200 {
  538. complete(true, result)
  539. } else {
  540. complete(false, result)
  541. }
  542. }
  543. }
  544. /**
  545. @abstract 退出登录
  546. @param complete 回调
  547. */
  548. func userLogout(_ complete: @escaping KMMemberCenterComplete) {
  549. let token: String = KMMemberInfo.shared.access_token
  550. if token == "" {
  551. return
  552. }
  553. let uuid: String = GetHardwareUUID() ?? ""
  554. let urlString = String(format: "%@/member-system-sso/user/logout?deviceSign=%@&appId=1", configuration.activityBaseURL(),uuid)
  555. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  556. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  557. } completion: { [weak self] (task, responseObject, error) in
  558. var dic: NSDictionary = [:]
  559. if error == nil, let responseObject = responseObject as? NSDictionary {
  560. dic = responseObject
  561. } else {
  562. var info = responseObject
  563. if let error = error {
  564. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  565. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  566. }
  567. }
  568. dic = (info as? NSDictionary) ?? [:]
  569. }
  570. let code: Int = dic["code"] as? Int ?? 0
  571. let result_bool: Bool = dic["result"] as? Bool ?? false
  572. let message: String = dic["msg"] as? String ?? ""
  573. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  574. if code == 200 {
  575. complete(true, result)
  576. } else {
  577. complete(false, result)
  578. }
  579. }
  580. }
  581. /**
  582. @abstract 获取用户当前会员情况
  583. @param complete 回调
  584. */
  585. func userRightList(_ complete: @escaping KMMemberCenterComplete) {
  586. let token: String = KMMemberInfo.shared.access_token
  587. if token == "" {
  588. return
  589. }
  590. let urlString = configuration.activityBaseURL() + "/member-system-sso//user/rightList"
  591. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  592. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  593. } completion: { [weak self] (task, responseObject, error) in
  594. var dic: NSDictionary = [:]
  595. if error == nil, let responseObject = responseObject as? NSDictionary {
  596. dic = responseObject
  597. } else {
  598. var info = responseObject
  599. if let error = error {
  600. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  601. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  602. }
  603. }
  604. dic = (info as? NSDictionary) ?? [:]
  605. }
  606. let code: Int = dic["code"] as? Int ?? 0
  607. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  608. let message: String = dic["msg"] as? String ?? ""
  609. var results: [KMMemberUserInfoActiveVIP] = []
  610. for dict1 in result_Array {
  611. var activeVIP_id = ""
  612. var activeVIP_userId = ""
  613. var activeVIP_status = 0
  614. var activeVIP_endDate = ""
  615. var activeVIP_payType = 0
  616. var activeVIP_productName = ""
  617. var activeVIP_levels = ""
  618. var activeVIP_platforms = ""
  619. var activeVIP_point = 0
  620. var activeVIP_maxDeviceNum = 0
  621. var activeVIP_paymentModel = ""
  622. var activeVIP_isAi = ""
  623. if let token = dict1["id"] { activeVIP_id = token as! String }
  624. if let token = dict1["userId"] { activeVIP_userId = token as! String }
  625. if let token = dict1["status"] { activeVIP_status = token as! Int }
  626. if let token = dict1["endDate"] { activeVIP_endDate = token as! String }
  627. if let token = dict1["payType"] { activeVIP_payType = token as! Int }
  628. if let token = dict1["productName"] { activeVIP_productName = token as! String }
  629. if let token = dict1["levels"] { activeVIP_levels = token as! String }
  630. if let token = dict1["platforms"] { activeVIP_platforms = token as! String }
  631. if let token = dict1["point"] { activeVIP_point = token as! Int }
  632. if let token = dict1["maxDeviceNum"] { activeVIP_maxDeviceNum = token as! Int }
  633. if let token = dict1["paymentModel"] { activeVIP_paymentModel = token as! String }
  634. if let token = dict1["isAi"] { activeVIP_isAi = token as! String }
  635. 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)
  636. results.append(userInfoActiveVIP)
  637. }
  638. let result = KMMemberCenterResult(code: code, msg: message, activeVIPs: results)
  639. if code == 200 {
  640. complete(true, result)
  641. } else {
  642. complete(false, result)
  643. }
  644. }
  645. }
  646. /**
  647. @abstract 用户手动注销
  648. @param code 邮箱验证码
  649. @param complete 回调
  650. */
  651. func userLogOffForUser(code: String, _ complete: @escaping KMMemberCenterComplete) {
  652. let token: String = KMMemberInfo.shared.access_token
  653. if token == "" {
  654. return
  655. }
  656. let urlString = String(format: "%@/member-system-sso/user/logOffForUser?code=%@&appId=1", configuration.activityBaseURL(), code)
  657. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  658. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  659. } completion: { [weak self] (task, responseObject, error) in
  660. var dic: NSDictionary = [:]
  661. if error == nil, let responseObject = responseObject as? NSDictionary {
  662. dic = responseObject
  663. } else {
  664. var info = responseObject
  665. if let error = error {
  666. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  667. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  668. }
  669. }
  670. dic = (info as? NSDictionary) ?? [:]
  671. }
  672. let code: Int = dic["code"] as? Int ?? 0
  673. let message: String = dic["msg"] as? String ?? ""
  674. let result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  675. var currentTime = ""
  676. var logOffTime = ""
  677. if let token = result_dict["currentTime"] { currentTime = token as! String }
  678. if let token = result_dict["logOffTime"] { logOffTime = token as! String }
  679. let logOff = KMMemberLogOff(currentTime: currentTime, logOffTime: logOffTime)
  680. let result = KMMemberCenterResult(code: code, msg: message, logOff: logOff)
  681. if code == 200 {
  682. complete(true, result)
  683. } else {
  684. complete(false, result)
  685. }
  686. }
  687. }
  688. /**
  689. @abstract 撤销注销申请
  690. @param complete 回调
  691. */
  692. func userRevokeCancel(_ complete: @escaping KMMemberCenterComplete) {
  693. let token: String = KMMemberInfo.shared.access_token
  694. if token == "" {
  695. return
  696. }
  697. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/revokeCancel"
  698. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  699. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  700. } completion: { [weak self] (task, responseObject, error) in
  701. var dic: NSDictionary = [:]
  702. if error == nil, let responseObject = responseObject as? NSDictionary {
  703. dic = responseObject
  704. } else {
  705. var info = responseObject
  706. if let error = error {
  707. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  708. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  709. }
  710. }
  711. dic = (info as? NSDictionary) ?? [:]
  712. }
  713. let code: Int = dic["code"] as? Int ?? 0
  714. let message: String = dic["msg"] as? String ?? ""
  715. let result_Bool: Bool = dic["result"] as? Bool ?? false
  716. let result = KMMemberCenterResult(code: code, msg: message, result: result_Bool)
  717. if code == 200 {
  718. complete(true, result)
  719. } else {
  720. complete(false, result)
  721. }
  722. }
  723. }
  724. // MARK: 商品模块
  725. /**
  726. @abstract 获取上架中的产品
  727. @param isEducation 是否教育优惠
  728. @param complete 回调
  729. */
  730. func getListingProducts(isEducation: Int, _ complete: @escaping KMMemberProductComplete) {
  731. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getListingProducts"
  732. var platformId = "2"
  733. #if VERSION_FREE
  734. #if VERSION_DMG
  735. // DMG
  736. platformId = "2"
  737. #else
  738. // AppStore 免费版本
  739. #endif
  740. platformId = "3"
  741. #else
  742. // AppStore 付费版
  743. platformId = "5"
  744. #endif
  745. let params: [String: Any] = ["isEducation": isEducation,
  746. "platformId": platformId]
  747. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  748. } completion: { [weak self] (task, responseObject, error) in
  749. var dic: NSDictionary = [:]
  750. if error == nil, let responseObject = responseObject as? NSDictionary {
  751. dic = responseObject
  752. } else {
  753. var info = responseObject
  754. if let error = error {
  755. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  756. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  757. }
  758. }
  759. dic = (info as? NSDictionary) ?? [:]
  760. }
  761. let code: Int = dic["code"] as? Int ?? 0
  762. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  763. let message: String = dic["msg"] as? String ?? ""
  764. var results: [KMListingProductsModel] = []
  765. for dict1 in result_Array {
  766. var id = ""
  767. var productName = ""
  768. var price = ""
  769. var maxDeviceNum = 0
  770. var levels = ""
  771. var platforms = ""
  772. var productLineId = 0
  773. var paymentModel = 0
  774. var cycle = 0
  775. var code = ""
  776. var cnyPrice = ""
  777. if let token = dict1["id"] { id = token as! String }
  778. if let token = dict1["productName"] { productName = token as! String }
  779. if let token = dict1["price"] { price = token as! String }
  780. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as! Int }
  781. if let token = dict1["levels"] { levels = token as! String }
  782. if let token = dict1["platforms"] { platforms = token as! String }
  783. if let token = dict1["productLineId"] { productLineId = token as! Int }
  784. if let token = dict1["paymentModel"] { paymentModel = token as! Int }
  785. if let token = dict1["cycle"] { cycle = token as! Int }
  786. if let token = dict1["code"] { code = token as! String }
  787. if let token = dict1["cnyPrice"] { cnyPrice = token as! String }
  788. let products = KMListingProductsModel(id: id, productName: productName, price: price, maxDeviceNum: maxDeviceNum, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, code: code, cnyPrice: cnyPrice)
  789. results.append(products)
  790. }
  791. let result = KMMemberProductResult(code: code, msg: message, listingProducts: results)
  792. if code == 200 {
  793. complete(true, result)
  794. } else {
  795. complete(false, result)
  796. }
  797. }
  798. }
  799. /**
  800. @abstract 获取已登录用户购买商品价格
  801. @param productId 购买的产品id
  802. @param isEducation 是否教育优惠
  803. @param complete 回调
  804. */
  805. func getProductPriceForBuy(productId: String, isEducation: Int, userId: String, _ complete: @escaping KMMemberProductComplete) {
  806. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getProductPriceForBuy"
  807. let params: [String: Any] = ["productId": productId,
  808. "isEducation": isEducation,
  809. "userId": userId]
  810. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  811. } completion: { [weak self] (task, responseObject, error) in
  812. var dic: NSDictionary = [:]
  813. if error == nil, let responseObject = responseObject as? NSDictionary {
  814. dic = responseObject
  815. } else {
  816. var info = responseObject
  817. if let error = error {
  818. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  819. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  820. }
  821. }
  822. dic = (info as? NSDictionary) ?? [:]
  823. }
  824. let code: Int = dic["code"] as? Int ?? 0
  825. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  826. let message: String = dic["msg"] as? String ?? ""
  827. var results: [KMListingProductsModel] = []
  828. for dict1 in result_Array {
  829. var id = ""
  830. var productName = ""
  831. var price = ""
  832. var maxDeviceNum = 0
  833. var levels = ""
  834. var platforms = ""
  835. var productLineId = 0
  836. var paymentModel = 0
  837. var cycle = 0
  838. var code = ""
  839. var cnyPrice = ""
  840. if let token = dict1["id"] { id = token as! String }
  841. if let token = dict1["productName"] { productName = token as! String }
  842. if let token = dict1["price"] { price = token as! String }
  843. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as! Int }
  844. if let token = dict1["levels"] { levels = token as! String }
  845. if let token = dict1["platforms"] { platforms = token as! String }
  846. if let token = dict1["productLineId"] { productLineId = token as! Int }
  847. if let token = dict1["paymentModel"] { paymentModel = token as! Int }
  848. if let token = dict1["cycle"] { cycle = token as! Int }
  849. if let token = dict1["code"] { code = token as! String }
  850. if let token = dict1["cnyPrice"] { cnyPrice = token as! String }
  851. let products = KMListingProductsModel(id: id, productName: productName, price: price, maxDeviceNum: maxDeviceNum, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, code: code, cnyPrice: cnyPrice)
  852. results.append(products)
  853. }
  854. let result = KMMemberProductResult(code: code, msg: message, listingProducts: results)
  855. if code == 200 {
  856. complete(true, result)
  857. } else {
  858. complete(false, result)
  859. }
  860. }
  861. }
  862. /**
  863. @abstract 获取批量阶段购买价格
  864. @param productId 购买的产品id
  865. @param isEducation 是否教育优惠
  866. @param complete 回调
  867. */
  868. func getBatchProductPrice(productId: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  869. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getBatchProductPrice"
  870. let params: [String: Any] = ["productId": productId,
  871. "num": num]
  872. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  873. } completion: { [weak self] (task, responseObject, error) in
  874. var dic: NSDictionary = [:]
  875. if error == nil, let responseObject = responseObject as? NSDictionary {
  876. dic = responseObject
  877. } else {
  878. var info = responseObject
  879. if let error = error {
  880. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  881. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  882. }
  883. }
  884. dic = (info as? NSDictionary) ?? [:]
  885. }
  886. let code: Int = dic["code"] as? Int ?? 0
  887. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  888. let message: String = dic["msg"] as? String ?? ""
  889. var productId = ""
  890. var totalPrice = ""
  891. var price = ""
  892. var batchPrice = ""
  893. var cnyPrice = ""
  894. var cnyBatchPrice = ""
  895. var cnyTotalPrice = ""
  896. if let token = result["productId"] { productId = token as! String }
  897. if let token = result["totalPrice"] { totalPrice = token as! String }
  898. if let token = result["price"] { price = token as! String }
  899. if let token = result["batchPrice"] { batchPrice = token as! String }
  900. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  901. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as! String }
  902. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as! String }
  903. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  904. let result1 = KMMemberProductResult(code: code, msg: message, batchProductPrice: products)
  905. if code == 200 {
  906. complete(true, result1)
  907. } else {
  908. complete(false, result1)
  909. }
  910. }
  911. }
  912. /**
  913. @abstract 验证商品优惠券,返回商品优惠后价格
  914. @param productId 购买的产品id
  915. @param isEducation 是否教育优惠
  916. @param complete 回调
  917. */
  918. func checkCoupon(productId: String, userId: String, code: String, _ complete: @escaping KMMemberProductComplete) {
  919. let token: String = KMMemberInfo.shared.access_token
  920. if token == "" {
  921. return
  922. }
  923. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkCoupon"
  924. let params: [String: Any] = ["productId": productId,
  925. "userId": userId,
  926. "code": code]
  927. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  928. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  929. } completion: { [weak self] (task, responseObject, error) in
  930. var dic: NSDictionary = [:]
  931. if error == nil, let responseObject = responseObject as? NSDictionary {
  932. dic = responseObject
  933. } else {
  934. var info = responseObject
  935. if let error = error {
  936. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  937. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  938. }
  939. }
  940. dic = (info as? NSDictionary) ?? [:]
  941. }
  942. let code: Int = dic["code"] as? Int ?? 0
  943. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  944. let message: String = dic["msg"] as? String ?? ""
  945. var id = ""
  946. var productName = ""
  947. var price = ""
  948. var maxDeviceNum = 0
  949. var displayPrice = ""
  950. var levels = ""
  951. var platforms = ""
  952. var productLineId = 0
  953. var paymentModel = 0
  954. var cycle = 0
  955. var cnyPrice = ""
  956. var displayCnyPrice = ""
  957. if let token = result["id"] { id = token as! String }
  958. if let token = result["productName"] { productName = token as! String }
  959. if let token = result["price"] { price = token as! String }
  960. if let token = result["maxDeviceNum"] { maxDeviceNum = token as! Int }
  961. if let token = result["displayPrice"] { displayPrice = token as! String }
  962. if let token = result["levels"] { levels = token as! String }
  963. if let token = result["platforms"] { platforms = token as! String }
  964. if let token = result["productLineId"] { productLineId = token as! Int }
  965. if let token = result["paymentModel"] { paymentModel = token as! Int }
  966. if let token = result["cycle"] { cycle = token as! Int }
  967. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  968. if let token = result["displayCnyPrice"] { displayCnyPrice = token as! String }
  969. 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)
  970. let result1 = KMMemberProductResult(code: code, msg: message, checkCoupon: products)
  971. if code == 200 {
  972. complete(true, result1)
  973. } else {
  974. complete(false, result1)
  975. }
  976. }
  977. }
  978. /**
  979. @abstract 获取批量阶段购买价格
  980. @param productId 购买的产品id
  981. @param isEducation 是否教育优惠
  982. @param complete 回调
  983. */
  984. func checkEducation(email: String, _ complete: @escaping KMMemberProductComplete) {
  985. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkEducation"
  986. let params: [String: Any] = ["email": email]
  987. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  988. } completion: { [weak self] (task, responseObject, error) in
  989. var dic: NSDictionary = [:]
  990. if error == nil, let responseObject = responseObject as? NSDictionary {
  991. dic = responseObject
  992. } else {
  993. var info = responseObject
  994. if let error = error {
  995. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  996. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  997. }
  998. }
  999. dic = (info as? NSDictionary) ?? [:]
  1000. }
  1001. let code: Int = dic["code"] as? Int ?? 0
  1002. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1003. let message: String = dic["msg"] as? String ?? ""
  1004. var productId = ""
  1005. var totalPrice = ""
  1006. var price = ""
  1007. var batchPrice = ""
  1008. var cnyPrice = ""
  1009. var cnyBatchPrice = ""
  1010. var cnyTotalPrice = ""
  1011. if let token = result["productId"] { productId = token as! String }
  1012. if let token = result["totalPrice"] { totalPrice = token as! String }
  1013. if let token = result["price"] { price = token as! String }
  1014. if let token = result["batchPrice"] { batchPrice = token as! String }
  1015. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  1016. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as! String }
  1017. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as! String }
  1018. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1019. let result1 = KMMemberProductResult(code: code, msg: message, batchProductPrice: products)
  1020. if code == 200 {
  1021. complete(true, result1)
  1022. } else {
  1023. complete(false, result1)
  1024. }
  1025. }
  1026. }
  1027. // MARK: 个人中心模块
  1028. // MARK: 订单模块
  1029. /**
  1030. @abstract 支付后查询订单状态
  1031. @param orderId 订单id
  1032. @param complete 回调
  1033. */
  1034. func getStateByOrderId(orderId: String, _ complete: @escaping KMMemberProductComplete) {
  1035. let token: String = KMMemberInfo.shared.access_token
  1036. if token == "" {
  1037. return
  1038. }
  1039. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getStateByOrderId"
  1040. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  1041. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1042. } completion: { [weak self] (task, responseObject, error) in
  1043. var dic: NSDictionary = [:]
  1044. if error == nil, let responseObject = responseObject as? NSDictionary {
  1045. dic = responseObject
  1046. } else {
  1047. var info = responseObject
  1048. if let error = error {
  1049. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1050. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1051. }
  1052. }
  1053. dic = (info as? NSDictionary) ?? [:]
  1054. }
  1055. let code: Int = dic["code"] as? Int ?? 0
  1056. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1057. let message: String = dic["msg"] as? String ?? ""
  1058. var status = ""
  1059. if let token = result["status"] { status = token as! String }
  1060. let result1 = KMMemberProductResult(code: code, msg: message, result: status)
  1061. if code == 200 {
  1062. complete(true, result1)
  1063. } else {
  1064. complete(false, result1)
  1065. }
  1066. }
  1067. }
  1068. /**
  1069. @abstract 创建订单(买断、订阅)
  1070. @param productId 产品id
  1071. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1072. @param price 价格
  1073. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1074. @param couponCode 优惠卷
  1075. @param num 批量购买数量
  1076. @param complete 回调
  1077. */
  1078. func createOrder(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1079. let token: String = KMMemberInfo.shared.access_token
  1080. if token == "" {
  1081. return
  1082. }
  1083. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createOrder"
  1084. let params: [String: Any] = ["productId": productId,
  1085. "paymentMethod": paymentMethod,
  1086. "price": price,
  1087. "discountFlag": discountFlag,
  1088. "couponCode": couponCode,
  1089. "num": num]
  1090. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1091. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1092. } completion: { [weak self] (task, responseObject, error) in
  1093. var dic: NSDictionary = [:]
  1094. if error == nil, let responseObject = responseObject as? NSDictionary {
  1095. dic = responseObject
  1096. } else {
  1097. var info = responseObject
  1098. if let error = error {
  1099. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1100. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1101. }
  1102. }
  1103. dic = (info as? NSDictionary) ?? [:]
  1104. }
  1105. let code: Int = dic["code"] as? Int ?? 0
  1106. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1107. let message: String = dic["msg"] as? String ?? ""
  1108. var thirdOrderNo = ""
  1109. var orderId = ""
  1110. var payHref = ""
  1111. if code == 200 {
  1112. if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1113. if let token = result["orderId"] { orderId = token as! String }
  1114. if let token = result["payHref"] { payHref = token as! String }
  1115. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1116. let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1117. complete(true, result1)
  1118. } else {
  1119. let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1120. complete(false, result1)
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. @abstract 获取订单列表
  1126. @param orderStatus 0全部1待支付
  1127. @param complete 回调
  1128. */
  1129. func getOrderListByStatus(orderStatus: Int, _ complete: @escaping KMMemberProductComplete) {
  1130. let token: String = KMMemberInfo.shared.access_token
  1131. if token == "" {
  1132. return
  1133. }
  1134. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getOrderListByStatus"
  1135. let params: [String: Any] = ["orderStatus": orderStatus]
  1136. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  1137. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1138. } completion: { [weak self] (task, responseObject, error) in
  1139. var dic: NSDictionary = [:]
  1140. if error == nil, let responseObject = responseObject as? NSDictionary {
  1141. dic = responseObject
  1142. } else {
  1143. var info = responseObject
  1144. if let error = error {
  1145. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1146. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1147. }
  1148. }
  1149. dic = (info as? NSDictionary) ?? [:]
  1150. }
  1151. let code: Int = dic["code"] as? Int ?? 0
  1152. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1153. let message: String = dic["msg"] as? String ?? ""
  1154. // var thirdOrderNo = ""
  1155. // var orderId = ""
  1156. // var payHref = ""
  1157. // if code == 200 {
  1158. // if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1159. // if let token = result["orderId"] { orderId = token as! String }
  1160. // if let token = result["payHref"] { payHref = token as! String }
  1161. // let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1162. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1163. // complete(true, result1)
  1164. // } else {
  1165. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1166. // complete(false, result1)
  1167. // }
  1168. }
  1169. }
  1170. // MARK: 评估模块
  1171. // MARK: 活动模块
  1172. // MARK: 翻译模块
  1173. // MARK: AI模块
  1174. // MARK: 会员赠送模块
  1175. // MARK: Error Code
  1176. static func typeOfMessage(type: KMMemberCenterErrorCodeType?) -> String {
  1177. var result: String = ""
  1178. switch type {
  1179. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  1180. result = "无效的token或者token已过期"
  1181. break;
  1182. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  1183. result = "您的登录设备已达上限,请退出其他设备重新登录"
  1184. break;
  1185. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  1186. result = "用户没有登录,请登录后再访问"
  1187. break;
  1188. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  1189. result = "email code error!"
  1190. break;
  1191. case .EMAIL_REGISTER_ERROR:
  1192. result = "Register error!"
  1193. break;
  1194. case .EXCEPTION_MSG_PASSWORD_ERROR:
  1195. result = "密码错误,您可以检查大小写状态"
  1196. break;
  1197. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  1198. result = "code action error"
  1199. break;
  1200. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  1201. result = "code send error"
  1202. break;
  1203. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  1204. result = "mail send error"
  1205. break;
  1206. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  1207. result = "code send too quickly"
  1208. break;
  1209. case .EXCEPTION_MSG_EMAIL_EXIST:
  1210. result = "该邮箱已有账号,请前往登录"
  1211. break;
  1212. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  1213. result = "当前账号未注册,点击下一步我们将为您注册账号"
  1214. break;
  1215. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  1216. result = "注销中,无法登录,若有问题可联系客服"
  1217. break;
  1218. case .EXCEPTION_MSG_EMAIL_INVALID:
  1219. result = "请输入正确的邮箱格式"
  1220. break;
  1221. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  1222. result = "密码不一致,请重新输入"
  1223. break;
  1224. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  1225. result = "密码请包含6~24个字符"
  1226. break;
  1227. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  1228. result = "密码请包含6~24个字符"
  1229. break;
  1230. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  1231. result = "please add auth config"
  1232. break;
  1233. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  1234. result = "您目前还在订阅期内,暂时无法注销,请订阅期结束后再点击注销账号"
  1235. break;
  1236. case .EXCEPTION_TIME_TRANSFER_ERROR:
  1237. result = "时间转换失败"
  1238. break;
  1239. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY:
  1240. result = "密码不能包含空格"
  1241. break;
  1242. case .UNKNOWN:
  1243. result = "UNKNOWN"
  1244. break;
  1245. case .SUCCESS:
  1246. result = "SUCCESS"
  1247. break;
  1248. default:
  1249. result = "Network error"
  1250. }
  1251. return result
  1252. }
  1253. }