NYAccountManager.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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:String = ""
  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:Int = 0
  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. var openid:String = ""
  59. // Implement the encode(to:) method to specify how the object should be encoded
  60. func encode(to encoder: Encoder) throws {
  61. var container = encoder.container(keyedBy: CodingKeys.self)
  62. try container.encode(idcard, forKey: .idcard)
  63. try container.encode(password, forKey: .password)
  64. try container.encode(name, forKey: .name)
  65. try container.encode(sex, forKey: .sex)
  66. try container.encode(photoPath, forKey: .photoPath)
  67. try container.encode(address, forKey: .address)
  68. try container.encode(city, forKey: .city)
  69. try container.encode(cityPower, forKey: .cityPower)
  70. try container.encode(dqbh, forKey: .dqbh)
  71. try container.encode(openid, forKey: .openid)
  72. }
  73. enum CodingKeys: String, CodingKey {
  74. case idcard
  75. case password
  76. case name
  77. case sex
  78. case photoPath
  79. case address
  80. case city
  81. case cityPower
  82. case dqbh
  83. case openid
  84. }
  85. init(from decoder: Decoder) throws {
  86. let container = try decoder.container(keyedBy: CodingKeys.self)
  87. idcard = try container.decode(String.self, forKey: .idcard)
  88. password = try container.decode(String.self, forKey: .password)
  89. name = try container.decode(String.self, forKey: .name)
  90. sex = try container.decode(String.self, forKey: .sex)
  91. photoPath = try container.decode(String.self, forKey: .photoPath)
  92. address = try container.decode(String.self, forKey: .address)
  93. city = try container.decode(String.self, forKey: .city)
  94. cityPower = try container.decode(String.self, forKey: .cityPower)
  95. dqbh = try container.decode(String.self, forKey: .dqbh)
  96. openid = try container.decode(String.self, forKey: .openid)
  97. }
  98. init(){}
  99. init(map: Map) throws {
  100. coachNum = try map.value("coachNum") ?? ""
  101. recordDate = try map.value("recordDate") ?? ""
  102. schoolId = try map.value("schoolId") ?? 0
  103. schoolName = try map.value("schoolName") ?? ""
  104. name = try map.value("name") ?? ""
  105. sex = try map.value("sex") ?? ""
  106. idcard = try map.value("idcard") ?? ""
  107. mobile = try map.value("mobile") ?? ""
  108. address = try map.value("address") ?? ""
  109. photo = try map.value("photo") ?? 0
  110. fingerPrint = try map.value("fingerPrint") ?? ""
  111. drilicence = try map.value("drilicence") ?? ""
  112. expireTime = try map.value("expireTime") ?? ""
  113. fstdrilicDate = try map.value("fstdrilicDate") ?? ""
  114. occupationNo = try map.value("occupationNo") ?? ""
  115. occupationLevel = try map.value("occupationLevel") ?? ""
  116. dripermitted = try map.value("dripermitted") ?? ""
  117. hireDate = try map.value("hireDate") ?? ""
  118. leaveDate = try map.value("leaveDate") ?? ""
  119. createDate = try map.value("createDate") ?? ""
  120. createUser = try map.value("createUser") ?? 0
  121. createUserName = try map.value("createUserName") ?? ""
  122. photoPath = try map.value("photoPath") ?? ""
  123. updateDate = try map.value("updateDate") ?? ""
  124. updateUser = try map.value("updateUser") ?? 0
  125. updateUserName = try map.value("updateUserName") ?? ""
  126. cardType = try map.value("cardType") ?? ""
  127. cardId = try map.value("cardId") ?? ""
  128. logout = try map.value("logout") ?? ""
  129. sync = try map.value("sync") ?? ""
  130. syncTime = try map.value("syncTime") ?? ""
  131. groupId = try map.value("groupId") ?? 0
  132. lockStatus = try map.value("lockStatus") ?? ""
  133. protocolPath = try map.value("protocolPath") ?? ""
  134. auditStatus = try map.value("auditStatus") ?? ""
  135. type = try map.value("type") ?? ""
  136. isIllegal = try map.value("isIllegal") ?? ""
  137. healthState = try map.value("healthState") ?? ""
  138. origo = try map.value("origo") ?? ""
  139. educationLevel = try map.value("educationLevel") ?? ""
  140. isAgree = try map.value("isAgree") ?? 0
  141. classType = try map.value("classType") ?? 0
  142. dqbh = try map.value("dqbh") ?? ""
  143. password = try map.value("password") ?? ""
  144. openid = try map.value("openid") ?? ""
  145. }
  146. }
  147. class NYAccountManager {
  148. private var accounts: [AccountInfo] = []
  149. private let maxAccountCount = 5
  150. func addUserinfo(user : UserInfo) {
  151. let encoder = JSONEncoder()
  152. encoder.outputFormatting = .prettyPrinted // Optional: To get pretty-printed JSON
  153. let jsonData = try? encoder.encode(user)
  154. // Convert JSON data to a JSON string
  155. let json = try? JSON(data:jsonData!)
  156. if var accountInfo = Mapper<AccountInfo>().map(JSON: (json?.dictionaryObject)!) {
  157. accountInfo.password = user.password!
  158. accountInfo.cityPower = LocalManager.userInfo.cityPower!
  159. addAccount(user: accountInfo)//添加用户信息
  160. }
  161. }
  162. func addAccount(user : AccountInfo) {
  163. if accounts.count>0 {
  164. for (index, userInfo) in accounts.enumerated() {
  165. if userInfo.idcard == user.idcard {
  166. return
  167. }
  168. }
  169. }
  170. if accounts.count < maxAccountCount {
  171. accounts.append(user)
  172. } else {
  173. accounts.removeFirst()
  174. accounts.append(user)
  175. }
  176. saveAccounts()
  177. }
  178. func deleteAccount(at index: Int) {
  179. guard index >= 0 && index < accounts.count else {
  180. return
  181. }
  182. accounts.remove(at: index)
  183. saveAccounts()
  184. }
  185. func getAccounts() -> [AccountInfo] {
  186. return accounts
  187. }
  188. private func saveAccounts() {
  189. // Convert the accounts array to a JSON representation and save it to UserDefaults or any other persistent storage of your choice
  190. let jsonEncoder = JSONEncoder()
  191. if let data = try? jsonEncoder.encode(accounts) {
  192. UserDefaults.standard.set(data, forKey: "savedAccounts")
  193. }
  194. }
  195. private func loadAccounts() {
  196. // Load the accounts from UserDefaults or any other persistent storage
  197. if let data = UserDefaults.standard.data(forKey: "savedAccounts") {
  198. let jsonDecoder = JSONDecoder()
  199. if let savedAccounts = try? jsonDecoder.decode([AccountInfo].self, from: data) {
  200. accounts = savedAccounts
  201. }
  202. }
  203. }
  204. // 静态属性,用于存储单例实例
  205. static let shared = NYAccountManager()
  206. init() {
  207. loadAccounts()
  208. }
  209. }