mirror of
https://github.com/easychen/pushdeer.git
synced 2024-11-17 07:49:22 +08:00
d3050e154f
修改配置为pushdeer官方配置(包名,团队标识,api域名); 补充新添加的文字的英文翻译 (国际化); 设备和key是空列表时,加一个提示,让用户知道可以新增; 尝试解决列表删除项后UI偶尔没刷新的bug; 首次打开提示注册设备; 首次自动生成一个 key; 消息列表放到第一个位置; 推送测试时自动生成一个key; 后台进入前台后, 刷新本地消息列表; 键盘上方添加完成按钮, 用于收键盘; 发送推送测试后 自动收键盘; 及时清角标, 拯救强迫症; 为了保持一致, 设置页也改为可滑动; 添加SelfHosted配置, 可以直接一套代码跑出两种版本; 支持服务自建; 添加Env统一管理环境变量.
106 lines
4.0 KiB
Swift
106 lines
4.0 KiB
Swift
//
|
||
// AppState.swift
|
||
// PushDeer
|
||
//
|
||
// Created by HEXT on 2022/1/4.
|
||
//
|
||
|
||
import Foundation
|
||
import AuthenticationServices
|
||
|
||
class AppState: ObservableObject {
|
||
/// 账号 token
|
||
@Published var token : String {
|
||
didSet {
|
||
UserDefaults.standard.set(token, forKey: "PushDeer_token")
|
||
}
|
||
}
|
||
/// 设备列表
|
||
@Published var devices: [DeviceItem] = []
|
||
/// key 列表
|
||
@Published var keys: [KeyItem] = []
|
||
/// 消息列表
|
||
// @Published var messages: [MessageItem] = []
|
||
/// 选中的 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")
|
||
}
|
||
}
|
||
|
||
/// API endpoint
|
||
@Published var api_endpoint : String {
|
||
didSet {
|
||
UserDefaults.standard.set(api_endpoint, forKey: "PushDeer_api_endpoint")
|
||
}
|
||
}
|
||
|
||
var isAppClip: Bool {
|
||
#if APPCLIP
|
||
return true
|
||
#else
|
||
return false
|
||
#endif
|
||
}
|
||
|
||
|
||
static let shared = AppState()
|
||
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")
|
||
let _api_endpoint = UserDefaults.standard.string(forKey: "PushDeer_api_endpoint")
|
||
token = _token ?? ""
|
||
tabSelectedIndex = _tabSelectedIndex
|
||
isShowTestPush = _isShowTestPush as? Bool ?? true
|
||
api_endpoint = _api_endpoint ?? ""
|
||
}
|
||
|
||
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)
|
||
// 后端登录失败
|
||
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -4, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-4)\n\(error.localizedDescription)"])
|
||
}
|
||
} else {
|
||
// 非 Apple 登录凭证
|
||
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示"), code: -3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-3)"])
|
||
}
|
||
case let .failure(error):
|
||
print(error)
|
||
// Apple 登录失败
|
||
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -2, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-2)\n\(error.localizedDescription)"])
|
||
}
|
||
}
|
||
|
||
}
|