KMMemberCenterManager.swift 67 KB

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