Account.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // account.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/6/28.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import ObjectMapper
  10. class Account:NSObject,NSCoding,Mappable{
  11. var id:String?
  12. var name:String?
  13. var passwd:String?
  14. var roleList:[String]?
  15. var token:String?
  16. var authentication:Bool?
  17. var autoLogin:Bool?
  18. static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
  19. static let ArchiveURL = DocumentsDirectory.appendingPathComponent("Account")
  20. struct AccountKey {
  21. static let idKey = "id"
  22. static let nameKey = "name"
  23. static let passwdKey = "passwd"
  24. static let roleListKey = "roleList"
  25. static let tokenKey = "token"
  26. static let authenticationKey = "authentication"
  27. static let autoLoginKey = "autoLogin"
  28. }
  29. required init?(map: Map) {
  30. }
  31. func mapping(map: Map) {
  32. id <- map["id"]
  33. name <- map["name"]
  34. passwd <- map["passwd"]
  35. roleList <- map["roleList"]
  36. token <- map["token"]
  37. }
  38. required init(id:String?,name:String?,passwd:String?,roleList:[String]?,token:String?,authentication:Bool?,autoLogin:Bool?){
  39. self.id = id
  40. self.name = name
  41. self.passwd = passwd
  42. self.roleList = roleList
  43. self.token = token
  44. self.authentication = authentication
  45. self.autoLogin = autoLogin
  46. }
  47. required convenience init?(coder aDecoder: NSCoder) {
  48. let id = aDecoder.decodeObject(forKey: AccountKey.idKey) as? String
  49. let name = aDecoder.decodeObject(forKey: AccountKey.nameKey) as? String
  50. let passwd = aDecoder.decodeObject(forKey: AccountKey.passwdKey) as? String
  51. let roleList = aDecoder.decodeObject(forKey: AccountKey.roleListKey) as? [String]
  52. let token = aDecoder.decodeObject(forKey: AccountKey.tokenKey) as? String
  53. let authentication = aDecoder.decodeBool(forKey: AccountKey.authenticationKey)
  54. let autoLogin = aDecoder.decodeBool(forKey: AccountKey.autoLoginKey)
  55. self.init(id:id,name:name,passwd:passwd,roleList:roleList,token:token,authentication:authentication,autoLogin:autoLogin)
  56. }
  57. func encode(with aCoder: NSCoder) {
  58. aCoder.encode(id,forKey: AccountKey.idKey)
  59. aCoder.encode(name,forKey: AccountKey.nameKey)
  60. aCoder.encode(passwd,forKey: AccountKey.passwdKey)
  61. aCoder.encode(roleList,forKey: AccountKey.roleListKey)
  62. aCoder.encode(token,forKey: AccountKey.tokenKey)
  63. aCoder.encode(authentication!,forKey: AccountKey.authenticationKey)
  64. aCoder.encode(autoLogin!,forKey: AccountKey.autoLoginKey)
  65. }
  66. static func saveAccount(_ account:Account){
  67. let saveData = NSKeyedArchiver.archiveRootObject(account, toFile:Account.ArchiveURL.path)
  68. if !saveData {
  69. print("save Account Fail")
  70. }
  71. }
  72. static func currentAccount() -> Account? {
  73. return NSKeyedUnarchiver.unarchiveObject(withFile: Account.ArchiveURL.path) as? Account
  74. }
  75. }