| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // 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 noticeRead
- case reSearchGoods
- case addressInfo
- /// 是否记住密码
- case isSavePwd
- /// 保存密码
- case password
- /// 保存密码时间戳
- case passwordTime
- }
- /// 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)
- }
-
- /// 设置通知未读标志
- ///
- /// - Parameter ids: ids
- static func setUnReasNoticeIds(_ ids: [String]) {
- UserDefaults.standard.setValue(ids, forKey: UserDefaultsType.noticeRead.rawValue)
- UserDefaults.standard.synchronize()
- }
-
- /// 获取未读标志
- ///
- /// - Returns: [String]
- static func getUnReadNoticeIds() -> [String] {
- let ids = UserDefaults.standard.value(forKey: UserDefaultsType.noticeRead.rawValue) ?? []
- return ids as! [String]
- }
-
- /// 获取上次登录的手机号码
- ///
- /// - 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()
- }
- }
- 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
- }
- }
- }
|