实现我的 Tab、对齐 Android 流程并接入 OSS 上传
This commit is contained in:
@ -6,7 +6,6 @@
|
||||
import Foundation
|
||||
|
||||
/// 本地会话存储,与 Android `AppStoreDataSource` 对齐。
|
||||
/// 当前以 Token 是否非空判断登录态。
|
||||
final class AppStore {
|
||||
|
||||
static let shared = AppStore()
|
||||
@ -15,6 +14,18 @@ final class AppStore {
|
||||
static let token = "key_in_token"
|
||||
static let lastLoginUsername = "key_last_login_username"
|
||||
static let privacyAgreementAccepted = "key_privacy_agreement_accepted"
|
||||
static let userId = "key_in_user_id"
|
||||
static let userName = "key_in_user_name"
|
||||
static let realName = "key_in_real_name"
|
||||
static let avatar = "key_in_avatar"
|
||||
static let phone = "key_in_phone"
|
||||
static let accountType = "key_in_account_type"
|
||||
static let accountDisplayName = "key_in_account_display_name"
|
||||
static let roleCode = "key_in_role_code"
|
||||
static let roleName = "key_in_role_name"
|
||||
static let currentScenicId = "key_in_current_scenic_id"
|
||||
static let currentScenicName = "key_in_current_scenic_name"
|
||||
static let currentStoreId = "key_in_current_store_id"
|
||||
}
|
||||
|
||||
private let defaults: UserDefaults
|
||||
@ -47,6 +58,85 @@ final class AppStore {
|
||||
set { defaults.set(newValue, forKey: Key.privacyAgreementAccepted) }
|
||||
}
|
||||
|
||||
var userId: String {
|
||||
get { defaults.string(forKey: Key.userId) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.userId) }
|
||||
}
|
||||
|
||||
var userName: String {
|
||||
get { defaults.string(forKey: Key.userName) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.userName) }
|
||||
}
|
||||
|
||||
var realName: String {
|
||||
get { defaults.string(forKey: Key.realName) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.realName) }
|
||||
}
|
||||
|
||||
var avatar: String {
|
||||
get { defaults.string(forKey: Key.avatar) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.avatar) }
|
||||
}
|
||||
|
||||
var phone: String {
|
||||
get { defaults.string(forKey: Key.phone) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.phone) }
|
||||
}
|
||||
|
||||
var accountType: String {
|
||||
get { defaults.string(forKey: Key.accountType) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.accountType) }
|
||||
}
|
||||
|
||||
var accountDisplayName: String {
|
||||
get { defaults.string(forKey: Key.accountDisplayName) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.accountDisplayName) }
|
||||
}
|
||||
|
||||
var roleCode: String {
|
||||
get { defaults.string(forKey: Key.roleCode) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.roleCode) }
|
||||
}
|
||||
|
||||
var roleName: String {
|
||||
get { defaults.string(forKey: Key.roleName) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.roleName) }
|
||||
}
|
||||
|
||||
var currentScenicId: Int {
|
||||
get {
|
||||
let value = defaults.string(forKey: Key.currentScenicId) ?? ""
|
||||
return Int(value) ?? 0
|
||||
}
|
||||
set {
|
||||
defaults.set(newValue > 0 ? String(newValue) : "", forKey: Key.currentScenicId)
|
||||
}
|
||||
}
|
||||
|
||||
var currentScenicName: String {
|
||||
get { defaults.string(forKey: Key.currentScenicName) ?? "" }
|
||||
set { defaults.set(newValue, forKey: Key.currentScenicName) }
|
||||
}
|
||||
|
||||
var currentStoreId: Int {
|
||||
get { defaults.integer(forKey: Key.currentStoreId) }
|
||||
set { defaults.set(newValue, forKey: Key.currentStoreId) }
|
||||
}
|
||||
|
||||
/// 当前解析后的业务角色。
|
||||
var currentAppRole: AppRoleCode? {
|
||||
AppRoleCode.fromCode(roleCode) ?? AppRoleCode.fromRoleName(roleName)
|
||||
}
|
||||
|
||||
/// 当前是否为摄影师角色。
|
||||
var isPhotographerRole: Bool {
|
||||
if let role = currentAppRole {
|
||||
return role.isPhotographer
|
||||
}
|
||||
let name = roleName.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return name == "摄影师"
|
||||
}
|
||||
|
||||
/// Token 非空视为已登录。
|
||||
var isLoggedIn: Bool {
|
||||
!token.isEmpty
|
||||
@ -57,8 +147,54 @@ final class AppStore {
|
||||
self.token = token
|
||||
}
|
||||
|
||||
/// 清除登录态。
|
||||
/// 从账号切换实体写入会话快照。
|
||||
func applyAccount(_ account: AccountSwitchAccount) {
|
||||
userId = String(account.businessUserId)
|
||||
accountType = account.accountType
|
||||
accountDisplayName = account.title
|
||||
userName = account.subtitle
|
||||
phone = account.phone
|
||||
if !account.avatar.isEmpty {
|
||||
avatar = account.avatar
|
||||
}
|
||||
currentScenicName = account.scenicName
|
||||
currentScenicId = account.scenicId ?? 0
|
||||
currentStoreId = account.storeId ?? 0
|
||||
}
|
||||
|
||||
/// 从用户资料接口回写展示字段。
|
||||
func applyUserInfo(_ info: UserInfoResponse) {
|
||||
if !info.nickname.isEmpty {
|
||||
userName = info.nickname
|
||||
}
|
||||
if !info.realName.isEmpty {
|
||||
realName = info.realName
|
||||
}
|
||||
if !info.avatar.isEmpty {
|
||||
avatar = info.avatar
|
||||
}
|
||||
if !info.phone.isEmpty {
|
||||
phone = info.phone
|
||||
}
|
||||
if !info.roleName.isEmpty, roleName.isEmpty {
|
||||
roleName = info.roleName
|
||||
}
|
||||
}
|
||||
|
||||
/// 清除登录态与会话快照。
|
||||
func logout() {
|
||||
token = ""
|
||||
userId = ""
|
||||
userName = ""
|
||||
realName = ""
|
||||
avatar = ""
|
||||
phone = ""
|
||||
accountType = ""
|
||||
accountDisplayName = ""
|
||||
roleCode = ""
|
||||
roleName = ""
|
||||
currentScenicId = 0
|
||||
currentScenicName = ""
|
||||
currentStoreId = 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user