QSLBaseManager.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // QSLBaseManager.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/4.
  6. //
  7. import Foundation
  8. import UIKit
  9. import AppTrackingTransparency
  10. import AdSupport
  11. import StoreKit
  12. import AdServices
  13. class QSLBaseManager {
  14. static let shared = QSLBaseManager()
  15. var userModel = QSLUserModel()
  16. private init() {}
  17. public func initConfig() {
  18. if let model = QSLCacheManager.getModel() {
  19. self.userModel = model
  20. QSLApi.updateToken(token: self.userModel.authToken)
  21. }
  22. initializeSystem()
  23. initIDFA()
  24. initPayCheck()
  25. }
  26. }
  27. extension QSLBaseManager {
  28. // 更新用户模型
  29. func updateUser(model: QSLUserModel) {
  30. self.userModel = model
  31. self.userModel.isMine = true
  32. QSLCacheManager.cacheModel(userModel)
  33. }
  34. // 登录更新模型
  35. func loginUpdateUser(authToken: String, phone: String) {
  36. QSLApi.updateToken(token: authToken)
  37. userModel.authToken = authToken
  38. userModel.phone = phone
  39. userModel.remark = "自己"
  40. userModel.isMine = true
  41. QSLCacheManager.cacheModel(userModel)
  42. }
  43. // 保存用户id
  44. func saveUserId(id: String) {
  45. userModel.userId = id
  46. QSLCacheManager.cacheModel(userModel)
  47. }
  48. // 保存vip时间
  49. func saveVipExpiredTime(time: Int) {
  50. if time != 0 {
  51. self.userModel.vipEndTimestamp = Date(timeIntervalSince1970: TimeInterval(time / 1000))
  52. } else {
  53. self.userModel.vipEndTimestamp = nil
  54. }
  55. }
  56. // 是否登录状态
  57. func isLogin() -> Bool {
  58. if userModel.authToken != "" {
  59. return true
  60. }
  61. return false
  62. }
  63. // 是否为vip
  64. func isVip() -> Bool {
  65. if userModel.vipEndTimestamp?.compare(NSDate.now) == .orderedDescending {
  66. return true
  67. }
  68. return false
  69. }
  70. // 退出登录
  71. func logOut() {
  72. print("用户退出登录")
  73. QSLCacheManager.clearCache()
  74. QSLApi.updateToken(token: "")
  75. self.saveVipExpiredTime(time: 0)
  76. self.userModel.phone = ""
  77. self.userModel.authToken = ""
  78. self.userModel.userId = ""
  79. self.userModel.remark = "自己"
  80. self.userModel.memberModel = QSLMemberModel()
  81. // 发送通知
  82. NotificationCenter.default.post(name: QSLNotification.QSLLogout, object: nil)
  83. }
  84. }
  85. extension QSLBaseManager {
  86. // 检查漏单
  87. func initPayCheck() {
  88. if let order = QSLCacheManager.getOrderModel() {
  89. if order.receiptData.count > 0 {
  90. if !order.isFinish {
  91. QSLVipManager.shared.orderModel = order
  92. QSLVipManager.shared.openAppRePay(receiptData: order.receiptData) { status, number in
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. extension QSLBaseManager {
  100. func initializeSystem() {
  101. // 获取 APP 名称
  102. if let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
  103. QSLApi.getAppName(appName: appName)
  104. } else {
  105. print("App名称未找到")
  106. }
  107. // 获取包名
  108. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  109. QSLApi.getPackageName(packageName: bundleIdentifier)
  110. debugPrint("Bundle ID: \(bundleIdentifier)")
  111. } else {
  112. debugPrint("Unable to retrieve bundle identifier")
  113. }
  114. // 获取版本号
  115. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  116. debugPrint("App Version: \(appVersion)")
  117. QSLApi.getAppVersionName(appVersionName: appVersion)
  118. let array = appVersion.components(separatedBy: ".")
  119. var appVersionCode = ""
  120. for i in array {
  121. appVersionCode.append(i)
  122. }
  123. if appVersionCode.count < 3 {
  124. appVersionCode.append("0")
  125. }
  126. QSLApi.getAppVersionCode(appVersionCode: Int(appVersionCode) ?? 100)
  127. } else {
  128. debugPrint("Unable to retrieve app version")
  129. }
  130. if let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String {
  131. debugPrint("当前的Build号: \(buildNumber)")
  132. }
  133. // 获取系统版本
  134. let osVersion = String(format: "%@%@", UIDevice.current.systemName, UIDevice.current.systemVersion)
  135. QSLApi.getOSVersion(osVersion: osVersion)
  136. // 获取 设置uuid 并更新
  137. if let uuid = UIDevice.current.identifierForVendor?.uuidString {
  138. QSLApi.getIDFV(newIDFV: uuid)
  139. debugPrint("IDFV ID: \(uuid)")
  140. } else {
  141. debugPrint("获取 IDFV ID 参数失败")
  142. }
  143. }
  144. func initIDFA() {
  145. DispatchQueue.global().asyncAfter(deadline: .now() + 0.1){
  146. if #available(iOS 14.0, *) {
  147. // 用户请求授权获得IDFA权限
  148. ATTrackingManager.requestTrackingAuthorization { status in
  149. if status == .authorized {
  150. debugPrint("授权成功")
  151. // 用户已授权
  152. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  153. debugPrint("IDFA: \(idfa.uuidString)")
  154. QSLApi.getIDFA(newIDFA: idfa.uuidString)
  155. self.initGE()
  156. } else {
  157. debugPrint("无法获取idfa")
  158. self.initGE()
  159. }
  160. }
  161. } else {
  162. // 检查用户是否授权应用使用广告标识符
  163. if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
  164. // 获取 IDFA
  165. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  166. QSLApi.getIDFA(newIDFA: idfa.uuidString)
  167. debugPrint("IDFA: \(idfa)")
  168. self.initGE()
  169. } else {
  170. debugPrint("用户拒绝广告追踪")
  171. self.initGE()
  172. }
  173. }
  174. }
  175. }
  176. // 引力引擎初始化
  177. func initGE() {
  178. DispatchQueue.main.async {
  179. if gravityInstance == nil {
  180. let gravity = QSLGravityManager()
  181. gravity.initGE()
  182. }
  183. }
  184. }
  185. }