KMMemberCenterManager.swift 64 KB

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