NYAccountManager.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // NYAccountManager.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/7/28.
  6. //
  7. import ObjectMapper
  8. import SwiftyUserDefaults
  9. import Foundation
  10. import SwiftyJSON
  11. struct AccountInfo :ImmutableMappable,Codable{
  12. var coachNum:String = ""
  13. var recordDate:String = ""
  14. var schoolId:Int = 0
  15. var schoolName = ""
  16. var name:String = ""
  17. var sex:String = ""
  18. var idcard:String = ""
  19. var mobile:String = ""
  20. var address:String = ""
  21. var photo:Int = 0
  22. var fingerPrint:String = ""
  23. var drilicence:Int = 0
  24. var expireTime:String = ""
  25. var fstdrilicDate:String = ""
  26. var occupationNo:String = ""
  27. var occupationLevel:String = ""
  28. var dripermitted:String = ""
  29. var hireDate:String = ""
  30. var leaveDate:String = ""
  31. var createDate:String = ""
  32. var createUser:Int = 0
  33. var createUserName:String = ""
  34. var photoPath:String = ""
  35. var updateDate:String = ""
  36. var updateUser:Int = 0
  37. var updateUserName:String = ""
  38. var cardType:String = ""
  39. var cardId:String = ""
  40. var logout:String = ""
  41. var sync:String = ""
  42. var syncTime:String = ""
  43. var groupId:String = ""
  44. var lockStatus:String = ""
  45. var protocolPath:String = ""
  46. var auditStatus:String = ""
  47. var type:String = ""
  48. var isIllegal:String = ""
  49. var healthState:String = ""
  50. var origo:String = ""
  51. var educationLevel:String = ""
  52. var isAgree:Int = 0
  53. var classType:Int = 0
  54. var dqbh:String = ""
  55. var password:String = ""
  56. var city:String = ""
  57. var cityPower:String = ""
  58. // Implement the encode(to:) method to specify how the object should be encoded
  59. func encode(to encoder: Encoder) throws {
  60. var container = encoder.container(keyedBy: CodingKeys.self)
  61. try container.encode(idcard, forKey: .idcard)
  62. try container.encode(password, forKey: .password)
  63. try container.encode(name, forKey: .name)
  64. try container.encode(sex, forKey: .sex)
  65. try container.encode(photoPath, forKey: .photoPath)
  66. try container.encode(address, forKey: .address)
  67. try container.encode(city, forKey: .city)
  68. try container.encode(cityPower, forKey: .cityPower)
  69. try container.encode(dqbh, forKey: .dqbh)
  70. }
  71. enum CodingKeys: String, CodingKey {
  72. case idcard
  73. case password
  74. case name
  75. case sex
  76. case photoPath
  77. case address
  78. case city
  79. case cityPower
  80. case dqbh
  81. }
  82. init(from decoder: Decoder) throws {
  83. let container = try decoder.container(keyedBy: CodingKeys.self)
  84. idcard = try container.decode(String.self, forKey: .idcard)
  85. password = try container.decode(String.self, forKey: .password)
  86. name = try container.decode(String.self, forKey: .name)
  87. sex = try container.decode(String.self, forKey: .sex)
  88. photoPath = try container.decode(String.self, forKey: .photoPath)
  89. address = try container.decode(String.self, forKey: .address)
  90. city = try container.decode(String.self, forKey: .city)
  91. cityPower = try container.decode(String.self, forKey: .cityPower)
  92. dqbh = try container.decode(String.self, forKey: .dqbh)
  93. }
  94. init(){}
  95. init(map: Map) throws {
  96. coachNum = try map.value("coachNum") ?? ""
  97. recordDate = try map.value("recordDate") ?? ""
  98. schoolId = try map.value("schoolId") ?? 0
  99. schoolName = try map.value("schoolName") ?? ""
  100. name = try map.value("name") ?? ""
  101. sex = try map.value("sex") ?? ""
  102. idcard = try map.value("idcard") ?? ""
  103. mobile = try map.value("mobile") ?? ""
  104. address = try map.value("address") ?? ""
  105. photo = try map.value("photo") ?? 0
  106. fingerPrint = try map.value("fingerPrint") ?? ""
  107. drilicence = try map.value("drilicence") ?? 0
  108. expireTime = try map.value("expireTime") ?? ""
  109. fstdrilicDate = try map.value("fstdrilicDate") ?? ""
  110. occupationNo = try map.value("occupationNo") ?? ""
  111. occupationLevel = try map.value("occupationLevel") ?? ""
  112. dripermitted = try map.value("dripermitted") ?? ""
  113. hireDate = try map.value("hireDate") ?? ""
  114. leaveDate = try map.value("leaveDate") ?? ""
  115. createDate = try map.value("createDate") ?? ""
  116. createUser = try map.value("createUser") ?? 0
  117. createUserName = try map.value("createUserName") ?? ""
  118. photoPath = try map.value("photoPath") ?? ""
  119. updateDate = try map.value("updateDate") ?? ""
  120. updateUser = try map.value("updateUser") ?? 0
  121. updateUserName = try map.value("updateUserName") ?? ""
  122. cardType = try map.value("cardType") ?? ""
  123. cardId = try map.value("cardId") ?? ""
  124. logout = try map.value("logout") ?? ""
  125. sync = try map.value("sync") ?? ""
  126. syncTime = try map.value("syncTime") ?? ""
  127. groupId = try map.value("groupId") ?? ""
  128. lockStatus = try map.value("lockStatus") ?? ""
  129. protocolPath = try map.value("protocolPath") ?? ""
  130. auditStatus = try map.value("auditStatus") ?? ""
  131. type = try map.value("type") ?? ""
  132. isIllegal = try map.value("isIllegal") ?? ""
  133. healthState = try map.value("healthState") ?? ""
  134. origo = try map.value("origo") ?? ""
  135. educationLevel = try map.value("educationLevel") ?? ""
  136. isAgree = try map.value("isAgree") ?? 0
  137. classType = try map.value("classType") ?? 0
  138. dqbh = try map.value("dqbh") ?? ""
  139. password = try map.value("password") ?? ""
  140. }
  141. }
  142. class NYAccountManager {
  143. private var accounts: [AccountInfo] = []
  144. private let maxAccountCount = 5
  145. func addUserinfo(user : UserInfo) {
  146. let encoder = JSONEncoder()
  147. encoder.outputFormatting = .prettyPrinted // Optional: To get pretty-printed JSON
  148. let jsonData = try? encoder.encode(user)
  149. // Convert JSON data to a JSON string
  150. let json = try? JSON(data:jsonData!)
  151. if var accountInfo = Mapper<AccountInfo>().map(JSON: (json?.dictionaryObject)!) {
  152. accountInfo.password = user.password!
  153. accountInfo.cityPower = LocalManager.userInfo.cityPower!
  154. addAccount(user: accountInfo)//添加用户信息
  155. }
  156. }
  157. func addAccount(user : AccountInfo) {
  158. if accounts.count>0 {
  159. for (index, userInfo) in accounts.enumerated() {
  160. if userInfo.idcard == user.idcard {
  161. return
  162. }
  163. }
  164. }
  165. if accounts.count < maxAccountCount {
  166. accounts.append(user)
  167. } else {
  168. accounts.removeFirst()
  169. accounts.append(user)
  170. }
  171. saveAccounts()
  172. }
  173. func deleteAccount(at index: Int) {
  174. guard index >= 0 && index < accounts.count else {
  175. return
  176. }
  177. accounts.remove(at: index)
  178. saveAccounts()
  179. }
  180. func getAccounts() -> [AccountInfo] {
  181. return accounts
  182. }
  183. private func saveAccounts() {
  184. // Convert the accounts array to a JSON representation and save it to UserDefaults or any other persistent storage of your choice
  185. let jsonEncoder = JSONEncoder()
  186. if let data = try? jsonEncoder.encode(accounts) {
  187. UserDefaults.standard.set(data, forKey: "savedAccounts")
  188. }
  189. }
  190. private func loadAccounts() {
  191. // Load the accounts from UserDefaults or any other persistent storage
  192. if let data = UserDefaults.standard.data(forKey: "savedAccounts") {
  193. let jsonDecoder = JSONDecoder()
  194. if let savedAccounts = try? jsonDecoder.decode([AccountInfo].self, from: data) {
  195. accounts = savedAccounts
  196. }
  197. }
  198. }
  199. // 静态属性,用于存储单例实例
  200. static let shared = NYAccountManager()
  201. init() {
  202. loadAccounts()
  203. }
  204. }