UserModel.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // UserModel.swift
  3. // Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport
  4. import Foundation
  5. import ObjectMapper
  6. class UserModel : NSObject, NSCoding, Mappable{
  7. private static let _sharedInstance = UserModel()
  8. private override init() {} // 私有化init方法
  9. class func shared() -> UserModel {
  10. return _sharedInstance
  11. }
  12. var accessToken : String?
  13. var avatarurl : String?
  14. var gender : Int?
  15. var isPassword : Int?
  16. var mobile : String?
  17. var openId : String?
  18. var token : String?
  19. var tokenTtl : Int?
  20. var uid : Int?
  21. var unionId : String?
  22. var username : String?
  23. var isFollowSuggestTopic : Int?
  24. var isNewMember : Int?
  25. class func newInstance(map: Map) -> Mappable?{
  26. return UserModel()
  27. }
  28. required init?(map: Map){}
  29. func mapping(map: Map)
  30. {
  31. accessToken <- map["access_token"]
  32. avatarurl <- map["avatarurl"]
  33. gender <- map["gender"]
  34. isPassword <- map["is_password"]
  35. mobile <- map["mobile"]
  36. openId <- map["open_id"]
  37. token <- map["token"]
  38. tokenTtl <- map["token_ttl"]
  39. uid <- map["uid"]
  40. unionId <- map["union_id"]
  41. isFollowSuggestTopic <- map["is_follow_suggest_topic"]
  42. isNewMember <- map["is_new_member"]
  43. username <- map["username"]
  44. }
  45. /**
  46. * NSCoding required initializer.
  47. * Fills the data from the passed decoder
  48. */
  49. @objc required init(coder aDecoder: NSCoder)
  50. {
  51. accessToken = aDecoder.decodeObject(forKey: "access_token") as? String
  52. avatarurl = aDecoder.decodeObject(forKey: "avatarurl") as? String
  53. gender = aDecoder.decodeObject(forKey: "gender") as? Int
  54. isPassword = aDecoder.decodeObject(forKey: "is_password") as? Int
  55. mobile = aDecoder.decodeObject(forKey: "mobile") as? String
  56. openId = aDecoder.decodeObject(forKey: "open_id") as? String
  57. token = aDecoder.decodeObject(forKey: "token") as? String
  58. tokenTtl = aDecoder.decodeObject(forKey: "token_ttl") as? Int
  59. uid = aDecoder.decodeObject(forKey: "uid") as? Int
  60. unionId = aDecoder.decodeObject(forKey: "union_id") as? String
  61. username = aDecoder.decodeObject(forKey: "username") as? String
  62. isFollowSuggestTopic = aDecoder.decodeObject(forKey: "is_follow_suggest_topic") as? Int
  63. isNewMember = aDecoder.decodeObject(forKey: "is_new_member") as? Int
  64. }
  65. /**
  66. * NSCoding required method.
  67. * Encodes mode properties into the decoder
  68. */
  69. @objc func encode(with aCoder: NSCoder)
  70. {
  71. if accessToken != nil{
  72. aCoder.encode(accessToken, forKey: "access_token")
  73. }
  74. if avatarurl != nil{
  75. aCoder.encode(avatarurl, forKey: "avatarurl")
  76. }
  77. if gender != nil{
  78. aCoder.encode(gender, forKey: "gender")
  79. }
  80. if isPassword != nil{
  81. aCoder.encode(isPassword, forKey: "is_password")
  82. }
  83. if mobile != nil{
  84. aCoder.encode(mobile, forKey: "mobile")
  85. }
  86. if openId != nil{
  87. aCoder.encode(openId, forKey: "open_id")
  88. }
  89. if token != nil{
  90. aCoder.encode(token, forKey: "token")
  91. }
  92. if tokenTtl != nil{
  93. aCoder.encode(tokenTtl, forKey: "token_ttl")
  94. }
  95. if uid != nil{
  96. aCoder.encode(uid, forKey: "uid")
  97. }
  98. if unionId != nil{
  99. aCoder.encode(unionId, forKey: "union_id")
  100. }
  101. if username != nil{
  102. aCoder.encode(username, forKey: "username")
  103. }
  104. if isFollowSuggestTopic != nil{
  105. aCoder.encode(isFollowSuggestTopic, forKey: "is_follow_suggest_topic")
  106. }
  107. if isNewMember != nil{
  108. aCoder.encode(isNewMember, forKey: "is_new_member")
  109. }
  110. }
  111. /// 存储个人信息
  112. ///
  113. /// - Parameter locationModel: 定位模型
  114. func setModel(model:UserModel) -> Void {
  115. //实例对象转换成Data
  116. let modelData = NSKeyedArchiver.archivedData(withRootObject: model)
  117. //存储Data对象
  118. UserDefaults.standard.set(modelData, forKey: "UserModel")
  119. NXLLog("UserModel存储成功")
  120. }
  121. /// 获取个人信息
  122. ///
  123. /// - Returns: 定位模型
  124. func getModel() -> UserModel? {
  125. //自定义对象读取
  126. let modelData = UserDefaults.standard.data(forKey: "UserModel")
  127. var model = UserModel()
  128. if modelData != nil {
  129. model = NSKeyedUnarchiver.unarchiveObject(with: modelData!) as! UserModel
  130. NXLLog("UserModel读取成功")
  131. }
  132. return model
  133. }
  134. /// 移除个人信息
  135. func removeLoginModel() {
  136. UserDefaults.standard.removeObject(forKey: "UserModel")
  137. }
  138. /// 获取H5需要的对象
  139. func getH5UserInfo() -> Dictionary<String,Any> {
  140. //获取当前时间
  141. let userModel = getModel()
  142. var userInfoDic = Dictionary<String,Any>()
  143. userInfoDic.updateValue("userInfo", forKey: "type")
  144. userInfoDic.updateValue(userModel?.toJSON() as Any, forKey: "params")
  145. return userInfoDic
  146. }
  147. }