2022-01-04 22:28:18 +08:00
|
|
|
|
//
|
|
|
|
|
// AppState.swift
|
|
|
|
|
// PushDeer
|
|
|
|
|
//
|
|
|
|
|
// Created by HEXT on 2022/1/4.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2022-01-10 00:26:29 +08:00
|
|
|
|
import AuthenticationServices
|
|
|
|
|
|
2022-01-04 22:28:18 +08:00
|
|
|
|
class AppState: ObservableObject {
|
2022-01-10 00:26:29 +08:00
|
|
|
|
/// 账号 token
|
|
|
|
|
@Published var token : String {
|
|
|
|
|
didSet {
|
|
|
|
|
UserDefaults.standard.set(token, forKey: "PushDeer_token")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// 设备列表
|
2022-01-04 22:28:18 +08:00
|
|
|
|
@Published var devices: [DeviceItem] = []
|
2022-01-10 00:26:29 +08:00
|
|
|
|
/// key 列表
|
2022-01-04 22:28:18 +08:00
|
|
|
|
@Published var keys: [KeyItem] = []
|
2022-01-10 00:26:29 +08:00
|
|
|
|
/// 消息列表
|
2022-01-04 22:28:18 +08:00
|
|
|
|
@Published var messages: [MessageItem] = []
|
2022-01-10 00:26:29 +08:00
|
|
|
|
/// 选中的 tab 下标
|
|
|
|
|
@Published var tabSelectedIndex: Int {
|
|
|
|
|
didSet {
|
|
|
|
|
UserDefaults.standard.set(tabSelectedIndex, forKey: "PushDeer_tabSelectedIndex")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// 设备推送 token
|
|
|
|
|
@Published var deviceToken: String = ""
|
|
|
|
|
/// 用户信息
|
|
|
|
|
@Published var userInfo: UserInfoContent?
|
|
|
|
|
/// 是否显示测试发推送的 UI
|
|
|
|
|
@Published var isShowTestPush: Bool {
|
|
|
|
|
didSet {
|
|
|
|
|
UserDefaults.standard.set(isShowTestPush, forKey: "PushDeer_isShowTestPush")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isAppClip: Bool {
|
|
|
|
|
#if APPCLIP
|
|
|
|
|
return true
|
|
|
|
|
#else
|
|
|
|
|
return false
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 22:28:18 +08:00
|
|
|
|
|
|
|
|
|
static let shared = AppState()
|
2022-01-10 00:26:29 +08:00
|
|
|
|
private init() {
|
|
|
|
|
let _token = UserDefaults.standard.string(forKey: "PushDeer_token")
|
|
|
|
|
let _tabSelectedIndex = UserDefaults.standard.integer(forKey: "PushDeer_tabSelectedIndex")
|
|
|
|
|
let _isShowTestPush = UserDefaults.standard.object(forKey: "PushDeer_isShowTestPush")
|
|
|
|
|
token = _token ?? ""
|
|
|
|
|
tabSelectedIndex = _tabSelectedIndex
|
|
|
|
|
isShowTestPush = _isShowTestPush as? Bool ?? true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func appleIdLogin(_ result: Result<ASAuthorization, Error>) async throws -> TokenContent {
|
|
|
|
|
switch result {
|
|
|
|
|
case let .success(authorization):
|
|
|
|
|
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
|
|
|
|
|
// 用户唯一ID,在一个开发者账号下的APP获取到的是一样的
|
|
|
|
|
print(appleIDCredential.user) // 000791.7a323f1326dd4674bc16d32fd6339875.1424
|
|
|
|
|
// 注意:当第一次认证成功之后,将不会再返回email,fullName等信息,可以在设置->Apple ID->密码与安全性->使用您AppleID的App 中删除对应的APP。
|
|
|
|
|
print(appleIDCredential.email as Any) // easychen@qq.com
|
|
|
|
|
print(appleIDCredential.fullName as Any) // givenName: lijie familyName: chen
|
|
|
|
|
// 「JWT」格式的token,用于验证信息合法性。其值用.分割成3段, 中间一段base64后会看到包含了 用户唯一标识 和 邮箱 等字段
|
|
|
|
|
let idToken = String(data:appleIDCredential.identityToken!, encoding: .utf8)
|
|
|
|
|
print(idToken as Any)
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
// 请求接口
|
|
|
|
|
let result = try await HttpRequest.login(idToken: idToken!)
|
|
|
|
|
print(result)
|
|
|
|
|
// 登录成功
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case let .failure(error):
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
// 登录失败
|
|
|
|
|
throw NSError(domain: "登录失败", code: -1, userInfo: nil)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 22:28:18 +08:00
|
|
|
|
}
|