KMLightMemberToken.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // KMLightMemberToken.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/27.
  6. //
  7. import Cocoa
  8. class KMLightMemberToken: NSObject, Codable{
  9. var access_token: String = ""
  10. var expires_in: String = ""
  11. var refresh_token: String = ""
  12. var token_type: String = ""
  13. // var scope: String = ""
  14. }
  15. extension KMLightMemberToken {
  16. static public func parseData(data: NSDictionary, completion:(_ data: KMLightMemberToken) -> Void) -> Void {
  17. if (data.count != 0) {
  18. KMLightMemberCache.cache.saveData(data: data, type: .token)
  19. }
  20. var result: KMLightMemberToken = KMLightMemberToken()
  21. let jsonString: String = DictionaryToJson.dictionary(toJson: data)!
  22. let jsonData: Data = jsonString.data(using: .utf8)!
  23. let decoder = JSONDecoder()
  24. // decoder.dataDecodingStrategy = .base64
  25. // decoder.keyDecodingStrategy = .convertFromSnakeCase //带下划线命名
  26. decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: "+∞", negativeInfinity: "-∞", nan: "NaN")
  27. #if DEBUG
  28. //MARK: 测试使用
  29. var lightMemberToken = try! decoder.decode(KMLightMemberToken.self, from: jsonData)
  30. if (lightMemberToken != nil) {
  31. result = lightMemberToken
  32. }
  33. #else
  34. // MARK: 此处try 使用 ? ,如果数据出问题 advertisementModel将无参数, 测试时使用!
  35. var lightMemberToken = try? decoder.decode(KMLightMemberToken.self, from: jsonData)
  36. if (lightMemberToken != nil) {
  37. result = lightMemberToken!
  38. }
  39. #endif
  40. completion(result)
  41. }
  42. }
  43. public class DictionaryToJson: NSObject {
  44. public static func dictionary(toJson dic: AnyObject?) -> String? {
  45. var jsonData: Data? = nil
  46. do {
  47. if let dic = dic {
  48. jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
  49. }
  50. } catch let parseError {
  51. print("(parseError.localizedDescription)")
  52. }
  53. if let jsonData = jsonData {
  54. return String(data: jsonData, encoding: .utf8)
  55. }
  56. return nil
  57. }
  58. }