KMMemberCenterManager.swift 72 KB

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