KMMemberCenterManager.swift 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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 = "2"
  755. #else
  756. // AppStore 免费版本
  757. #endif
  758. platformId = "3"
  759. #else
  760. // AppStore 付费版
  761. platformId = "5"
  762. #endif
  763. let params: [String: Any] = ["isEducation": isEducation,
  764. "platformId": platformId]
  765. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  766. } completion: { [weak self] (task, responseObject, error) in
  767. var dic: NSDictionary = [:]
  768. if error == nil, let responseObject = responseObject as? NSDictionary {
  769. dic = responseObject
  770. } else {
  771. var info = responseObject
  772. if let error = error {
  773. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  774. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  775. }
  776. }
  777. dic = (info as? NSDictionary) ?? [:]
  778. }
  779. let code: Int = dic["code"] as? Int ?? 0
  780. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  781. let message: String = dic["msg"] as? String ?? ""
  782. var results: [KMListingProductsModel] = []
  783. for dict1 in result_Array {
  784. var id = ""
  785. var productName = ""
  786. var price = ""
  787. var maxDeviceNum = 0
  788. var levels = ""
  789. var platforms = ""
  790. var productLineId = 0
  791. var paymentModel = 0
  792. var cycle = 0
  793. var code = ""
  794. var cnyPrice = ""
  795. if let token = dict1["id"] { id = token as! String }
  796. if let token = dict1["productName"] { productName = token as! String }
  797. if let token = dict1["price"] { price = token as! String }
  798. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as! Int }
  799. if let token = dict1["levels"] { levels = token as! String }
  800. if let token = dict1["platforms"] { platforms = token as! String }
  801. if let token = dict1["productLineId"] { productLineId = token as! Int }
  802. if let token = dict1["paymentModel"] { paymentModel = token as! Int }
  803. if let token = dict1["cycle"] { cycle = token as! Int }
  804. if let token = dict1["code"] { code = token as! String }
  805. if let token = dict1["cnyPrice"] { cnyPrice = token as! String }
  806. 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)
  807. results.append(products)
  808. }
  809. let result = KMMemberProductResult(code: code, msg: message, listingProducts: results)
  810. if code == 200 {
  811. complete(true, result)
  812. } else {
  813. complete(false, result)
  814. }
  815. }
  816. }
  817. /**
  818. @abstract 获取已登录用户购买商品价格
  819. @param productId 购买的产品id
  820. @param isEducation 是否教育优惠
  821. @param complete 回调
  822. */
  823. func getProductPriceForBuy(productId: String, isEducation: Int, userId: String, _ complete: @escaping KMMemberProductComplete) {
  824. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getProductPriceForBuy"
  825. let params: [String: Any] = ["productId": productId,
  826. "isEducation": isEducation,
  827. "userId": userId]
  828. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  829. } completion: { [weak self] (task, responseObject, error) in
  830. var dic: NSDictionary = [:]
  831. if error == nil, let responseObject = responseObject as? NSDictionary {
  832. dic = responseObject
  833. } else {
  834. var info = responseObject
  835. if let error = error {
  836. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  837. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  838. }
  839. }
  840. dic = (info as? NSDictionary) ?? [:]
  841. }
  842. let code: Int = dic["code"] as? Int ?? 0
  843. let result_Array: [NSDictionary] = dic["result"] as? [NSDictionary] ?? [[:]]
  844. let message: String = dic["msg"] as? String ?? ""
  845. var results: [KMListingProductsModel] = []
  846. for dict1 in result_Array {
  847. var id = ""
  848. var productName = ""
  849. var price = ""
  850. var maxDeviceNum = 0
  851. var levels = ""
  852. var platforms = ""
  853. var productLineId = 0
  854. var paymentModel = 0
  855. var cycle = 0
  856. var code = ""
  857. var cnyPrice = ""
  858. if let token = dict1["id"] { id = token as! String }
  859. if let token = dict1["productName"] { productName = token as! String }
  860. if let token = dict1["price"] { price = token as! String }
  861. if let token = dict1["maxDeviceNum"] { maxDeviceNum = token as! Int }
  862. if let token = dict1["levels"] { levels = token as! String }
  863. if let token = dict1["platforms"] { platforms = token as! String }
  864. if let token = dict1["productLineId"] { productLineId = token as! Int }
  865. if let token = dict1["paymentModel"] { paymentModel = token as! Int }
  866. if let token = dict1["cycle"] { cycle = token as! Int }
  867. if let token = dict1["code"] { code = token as! String }
  868. if let token = dict1["cnyPrice"] { cnyPrice = token as! String }
  869. 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)
  870. results.append(products)
  871. }
  872. let result = KMMemberProductResult(code: code, msg: message, listingProducts: results)
  873. if code == 200 {
  874. complete(true, result)
  875. } else {
  876. complete(false, result)
  877. }
  878. }
  879. }
  880. /**
  881. @abstract 获取批量阶段购买价格
  882. @param productId 购买的产品id
  883. @param isEducation 是否教育优惠
  884. @param complete 回调
  885. */
  886. func getBatchProductPrice(productId: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  887. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/getBatchProductPrice"
  888. let params: [String: Any] = ["productId": productId,
  889. "num": num]
  890. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  891. } completion: { [weak self] (task, responseObject, error) in
  892. var dic: NSDictionary = [:]
  893. if error == nil, let responseObject = responseObject as? NSDictionary {
  894. dic = responseObject
  895. } else {
  896. var info = responseObject
  897. if let error = error {
  898. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  899. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  900. }
  901. }
  902. dic = (info as? NSDictionary) ?? [:]
  903. }
  904. let code: Int = dic["code"] as? Int ?? 0
  905. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  906. let message: String = dic["msg"] as? String ?? ""
  907. var productId = ""
  908. var totalPrice = ""
  909. var price = ""
  910. var batchPrice = ""
  911. var cnyPrice = ""
  912. var cnyBatchPrice = ""
  913. var cnyTotalPrice = ""
  914. if let token = result["productId"] { productId = token as! String }
  915. if let token = result["totalPrice"] { totalPrice = token as! String }
  916. if let token = result["price"] { price = token as! String }
  917. if let token = result["batchPrice"] { batchPrice = token as! String }
  918. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  919. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as! String }
  920. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as! String }
  921. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  922. let result1 = KMMemberProductResult(code: code, msg: message, batchProductPrice: products)
  923. if code == 200 {
  924. complete(true, result1)
  925. } else {
  926. complete(false, result1)
  927. }
  928. }
  929. }
  930. /**
  931. @abstract 验证商品优惠券,返回商品优惠后价格
  932. @param productId 购买的产品id
  933. @param isEducation 是否教育优惠
  934. @param complete 回调
  935. */
  936. func checkCoupon(productId: String, userId: String, code: String, _ complete: @escaping KMMemberProductComplete) {
  937. let token: String = KMMemberInfo.shared.access_token
  938. if token == "" {
  939. return
  940. }
  941. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkCoupon"
  942. let params: [String: Any] = ["productId": productId,
  943. "userId": userId,
  944. "code": code]
  945. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  946. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  947. } completion: { [weak self] (task, responseObject, error) in
  948. var dic: NSDictionary = [:]
  949. if error == nil, let responseObject = responseObject as? NSDictionary {
  950. dic = responseObject
  951. } else {
  952. var info = responseObject
  953. if let error = error {
  954. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  955. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  956. }
  957. }
  958. dic = (info as? NSDictionary) ?? [:]
  959. }
  960. let code: Int = dic["code"] as? Int ?? 0
  961. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  962. let message: String = dic["msg"] as? String ?? ""
  963. var id = ""
  964. var productName = ""
  965. var price = ""
  966. var maxDeviceNum = 0
  967. var displayPrice = ""
  968. var levels = ""
  969. var platforms = ""
  970. var productLineId = 0
  971. var paymentModel = 0
  972. var cycle = 0
  973. var cnyPrice = ""
  974. var displayCnyPrice = ""
  975. if let token = result["id"] { id = token as! String }
  976. if let token = result["productName"] { productName = token as! String }
  977. if let token = result["price"] { price = token as! String }
  978. if let token = result["maxDeviceNum"] { maxDeviceNum = token as! Int }
  979. if let token = result["displayPrice"] { displayPrice = token as! String }
  980. if let token = result["levels"] { levels = token as! String }
  981. if let token = result["platforms"] { platforms = token as! String }
  982. if let token = result["productLineId"] { productLineId = token as! Int }
  983. if let token = result["paymentModel"] { paymentModel = token as! Int }
  984. if let token = result["cycle"] { cycle = token as! Int }
  985. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  986. if let token = result["displayCnyPrice"] { displayCnyPrice = token as! String }
  987. 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)
  988. let result1 = KMMemberProductResult(code: code, msg: message, checkCoupon: products)
  989. if code == 200 {
  990. complete(true, result1)
  991. } else {
  992. complete(false, result1)
  993. }
  994. }
  995. }
  996. /**
  997. @abstract 获取批量阶段购买价格
  998. @param productId 购买的产品id
  999. @param isEducation 是否教育优惠
  1000. @param complete 回调
  1001. */
  1002. func checkEducation(email: String, _ complete: @escaping KMMemberProductComplete) {
  1003. let urlString = configuration.activityBaseURL() + "/pdf-office-website/web/checkEducation"
  1004. let params: [String: Any] = ["email": email]
  1005. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  1006. } completion: { [weak self] (task, responseObject, error) in
  1007. var dic: NSDictionary = [:]
  1008. if error == nil, let responseObject = responseObject as? NSDictionary {
  1009. dic = responseObject
  1010. } else {
  1011. var info = responseObject
  1012. if let error = error {
  1013. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1014. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1015. }
  1016. }
  1017. dic = (info as? NSDictionary) ?? [:]
  1018. }
  1019. let code: Int = dic["code"] as? Int ?? 0
  1020. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1021. let message: String = dic["msg"] as? String ?? ""
  1022. var productId = ""
  1023. var totalPrice = ""
  1024. var price = ""
  1025. var batchPrice = ""
  1026. var cnyPrice = ""
  1027. var cnyBatchPrice = ""
  1028. var cnyTotalPrice = ""
  1029. if let token = result["productId"] { productId = token as! String }
  1030. if let token = result["totalPrice"] { totalPrice = token as! String }
  1031. if let token = result["price"] { price = token as! String }
  1032. if let token = result["batchPrice"] { batchPrice = token as! String }
  1033. if let token = result["cnyPrice"] { cnyPrice = token as! String }
  1034. if let token = result["cnyBatchPrice"] { cnyBatchPrice = token as! String }
  1035. if let token = result["cnyTotalPrice"] { cnyTotalPrice = token as! String }
  1036. let products = KMBatchProductPriceModel(productId: productId, totalPrice: totalPrice, price: price, batchPrice: batchPrice, cnyPrice: cnyPrice, cnyBatchPrice: cnyBatchPrice, cnyTotalPrice: cnyTotalPrice)
  1037. let result1 = KMMemberProductResult(code: code, msg: message, batchProductPrice: products)
  1038. if code == 200 {
  1039. complete(true, result1)
  1040. } else {
  1041. complete(false, result1)
  1042. }
  1043. }
  1044. }
  1045. // MARK: 个人中心模块
  1046. // MARK: 订单模块
  1047. /**
  1048. @abstract 支付后查询订单状态
  1049. @param orderId 订单id
  1050. @param complete 回调
  1051. */
  1052. func getStateByOrderId(orderId: String, _ complete: @escaping KMMemberProductComplete) {
  1053. let token: String = KMMemberInfo.shared.access_token
  1054. if token == "" {
  1055. return
  1056. }
  1057. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getStateByOrderId"
  1058. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: nil) { requestSerializer in
  1059. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1060. } completion: { [weak self] (task, responseObject, error) in
  1061. var dic: NSDictionary = [:]
  1062. if error == nil, let responseObject = responseObject as? NSDictionary {
  1063. dic = responseObject
  1064. } else {
  1065. var info = responseObject
  1066. if let error = error {
  1067. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1068. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1069. }
  1070. }
  1071. dic = (info as? NSDictionary) ?? [:]
  1072. }
  1073. let code: Int = dic["code"] as? Int ?? 0
  1074. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1075. let message: String = dic["msg"] as? String ?? ""
  1076. var status = ""
  1077. if let token = result["status"] { status = token as! String }
  1078. let result1 = KMMemberProductResult(code: code, msg: message, result: status)
  1079. if code == 200 {
  1080. complete(true, result1)
  1081. } else {
  1082. complete(false, result1)
  1083. }
  1084. }
  1085. }
  1086. /**
  1087. @abstract 创建订单(买断、订阅)
  1088. @param productId 产品id
  1089. @param paymentMethod 支付方式 PAYPAL(0),ALIPAY(1),WXPAY(2),PADDLE(3)
  1090. @param price 价格
  1091. @param discountFlag 0原价,1优惠券,2升级,3教育优惠,4批量购买,5黑五折扣
  1092. @param couponCode 优惠卷
  1093. @param num 批量购买数量
  1094. @param complete 回调
  1095. */
  1096. func createOrder(productId: String, paymentMethod: Int, price: String, discountFlag: Int, couponCode: String, num: Int, _ complete: @escaping KMMemberProductComplete) {
  1097. let token: String = KMMemberInfo.shared.access_token
  1098. if token == "" {
  1099. return
  1100. }
  1101. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/createOrder"
  1102. let params: [String: Any] = ["productId": productId,
  1103. "paymentMethod": paymentMethod,
  1104. "price": price,
  1105. "discountFlag": discountFlag,
  1106. "couponCode": couponCode,
  1107. "num": num]
  1108. KMRequestServer.requestServer.request(urlString: urlString, method: .post, params: params) { requestSerializer in
  1109. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1110. } completion: { [weak self] (task, responseObject, error) in
  1111. var dic: NSDictionary = [:]
  1112. if error == nil, let responseObject = responseObject as? NSDictionary {
  1113. dic = responseObject
  1114. } else {
  1115. var info = responseObject
  1116. if let error = error {
  1117. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1118. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1119. }
  1120. }
  1121. dic = (info as? NSDictionary) ?? [:]
  1122. }
  1123. let code: Int = dic["code"] as? Int ?? 0
  1124. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1125. let message: String = dic["msg"] as? String ?? ""
  1126. var thirdOrderNo = ""
  1127. var orderId = ""
  1128. var payHref = ""
  1129. if code == 200 {
  1130. if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1131. if let token = result["orderId"] { orderId = token as! String }
  1132. if let token = result["payHref"] { payHref = token as! String }
  1133. let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1134. let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1135. complete(true, result1)
  1136. } else {
  1137. let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1138. complete(false, result1)
  1139. }
  1140. }
  1141. }
  1142. /**
  1143. @abstract 获取订单列表
  1144. @param orderStatus 0全部1待支付
  1145. @param complete 回调
  1146. */
  1147. func getOrderListByStatus(orderStatus: Int, _ complete: @escaping KMMemberProductComplete) {
  1148. let token: String = KMMemberInfo.shared.access_token
  1149. if token == "" {
  1150. return
  1151. }
  1152. let urlString = configuration.activityBaseURL() + "/pdf-office-website/order/getOrderListByStatus"
  1153. let params: [String: Any] = ["orderStatus": orderStatus]
  1154. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  1155. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1156. } completion: { [weak self] (task, responseObject, error) in
  1157. var dic: NSDictionary = [:]
  1158. if error == nil, let responseObject = responseObject as? NSDictionary {
  1159. dic = responseObject
  1160. } else {
  1161. var info = responseObject
  1162. if let error = error {
  1163. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1164. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1165. }
  1166. }
  1167. dic = (info as? NSDictionary) ?? [:]
  1168. }
  1169. let code: Int = dic["code"] as? Int ?? 0
  1170. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1171. let message: String = dic["msg"] as? String ?? ""
  1172. // var thirdOrderNo = ""
  1173. // var orderId = ""
  1174. // var payHref = ""
  1175. // if code == 200 {
  1176. // if let token = result["thirdOrderNo"] { thirdOrderNo = token as! String }
  1177. // if let token = result["orderId"] { orderId = token as! String }
  1178. // if let token = result["payHref"] { payHref = token as! String }
  1179. // let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
  1180. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: products)
  1181. // complete(true, result1)
  1182. // } else {
  1183. // let result1 = KMMemberProductResult(code: code, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
  1184. // complete(false, result1)
  1185. // }
  1186. }
  1187. }
  1188. // MARK: 苹果支付
  1189. /**
  1190. @abstract APPstore 权益校验
  1191. @param orderStatus 0全部1待支付
  1192. @param complete 回调
  1193. */
  1194. func appStoreEquityVerification(orderStatus: Int, _ complete: @escaping KMMemberProductComplete) {
  1195. let token: String = KMMemberInfo.shared.access_token
  1196. if token == "" {
  1197. return
  1198. }
  1199. let urlString = configuration.activityBaseURL() + "/member-system-website/applePay/appStoreEquityVerification"
  1200. let params: [String: Any] = ["userId": "",
  1201. "applePayProductId": "",
  1202. "transactionId": "",
  1203. "productCode": ""]
  1204. KMRequestServer.requestServer.request(urlString: urlString, method: .get, params: params) { requestSerializer in
  1205. requestSerializer.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
  1206. } completion: { [weak self] (task, responseObject, error) in
  1207. var dic: NSDictionary = [:]
  1208. if error == nil, let responseObject = responseObject as? NSDictionary {
  1209. dic = responseObject
  1210. } else {
  1211. var info = responseObject
  1212. if let error = error {
  1213. if let data = error.userInfo["com.alamofire.serialization.response.error.data"] as? Data {
  1214. info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? NSDictionary
  1215. }
  1216. }
  1217. dic = (info as? NSDictionary) ?? [:]
  1218. }
  1219. let code: Int = dic["code"] as? Int ?? 0
  1220. let result: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1221. let message: String = dic["msg"] as? String ?? ""
  1222. let result1 = KMMemberProductResult(code: code, msg: message, result: "")
  1223. if code == 200 {
  1224. complete(true, result1)
  1225. } else {
  1226. complete(false, result1)
  1227. }
  1228. }
  1229. }
  1230. // MARK: 评估模块
  1231. // MARK: 活动模块
  1232. // MARK: 翻译模块
  1233. // MARK: AI模块
  1234. // MARK: 会员赠送模块
  1235. // MARK: Error Code
  1236. static func typeOfMessage(type: KMMemberCenterErrorCodeType?) -> String {
  1237. var result: String = ""
  1238. switch type {
  1239. case .EXCEPTION_MSG_TOKEN_IS_INVALID:
  1240. result = "无效的token或者token已过期"
  1241. break;
  1242. case .EXCEPTION_MSG_DEVICE_NUM_MAX:
  1243. result = "您的登录设备已达上限,请退出其他设备重新登录"
  1244. break;
  1245. case .EXCEPTION_MSG_USER_NOT_LOGIN:
  1246. result = "用户没有登录,请登录后再访问"
  1247. break;
  1248. case .EMAIL_VERIFY_CODE_KEY_ERROR:
  1249. result = "email code error!"
  1250. break;
  1251. case .EMAIL_REGISTER_ERROR:
  1252. result = "Register error!"
  1253. break;
  1254. case .EXCEPTION_MSG_PASSWORD_ERROR:
  1255. result = NSLocalizedString("Password error.", tableName: "MemberCenterLocalizable", comment: "")
  1256. break;
  1257. case .EXCEPTION_MSG_CODE_ACTION_ERROR:
  1258. result = "code action error"
  1259. break;
  1260. case .EXCEPTION_MSG_CODE_SEND_ERROR:
  1261. result = "code send error"
  1262. break;
  1263. case .EXCEPTION_MAIL_CODE_SEND_ERROR:
  1264. result = "mail send error"
  1265. break;
  1266. case .VERIFY_CODE_SEND_TOO_QUICKLY:
  1267. result = "code send too quickly"
  1268. break;
  1269. case .EXCEPTION_MSG_EMAIL_EXIST:
  1270. result = "该邮箱已有账号,请前往登录"
  1271. break;
  1272. case .EXCEPTION_MSG_EMAIL_NOT_REGISTER:
  1273. 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: "")
  1274. break;
  1275. case .EXCEPTION_MSG_EMAIL_LOGOUT_ING:
  1276. result = "注销中,无法登录,若有问题可联系客服"
  1277. break;
  1278. case .EXCEPTION_MSG_EMAIL_INVALID:
  1279. result = NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: "")
  1280. break;
  1281. case .EXCEPTION_MSG_PASSWORD_NOT_INCONSISTENT:
  1282. result = "密码不一致,请重新输入"
  1283. break;
  1284. case .EXCEPTION_MSG_PASSWORD_SIZE_MIN:
  1285. result = "密码请包含6~24个字符"
  1286. break;
  1287. case .EXCEPTION_MSG_PASSWORD_SIZE_MAX:
  1288. result = "密码请包含6~24个字符"
  1289. break;
  1290. case .EXCEPTION_MSG_PLEASE_ADD_AUTH_CONFIG:
  1291. result = "please add auth config"
  1292. break;
  1293. case .EXCEPTION_MSG_USER_SUBSCRIPTION_IN_PROGRESS:
  1294. result = "您目前还在订阅期内,暂时无法注销,请订阅期结束后再点击注销账号"
  1295. break;
  1296. case .EXCEPTION_TIME_TRANSFER_ERROR:
  1297. result = "时间转换失败"
  1298. break;
  1299. case .EXCEPTION_MSG_PASSWORD_CONTAIN_EMPTY:
  1300. result = "密码不能包含空格"
  1301. break;
  1302. case .UNKNOWN:
  1303. result = "UNKNOWN"
  1304. break;
  1305. case .SUCCESS:
  1306. result = "SUCCESS"
  1307. break;
  1308. default:
  1309. result = "Network error"
  1310. }
  1311. return result
  1312. }
  1313. }