KMMemberCenterManager.swift 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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. if code == 200 {
  537. complete(true, result)
  538. } else {
  539. complete(false, result)
  540. }
  541. }
  542. }
  543. /**
  544. @abstract 退出登录
  545. @param complete 回调
  546. */
  547. func userLogout(_ complete: @escaping KMMemberCenterComplete) {
  548. let token: String = KMMemberInfo.shared.access_token
  549. if token == "" {
  550. return
  551. }
  552. let uuid: String = GetHardwareUUID() ?? ""
  553. let urlString = String(format: "%@/member-system-sso/user/logout?deviceSign=%@&appId=1", configuration.activityBaseURL(),uuid)
  554. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  555. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  556. } completion: { [weak self] (task, responseObject, error) in
  557. var dic: NSDictionary = [:]
  558. if error == nil, let responseObject = responseObject as? NSDictionary {
  559. dic = responseObject
  560. } else {
  561. var info = responseObject
  562. if let error = error {
  563. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  564. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  565. }
  566. }
  567. dic = (info as? NSDictionary) ?? [:]
  568. }
  569. let code: Int = dic["code"] as? Int ?? 0
  570. let result_bool: Bool = dic["result"] as? Bool ?? false
  571. let message: String = dic["msg"] as? String ?? ""
  572. let result = KMMemberCenterResult(code: code, msg: message, result: result_bool)
  573. if code == 200 {
  574. complete(true, result)
  575. } else {
  576. complete(false, result)
  577. }
  578. }
  579. }
  580. /**
  581. @abstract 获取用户当前会员情况
  582. @param complete 回调
  583. */
  584. func userRightList(_ complete: @escaping KMMemberCenterComplete) {
  585. let token: String = KMMemberInfo.shared.access_token
  586. if token == "" {
  587. return
  588. }
  589. let urlString = configuration.activityBaseURL() + "/member-system-sso//user/rightList"
  590. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  591. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  592. } completion: { [weak self] (task, responseObject, error) in
  593. var dic: NSDictionary = [:]
  594. if error == nil, let responseObject = responseObject as? NSDictionary {
  595. dic = responseObject
  596. } else {
  597. var info = responseObject
  598. if let error = error {
  599. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  600. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  601. }
  602. }
  603. dic = (info as? NSDictionary) ?? [:]
  604. }
  605. let code: Int = dic["code"] as? Int ?? 0
  606. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  607. let message: String = dic["msg"] as? String ?? ""
  608. var results: [KMMemberUserInfoActiveVIP] = []
  609. for dict1 in result_Array {
  610. var activeVIP_id = ""
  611. var activeVIP_userId = ""
  612. var activeVIP_status = 0
  613. var activeVIP_endDate = ""
  614. var activeVIP_payType = 0
  615. var activeVIP_productName = ""
  616. var activeVIP_levels = ""
  617. var activeVIP_platforms = ""
  618. var activeVIP_point = 0
  619. var activeVIP_maxDeviceNum = 0
  620. var activeVIP_paymentModel = ""
  621. var activeVIP_isAi = ""
  622. if let token = dict1["id"] { activeVIP_id = token as! String }
  623. if let token = dict1["userId"] { activeVIP_userId = token as! String }
  624. if let token = dict1["status"] { activeVIP_status = token as! Int }
  625. if let token = dict1["endDate"] { activeVIP_endDate = token as! String }
  626. if let token = dict1["payType"] { activeVIP_payType = token as! Int }
  627. if let token = dict1["productName"] { activeVIP_productName = token as! String }
  628. if let token = dict1["levels"] { activeVIP_levels = token as! String }
  629. if let token = dict1["platforms"] { activeVIP_platforms = token as! String }
  630. if let token = dict1["point"] { activeVIP_point = token as! Int }
  631. if let token = dict1["maxDeviceNum"] { activeVIP_maxDeviceNum = token as! Int }
  632. if let token = dict1["paymentModel"] { activeVIP_paymentModel = token as! String }
  633. if let token = dict1["isAi"] { activeVIP_isAi = token as! String }
  634. 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)
  635. results.append(userInfoActiveVIP)
  636. }
  637. let result = KMMemberCenterResult(code: code, msg: message, activeVIPs: results)
  638. if code == 200 {
  639. complete(true, result)
  640. } else {
  641. complete(false, result)
  642. }
  643. }
  644. }
  645. /**
  646. @abstract 用户手动注销
  647. @param code 邮箱验证码
  648. @param complete 回调
  649. */
  650. func userLogOffForUser(code: String, _ complete: @escaping KMMemberCenterComplete) {
  651. let token: String = KMMemberInfo.shared.access_token
  652. if token == "" {
  653. return
  654. }
  655. let urlString = String(format: "%@/member-system-sso/user/logOffForUser?code=%@&appId=1", configuration.activityBaseURL(), code)
  656. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  657. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  658. } completion: { [weak self] (task, responseObject, error) in
  659. var dic: NSDictionary = [:]
  660. if error == nil, let responseObject = responseObject as? NSDictionary {
  661. dic = responseObject
  662. } else {
  663. var info = responseObject
  664. if let error = error {
  665. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  666. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  667. }
  668. }
  669. dic = (info as? NSDictionary) ?? [:]
  670. }
  671. let code: Int = dic["code"] as? Int ?? 0
  672. let message: String = dic["msg"] as? String ?? ""
  673. let result_dict: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  674. var currentTime = ""
  675. var logOffTime = ""
  676. if let token = result_dict["currentTime"] { currentTime = token as! String }
  677. if let token = result_dict["logOffTime"] { logOffTime = token as! String }
  678. let logOff = KMMemberLogOff(currentTime: currentTime, logOffTime: logOffTime)
  679. let result = KMMemberCenterResult(code: code, msg: message, logOff: logOff)
  680. if code == 200 {
  681. complete(true, result)
  682. } else {
  683. complete(false, result)
  684. }
  685. }
  686. }
  687. /**
  688. @abstract 撤销注销申请
  689. @param complete 回调
  690. */
  691. func userRevokeCancel(_ complete: @escaping KMMemberCenterComplete) {
  692. let token: String = KMMemberInfo.shared.access_token
  693. if token == "" {
  694. return
  695. }
  696. let urlString = configuration.activityBaseURL() + "/member-system-sso/user/revokeCancel"
  697. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: nil) { requestSerializer in
  698. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  699. } completion: { [weak self] (task, responseObject, error) in
  700. var dic: NSDictionary = [:]
  701. if error == nil, let responseObject = responseObject as? NSDictionary {
  702. dic = responseObject
  703. } else {
  704. var info = responseObject
  705. if let error = error {
  706. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  707. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  708. }
  709. }
  710. dic = (info as? NSDictionary) ?? [:]
  711. }
  712. let code: Int = dic["code"] as? Int ?? 0
  713. let message: String = dic["msg"] as? String ?? ""
  714. let result_Bool: Bool = dic["result"] as? Bool ?? false
  715. let result = KMMemberCenterResult(code: code, msg: message, result: result_Bool)
  716. if code == 200 {
  717. complete(true, result)
  718. } else {
  719. complete(false, result)
  720. }
  721. }
  722. }
  723. // MARK: 商品模块
  724. /**
  725. @abstract 获取上架中的产品
  726. @param isEducation 是否教育优惠
  727. @param complete 回调
  728. */
  729. func getListingProducts(isEducation: Int, _ complete: @escaping KMMemberProductComplete) {
  730. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getListingProducts"
  731. var platformId = "2"
  732. #if VERSION_FREE
  733. #if VERSION_DMG
  734. // DMG
  735. platformId = "2"
  736. #else
  737. // AppStore 免费版本
  738. #endif
  739. platformId = "3"
  740. #else
  741. // AppStore 付费版
  742. platformId = "5"
  743. #endif
  744. let params: [String: Any] = ["isEducation": isEducation,
  745. "platformId": platformId]
  746. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  747. } completion: { [weak self] (task, responseObject, error) in
  748. var dic: NSDictionary = [:]
  749. if error == nil, let responseObject = responseObject as? NSDictionary {
  750. dic = responseObject
  751. } else {
  752. var info = responseObject
  753. if let error = error {
  754. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  755. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  756. }
  757. }
  758. dic = (info as? NSDictionary) ?? [:]
  759. }
  760. let code: Int = dic["code"] as? Int ?? 0
  761. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  762. let message: String = dic["msg"] as? String ?? ""
  763. var results: [KMListingProductsModel] = []
  764. for dict1 in result_Array {
  765. var id = ""
  766. var productName = ""
  767. var price = ""
  768. var maxDeviceNum = 0
  769. var levels = ""
  770. var platforms = ""
  771. var productLineId = 0
  772. var paymentModel = 0
  773. var cycle = 0
  774. var code = ""
  775. var cnyPrice = ""
  776. if let token = dict1["id"] { id = token as! String }
  777. if let token = dict1["productName"] { productName = token as! String }
  778. if let token = dict1["price"] { price = token as! String }
  779. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as! Int }
  780. if let token = dict1["levels"] { levels = token as! String }
  781. if let token = dict1["platforms"] { platforms = token as! String }
  782. if let token = dict1["productLineId"] { productLineId = token as! Int }
  783. if let token = dict1["paymentModel"] { paymentModel = token as! Int }
  784. if let token = dict1["cycle"] { cycle = token as! Int }
  785. if let token = dict1["code"] { code = token as! String }
  786. if let token = dict1["cnyPrice"] { cnyPrice = token as! String }
  787. 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)
  788. results.append(products)
  789. }
  790. let result = KMMemberProductResult(code: code, msg: message, listingProducts: results)
  791. if code == 200 {
  792. complete(true, result)
  793. } else {
  794. complete(false, result)
  795. }
  796. }
  797. }
  798. /**
  799. @abstract 获取已登录用户购买商品价格
  800. @param productId 购买的产品id
  801. @param isEducation 是否教育优惠
  802. @param complete 回调
  803. */
  804. func getProductPriceForBuy(productId: String, isEducation: Int, userId: String, _ complete: @escaping KMMemberProductComplete) {
  805. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getProductPriceForBuy"
  806. let params: [String: Any] = ["productId": productId,
  807. "isEducation": isEducation,
  808. "userId": userId]
  809. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  810. } completion: { [weak self] (task, responseObject, error) in
  811. var dic: NSDictionary = [:]
  812. if error == nil, let responseObject = responseObject as? NSDictionary {
  813. dic = responseObject
  814. } else {
  815. var info = responseObject
  816. if let error = error {
  817. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  818. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  819. }
  820. }
  821. dic = (info as? NSDictionary) ?? [:]
  822. }
  823. let code: Int = dic["code"] as? Int ?? 0
  824. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  825. let message: String = dic["msg"] as? String ?? ""
  826. var results: [KMListingProductsModel] = []
  827. for dict1 in result_Array {
  828. var id = ""
  829. var productName = ""
  830. var price = ""
  831. var maxDeviceNum = 0
  832. var levels = ""
  833. var platforms = ""
  834. var productLineId = 0
  835. var paymentModel = 0
  836. var cycle = 0
  837. var code = ""
  838. var cnyPrice = ""
  839. if let token = dict1["id"] { id = token as! String }
  840. if let token = dict1["productName"] { productName = token as! String }
  841. if let token = dict1["price"] { price = token as! String }
  842. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as! Int }
  843. if let token = dict1["levels"] { levels = token as! String }
  844. if let token = dict1["platforms"] { platforms = token as! String }
  845. if let token = dict1["productLineId"] { productLineId = token as! Int }
  846. if let token = dict1["paymentModel"] { paymentModel = token as! Int }
  847. if let token = dict1["cycle"] { cycle = token as! Int }
  848. if let token = dict1["code"] { code = token as! String }
  849. if let token = dict1["cnyPrice"] { cnyPrice = token as! String }
  850. 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)
  851. results.append(products)
  852. }
  853. let result = KMMemberProductResult(code: code, msg: message, listingProducts: results)
  854. if code == 200 {
  855. complete(true, result)
  856. } else {
  857. complete(false, result)
  858. }
  859. }
  860. }
  861. /**
  862. @abstract 获取批量阶段购买价格
  863. @param productId 购买的产品id
  864. @param isEducation 是否教育优惠
  865. @param complete 回调
  866. */
  867. func getBatchProductPrice(productId: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  868. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getBatchProductPrice"
  869. let params: [String: Any] = ["productId": productId,
  870. "num": num]
  871. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  872. } completion: { [weak self] (task, responseObject, error) in
  873. var dic: NSDictionary = [:]
  874. if error == nil, let responseObject = responseObject as? NSDictionary {
  875. dic = responseObject
  876. } else {
  877. var info = responseObject
  878. if let error = error {
  879. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  880. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  881. }
  882. }
  883. dic = (info as? NSDictionary) ?? [:]
  884. }
  885. let code: Int = dic["code"] as? Int ?? 0
  886. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  887. let message: String = dic["msg"] as? String ?? ""
  888. var productId = ""
  889. var totalPrice = ""
  890. var price = ""
  891. var batchPrice = ""
  892. var cnyPrice = ""
  893. var cnyBatchPrice = ""
  894. var cnyTotalPrice = ""
  895. if let token = result["productId"] { productId = token as! String }
  896. if let token = result["totalPrice"] { totalPrice = token as! String }
  897. if let token = result["price"] { price = token as! String }
  898. if let token = result["batchPrice"] { batchPrice = token as! String }
  899. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  900. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as! String }
  901. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as! String }
  902. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  903. let result1 = KMMemberProductResult(code: code, msg: message, batchProductPrice: products)
  904. if code == 200 {
  905. complete(true, result1)
  906. } else {
  907. complete(false, result1)
  908. }
  909. }
  910. }
  911. /**
  912. @abstract 验证商品优惠券,返回商品优惠后价格
  913. @param productId 购买的产品id
  914. @param isEducation 是否教育优惠
  915. @param complete 回调
  916. */
  917. func checkCoupon(productId: String, userId: String, code: String, _ complete: @escaping KMMemberProductComplete) {
  918. let token: String = KMMemberInfo.shared.access_token
  919. if token == "" {
  920. return
  921. }
  922. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkCoupon"
  923. let params: [String: Any] = ["productId": productId,
  924. "userId": userId,
  925. "code": code]
  926. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  927. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  928. } completion: { [weak self] (task, responseObject, error) in
  929. var dic: NSDictionary = [:]
  930. if error == nil, let responseObject = responseObject as? NSDictionary {
  931. dic = responseObject
  932. } else {
  933. var info = responseObject
  934. if let error = error {
  935. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  936. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  937. }
  938. }
  939. dic = (info as? NSDictionary) ?? [:]
  940. }
  941. let code: Int = dic["code"] as? Int ?? 0
  942. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  943. let message: String = dic["msg"] as? String ?? ""
  944. var id = ""
  945. var productName = ""
  946. var price = ""
  947. var maxDeviceNum = 0
  948. var displayPrice = ""
  949. var levels = ""
  950. var platforms = ""
  951. var productLineId = 0
  952. var paymentModel = 0
  953. var cycle = 0
  954. var cnyPrice = ""
  955. var displayCnyPrice = ""
  956. if let token = result["id"] { id = token as! String }
  957. if let token = result["productName"] { productName = token as! String }
  958. if let token = result["price"] { price = token as! String }
  959. if let token = result["maxDeviceNum"] { maxDeviceNum = token as! Int }
  960. if let token = result["displayPrice"] { displayPrice = token as! String }
  961. if let token = result["levels"] { levels = token as! String }
  962. if let token = result["platforms"] { platforms = token as! String }
  963. if let token = result["productLineId"] { productLineId = token as! Int }
  964. if let token = result["paymentModel"] { paymentModel = token as! Int }
  965. if let token = result["cycle"] { cycle = token as! Int }
  966. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  967. if let token = result["displayCnyPrice"] { displayCnyPrice = token as! String }
  968. 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)
  969. let result1 = KMMemberProductResult(code: code, msg: message, checkCoupon: products)
  970. if code == 200 {
  971. complete(true, result1)
  972. } else {
  973. complete(false, result1)
  974. }
  975. }
  976. }
  977. /**
  978. @abstract 获取批量阶段购买价格
  979. @param productId 购买的产品id
  980. @param isEducation 是否教育优惠
  981. @param complete 回调
  982. */
  983. func checkEducation(email: String, _ complete: @escaping KMMemberProductComplete) {
  984. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkEducation"
  985. let params: [String: Any] = ["email": email]
  986. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  987. } completion: { [weak self] (task, responseObject, error) in
  988. var dic: NSDictionary = [:]
  989. if error == nil, let responseObject = responseObject as? NSDictionary {
  990. dic = responseObject
  991. } else {
  992. var info = responseObject
  993. if let error = error {
  994. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  995. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  996. }
  997. }
  998. dic = (info as? NSDictionary) ?? [:]
  999. }
  1000. let code: Int = dic["code"] as? Int ?? 0
  1001. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1002. let message: String = dic["msg"] as? String ?? ""
  1003. var productId = ""
  1004. var totalPrice = ""
  1005. var price = ""
  1006. var batchPrice = ""
  1007. var cnyPrice = ""
  1008. var cnyBatchPrice = ""
  1009. var cnyTotalPrice = ""
  1010. if let token = result["productId"] { productId = token as! String }
  1011. if let token = result["totalPrice"] { totalPrice = token as! String }
  1012. if let token = result["price"] { price = token as! String }
  1013. if let token = result["batchPrice"] { batchPrice = token as! String }
  1014. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  1015. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as! String }
  1016. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as! String }
  1017. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1018. let result1 = KMMemberProductResult(code: code, msg: message, batchProductPrice: products)
  1019. if code == 200 {
  1020. complete(true, result1)
  1021. } else {
  1022. complete(false, result1)
  1023. }
  1024. }
  1025. }
  1026. // MARK: 个人中心模块
  1027. // MARK: 订单模块
  1028. /**
  1029. @abstract 支付后查询订单状态
  1030. @param orderId 订单id
  1031. @param complete 回调
  1032. */
  1033. func getStateByOrderId(orderId: String, _ complete: @escaping KMMemberProductComplete) {
  1034. let token: String = KMMemberInfo.shared.access_token
  1035. if token == "" {
  1036. return
  1037. }
  1038. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getStateByOrderId"
  1039. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  1040. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1041. } completion: { [weak self] (task, responseObject, error) in
  1042. var dic: NSDictionary = [:]
  1043. if error == nil, let responseObject = responseObject as? NSDictionary {
  1044. dic = responseObject
  1045. } else {
  1046. var info = responseObject
  1047. if let error = error {
  1048. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1049. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1050. }
  1051. }
  1052. dic = (info as? NSDictionary) ?? [:]
  1053. }
  1054. let code: Int = dic["code"] as? Int ?? 0
  1055. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1056. let message: String = dic["msg"] as? String ?? ""
  1057. var status = ""
  1058. if let token = result["status"] { status = token as! String }
  1059. let result1 = KMMemberProductResult(code: code, msg: message, result: status)
  1060. if code == 200 {
  1061. complete(true, result1)
  1062. } else {
  1063. complete(false, result1)
  1064. }
  1065. }
  1066. }
  1067. /**
  1068. @abstract 创建订单(买断、订阅)
  1069. @param productId 产品id
  1070. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1071. @param price 价格
  1072. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1073. @param couponCode 优惠卷
  1074. @param num 批量购买数量
  1075. @param complete 回调
  1076. */
  1077. func createOrder(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1078. let token: String = KMMemberInfo.shared.access_token
  1079. if token == "" {
  1080. return
  1081. }
  1082. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createOrder"
  1083. let params: [String: Any] = ["productId": productId,
  1084. "paymentMethod": paymentMethod,
  1085. "price": price,
  1086. "discountFlag": discountFlag,
  1087. "couponCode": couponCode,
  1088. "num": num]
  1089. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1090. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1091. } completion: { [weak self] (task, responseObject, error) in
  1092. var dic: NSDictionary = [:]
  1093. if error == nil, let responseObject = responseObject as? NSDictionary {
  1094. dic = responseObject
  1095. } else {
  1096. var info = responseObject
  1097. if let error = error {
  1098. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1099. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1100. }
  1101. }
  1102. dic = (info as? NSDictionary) ?? [:]
  1103. }
  1104. let code: Int = dic["code"] as? Int ?? 0
  1105. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1106. let message: String = dic["msg"] as? String ?? ""
  1107. var thirdOrderNo = ""
  1108. var orderId = ""
  1109. var payHref = ""
  1110. if code == 200 {
  1111. if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1112. if let token = result["orderId"] { orderId = token as! String }
  1113. if let token = result["payHref"] { payHref = token as! String }
  1114. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1115. let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1116. complete(true, result1)
  1117. } else {
  1118. let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1119. complete(false, result1)
  1120. }
  1121. }
  1122. }
  1123. /**
  1124. @abstract 获取订单列表
  1125. @param orderStatus 0全部1待支付
  1126. @param complete 回调
  1127. */
  1128. func getOrderListByStatus(orderStatus: Int, _ complete: @escaping KMMemberProductComplete) {
  1129. let token: String = KMMemberInfo.shared.access_token
  1130. if token == "" {
  1131. return
  1132. }
  1133. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getOrderListByStatus"
  1134. let params: [String: Any] = ["orderStatus": orderStatus]
  1135. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  1136. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1137. } completion: { [weak self] (task, responseObject, error) in
  1138. var dic: NSDictionary = [:]
  1139. if error == nil, let responseObject = responseObject as? NSDictionary {
  1140. dic = responseObject
  1141. } else {
  1142. var info = responseObject
  1143. if let error = error {
  1144. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1145. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1146. }
  1147. }
  1148. dic = (info as? NSDictionary) ?? [:]
  1149. }
  1150. let code: Int = dic["code"] as? Int ?? 0
  1151. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1152. let message: String = dic["msg"] as? String ?? ""
  1153. // var thirdOrderNo = ""
  1154. // var orderId = ""
  1155. // var payHref = ""
  1156. // if code == 200 {
  1157. // if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1158. // if let token = result["orderId"] { orderId = token as! String }
  1159. // if let token = result["payHref"] { payHref = token as! String }
  1160. // let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1161. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1162. // complete(true, result1)
  1163. // } else {
  1164. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1165. // complete(false, result1)
  1166. // }
  1167. }
  1168. }
  1169. // MARK: 评估模块
  1170. // MARK: 活动模块
  1171. // MARK: 翻译模块
  1172. // MARK: AI模块
  1173. // MARK: 会员赠送模块
  1174. // MARK: Error Code
  1175. static func typeOfMessage(type: KMMemberCenterErrorCodeType?) -> String {
  1176. var result: String = ""
  1177. switch type {
  1178. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  1179. result = "无效的token或者token已过期"
  1180. break;
  1181. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  1182. result = "您的登录设备已达上限,请退出其他设备重新登录"
  1183. break;
  1184. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  1185. result = "用户没有登录,请登录后再访问"
  1186. break;
  1187. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  1188. result = "email code error!"
  1189. break;
  1190. case .EMAIL_REGISTER_ERROR:
  1191. result = "Register error!"
  1192. break;
  1193. case .EXCEPTION_MSG_PASSWORD_ERROR:
  1194. result = "密码错误,您可以检查大小写状态"
  1195. break;
  1196. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  1197. result = "code action error"
  1198. break;
  1199. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  1200. result = "code send error"
  1201. break;
  1202. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  1203. result = "mail send error"
  1204. break;
  1205. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  1206. result = "code send too quickly"
  1207. break;
  1208. case .EXCEPTION_MSG_EMAIL_EXIST:
  1209. result = "该邮箱已有账号,请前往登录"
  1210. break;
  1211. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  1212. result = "当前账号未注册,点击下一步我们将为您注册账号"
  1213. break;
  1214. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  1215. result = "注销中,无法登录,若有问题可联系客服"
  1216. break;
  1217. case .EXCEPTION_MSG_EMAIL_INVALID:
  1218. result = "请输入正确的邮箱格式"
  1219. break;
  1220. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  1221. result = "密码不一致,请重新输入"
  1222. break;
  1223. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  1224. result = "密码请包含6~24个字符"
  1225. break;
  1226. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  1227. result = "密码请包含6~24个字符"
  1228. break;
  1229. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  1230. result = "please add auth config"
  1231. break;
  1232. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  1233. result = "您目前还在订阅期内,暂时无法注销,请订阅期结束后再点击注销账号"
  1234. break;
  1235. case .EXCEPTION_TIME_TRANSFER_ERROR:
  1236. result = "时间转换失败"
  1237. break;
  1238. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY:
  1239. result = "密码不能包含空格"
  1240. break;
  1241. case .UNKNOWN:
  1242. result = "UNKNOWN"
  1243. break;
  1244. case .SUCCESS:
  1245. result = "SUCCESS"
  1246. break;
  1247. default:
  1248. result = "Network error"
  1249. }
  1250. return result
  1251. }
  1252. }