// // NYAccountManager.swift // JiaPeiManage // // Created by Ning.ge on 2023/7/28. // import ObjectMapper import SwiftyUserDefaults import Foundation import SwiftyJSON struct AccountInfo :ImmutableMappable,Codable{ var coachNum:String = "" var recordDate:String = "" var schoolId:Int = 0 var schoolName = "" var name:String = "" var sex:String = "" var idcard:String = "" var mobile:String = "" var address:String = "" var photo:Int = 0 var fingerPrint:String = "" var drilicence:String = "" var expireTime:String = "" var fstdrilicDate:String = "" var occupationNo:String = "" var occupationLevel:String = "" var dripermitted:String = "" var hireDate:String = "" var leaveDate:String = "" var createDate:String = "" var createUser:Int = 0 var createUserName:String = "" var photoPath:String = "" var updateDate:String = "" var updateUser:Int = 0 var updateUserName:String = "" var cardType:String = "" var cardId:String = "" var logout:String = "" var sync:String = "" var syncTime:String = "" var groupId:Int = 0 var lockStatus:String = "" var protocolPath:String = "" var auditStatus:String = "" var type:String = "" var isIllegal:String = "" var healthState:String = "" var origo:String = "" var educationLevel:String = "" var isAgree:Int = 0 var classType:Int = 0 var dqbh:String = "" var password:String = "" var city:String = "" var cityPower:String = "" var openid:String = "" // Implement the encode(to:) method to specify how the object should be encoded func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(idcard, forKey: .idcard) try container.encode(password, forKey: .password) try container.encode(name, forKey: .name) try container.encode(sex, forKey: .sex) try container.encode(photoPath, forKey: .photoPath) try container.encode(address, forKey: .address) try container.encode(city, forKey: .city) try container.encode(cityPower, forKey: .cityPower) try container.encode(dqbh, forKey: .dqbh) try container.encode(openid, forKey: .openid) } enum CodingKeys: String, CodingKey { case idcard case password case name case sex case photoPath case address case city case cityPower case dqbh case openid } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) idcard = try container.decode(String.self, forKey: .idcard) password = try container.decode(String.self, forKey: .password) name = try container.decode(String.self, forKey: .name) sex = try container.decode(String.self, forKey: .sex) photoPath = try container.decode(String.self, forKey: .photoPath) address = try container.decode(String.self, forKey: .address) city = try container.decode(String.self, forKey: .city) cityPower = try container.decode(String.self, forKey: .cityPower) dqbh = try container.decode(String.self, forKey: .dqbh) openid = try container.decode(String.self, forKey: .openid) } init(){} init(map: Map) throws { coachNum = try map.value("coachNum") ?? "" recordDate = try map.value("recordDate") ?? "" schoolId = try map.value("schoolId") ?? 0 schoolName = try map.value("schoolName") ?? "" name = try map.value("name") ?? "" sex = try map.value("sex") ?? "" idcard = try map.value("idcard") ?? "" mobile = try map.value("mobile") ?? "" address = try map.value("address") ?? "" photo = try map.value("photo") ?? 0 fingerPrint = try map.value("fingerPrint") ?? "" drilicence = try map.value("drilicence") ?? "" expireTime = try map.value("expireTime") ?? "" fstdrilicDate = try map.value("fstdrilicDate") ?? "" occupationNo = try map.value("occupationNo") ?? "" occupationLevel = try map.value("occupationLevel") ?? "" dripermitted = try map.value("dripermitted") ?? "" hireDate = try map.value("hireDate") ?? "" leaveDate = try map.value("leaveDate") ?? "" createDate = try map.value("createDate") ?? "" createUser = try map.value("createUser") ?? 0 createUserName = try map.value("createUserName") ?? "" photoPath = try map.value("photoPath") ?? "" updateDate = try map.value("updateDate") ?? "" updateUser = try map.value("updateUser") ?? 0 updateUserName = try map.value("updateUserName") ?? "" cardType = try map.value("cardType") ?? "" cardId = try map.value("cardId") ?? "" logout = try map.value("logout") ?? "" sync = try map.value("sync") ?? "" syncTime = try map.value("syncTime") ?? "" groupId = try map.value("groupId") ?? 0 lockStatus = try map.value("lockStatus") ?? "" protocolPath = try map.value("protocolPath") ?? "" auditStatus = try map.value("auditStatus") ?? "" type = try map.value("type") ?? "" isIllegal = try map.value("isIllegal") ?? "" healthState = try map.value("healthState") ?? "" origo = try map.value("origo") ?? "" educationLevel = try map.value("educationLevel") ?? "" isAgree = try map.value("isAgree") ?? 0 classType = try map.value("classType") ?? 0 dqbh = try map.value("dqbh") ?? "" password = try map.value("password") ?? "" openid = try map.value("openid") ?? "" } } class NYAccountManager { private var accounts: [AccountInfo] = [] private let maxAccountCount = 5 func addUserinfo(user : UserInfo) { let encoder = JSONEncoder() encoder.outputFormatting = .prettyPrinted // Optional: To get pretty-printed JSON let jsonData = try? encoder.encode(user) // Convert JSON data to a JSON string let json = try? JSON(data:jsonData!) if var accountInfo = Mapper().map(JSON: (json?.dictionaryObject)!) { accountInfo.password = user.password! accountInfo.cityPower = LocalManager.userInfo.cityPower! addAccount(user: accountInfo)//添加用户信息 } } func addAccount(user : AccountInfo) { if accounts.count>0 { for (index, userInfo) in accounts.enumerated() { if userInfo.idcard == user.idcard { return } } } if accounts.count < maxAccountCount { accounts.append(user) } else { accounts.removeFirst() accounts.append(user) } saveAccounts() } func deleteAccount(at index: Int) { guard index >= 0 && index < accounts.count else { return } accounts.remove(at: index) saveAccounts() } func getAccounts() -> [AccountInfo] { return accounts } private func saveAccounts() { // Convert the accounts array to a JSON representation and save it to UserDefaults or any other persistent storage of your choice let jsonEncoder = JSONEncoder() if let data = try? jsonEncoder.encode(accounts) { UserDefaults.standard.set(data, forKey: "savedAccounts") } } private func loadAccounts() { // Load the accounts from UserDefaults or any other persistent storage if let data = UserDefaults.standard.data(forKey: "savedAccounts") { let jsonDecoder = JSONDecoder() if let savedAccounts = try? jsonDecoder.decode([AccountInfo].self, from: data) { accounts = savedAccounts } } } // 静态属性,用于存储单例实例 static let shared = NYAccountManager() init() { loadAccounts() } }