KMMemberCenterManager.swift 64 KB

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