// // UserDefaultsUtils.swift // MTP2_iOS // // Created by zhongyuan on 2018/6/10. // Copyright © 2018年 zhongyuan.All rights reserved. // import Foundation enum UserDefaultsType: String { /// 是否第一次登录 case isOpened /// 上次登录的手机号 case loginPhone /// 已检索商品信息 case reSearchGoods /// 是否显示风险提示 case isShowRisk /// 地址信息 case addressInfo /// 是否记住密码 case isSavePwd /// 保存密码 case password /// 保存密码时间戳 case passwordTime /// 商品最后更新时间 case lastUpdateTime /// 字体大小 case fontSize /// 密码有效期 case pwdDown } /// UserDefault相关工具类 class UserDefaultsUtils { // MARK: - 生命周期相关 static func isFirstOpen() -> Bool { if let isOpened = UserDefaults.standard.value(forKey: UserDefaultsType.isOpened.rawValue) as? Bool { return !isOpened } else { return true } } static func setOpened() { UserDefaults.standard.set(true, forKey: UserDefaultsType.isOpened.rawValue) } /// 获取是否记住密码 /// - Returns: Bool static func isSavePwd() -> Bool { if let isSavePwd = UserDefaults.standard.value(forKey: UserDefaultsType.isSavePwd.rawValue) as? Bool { return isSavePwd } else { return false } } /// 设置是否记住密码 /// - Parameter save: save static func setIsSavePwd(_ save: Bool) { UserDefaults.standard.set(save, forKey: UserDefaultsType.isSavePwd.rawValue) } /// 获取记录的密码 /// - Returns: Bool static func savePwd() -> String? { if let pwd = UserDefaults.standard.value(forKey: UserDefaultsType.password.rawValue) as? String { return pwd } else { return nil } } /// 设置记住密码 /// - Parameter save: save static func setSavePwd(_ pwd: String) { UserDefaults.standard.set(pwd, forKey: UserDefaultsType.password.rawValue) } /// 获取记录的密码时间戳 /// - Returns: Bool static func savePwdTime() -> Date? { if let pwd = UserDefaults.standard.value(forKey: UserDefaultsType.passwordTime.rawValue) as? Date { return pwd } else { return nil } } /// 设置记住密码时间戳 /// - Parameter save: save static func setSavePwdTime(_ time: Date) { UserDefaults.standard.set(time, forKey: UserDefaultsType.passwordTime.rawValue) } /// 获取地址信息 /// - Returns: Bool static func addressInfo() -> MoAddress? { if let jsonString = UserDefaults.standard.value(forKey: UserDefaultsType.addressInfo.rawValue) as? String, let address = MoAddress.deserialize(from: jsonString) { return address } else { return nil } } /// 存储地址信息 /// - Parameter address: address static func setAddressInfo(_ address: MoAddress) { UserDefaults.standard.set(address.toJSONString() ?? "", forKey: UserDefaultsType.addressInfo.rawValue) } /// 是否同意用户隐私协议 /// - Returns: Bool static func isPrivacy() -> Bool { if let isShowRisk = UserDefaults.standard.value(forKey: UserDefaultsType.isShowRisk.rawValue) as? Bool { return isShowRisk } else { return false } } /// 设置是否同意用户隐私协议 static func setShowIsPrivacy(_ isPrivacy: Bool) { UserDefaults.standard.set(isPrivacy, forKey: UserDefaultsType.isShowRisk.rawValue) } /// 字体大小 /// - Returns: Bool static func fontSize() -> CGFloat { if let size = UserDefaults.standard.value(forKey: UserDefaultsType.fontSize.rawValue) as? CGFloat { return size } else { return 1.0 } } /// 系统设置字体大小 static func setFontSize(_ size: CGFloat) { UserDefaults.standard.set(size, forKey: UserDefaultsType.fontSize.rawValue) } /// 获取上次登录的手机号码 /// /// - Returns: String static func getLoginPhone() -> String? { return UserDefaults.standard.value(forKey: UserDefaultsType.loginPhone.rawValue) as? String } /// 设置登录手机号信息 /// /// - Parameter phone: 手机号码 static func setLoginPhone(_ phone: String) { UserDefaults.standard.setValue(phone, forKey: UserDefaultsType.loginPhone.rawValue) UserDefaults.standard.synchronize() } /// 设置已搜索的商品数据信息 /// - Parameter result: goodsId static func setResearchGoodsInfo(_ result: String) { var results = UserDefaultsUtils.getResearchGoodsInfo() if !results.contains(result) { results.append(result) } UserDefaults.standard.setValue(results, forKey: UserDefaultsType.reSearchGoods.rawValue) UserDefaults.standard.synchronize() } /// 获取之前搜索的商品数据信息 static func getResearchGoodsInfo() -> [String] { return (UserDefaults.standard.value(forKey: UserDefaultsType.reSearchGoods.rawValue) as? [String]) ?? [] } /// 删除所有的搜索记录 static func deleteAllResearchGoodsInfo() { UserDefaults.standard.setValue([], forKey: UserDefaultsType.reSearchGoods.rawValue) UserDefaults.standard.synchronize() } /// 登录密码有效期 /// - Returns: Bool static func pwdDown() -> Int { if let hour = UserDefaults.standard.value(forKey: UserDefaultsType.pwdDown.rawValue) as? Int { return hour } else { return 1 } } /// 登录密码有效期 static func setPwdDown(_ hour: Int) { UserDefaults.standard.set(hour, forKey: UserDefaultsType.pwdDown.rawValue) } } protocol UserDefaultsSettable { static var name: String { get } associatedtype defaultKeys: RawRepresentable } extension UserDefaultsSettable where defaultKeys.RawValue == String { static func set(value: String?, forKey key: defaultKeys) { let aKey = name + key.rawValue UserDefaults.standard.set(value, forKey: aKey) } static func string(forKey key: defaultKeys) -> String? { let aKey = name + key.rawValue return UserDefaults.standard.string(forKey: aKey) } } extension UserDefaults { /// 商品更新时间戳 struct goodsUpdateTime: UserDefaultsSettable { static var name: String = "goodsUpdateTime" enum defaultKeys: String { case actual } } /// 错误码更新时间戳 struct errorcodeUTime: UserDefaultsSettable { static var name: String = "errorcodeUTime" enum defaultKeys: String { case actual } } /// 枚举常量 struct enumUTime: UserDefaultsSettable { static var name: String = "enumUTime" enum defaultKeys: String { case actual } } }