QSLBaseManager.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // 上报 clientId
  99. func uploadClientId(clientId: String) {
  100. QSLNetwork().request(.deviceInfoUpload(dict: ["clientId": clientId])) { response in
  101. debugPrint("上报设备信息成功")
  102. } fail: { code, error in
  103. debugPrint("上报设备信息失败")
  104. }
  105. }
  106. }
  107. extension QSLBaseManager {
  108. func initializeSystem() {
  109. // 获取 APP 名称
  110. if let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
  111. QSLApi.getAppName(appName: appName)
  112. } else {
  113. print("App名称未找到")
  114. }
  115. // 获取包名
  116. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  117. QSLApi.getPackageName(packageName: bundleIdentifier)
  118. debugPrint("Bundle ID: \(bundleIdentifier)")
  119. } else {
  120. debugPrint("Unable to retrieve bundle identifier")
  121. }
  122. // 获取版本号
  123. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  124. debugPrint("App Version: \(appVersion)")
  125. QSLApi.getAppVersionName(appVersionName: appVersion)
  126. let array = appVersion.components(separatedBy: ".")
  127. var appVersionCode = ""
  128. for i in array {
  129. appVersionCode.append(i)
  130. }
  131. if appVersionCode.count < 3 {
  132. appVersionCode.append("0")
  133. }
  134. QSLApi.getAppVersionCode(appVersionCode: Int(appVersionCode) ?? 100)
  135. } else {
  136. debugPrint("Unable to retrieve app version")
  137. }
  138. if let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String {
  139. debugPrint("当前的Build号: \(buildNumber)")
  140. }
  141. // 获取系统版本
  142. let osVersion = String(format: "%@%@", UIDevice.current.systemName, UIDevice.current.systemVersion)
  143. QSLApi.getOSVersion(osVersion: osVersion)
  144. // 获取 设置uuid 并更新
  145. if let uuid = UIDevice.current.identifierForVendor?.uuidString {
  146. QSLApi.getIDFV(newIDFV: uuid)
  147. debugPrint("IDFV ID: \(uuid)")
  148. } else {
  149. debugPrint("获取 IDFV ID 参数失败")
  150. }
  151. }
  152. func initIDFA() {
  153. DispatchQueue.global().asyncAfter(deadline: .now() + 0.1){
  154. if #available(iOS 14.0, *) {
  155. // 用户请求授权获得IDFA权限
  156. ATTrackingManager.requestTrackingAuthorization { status in
  157. if status == .authorized {
  158. debugPrint("授权成功")
  159. // 用户已授权
  160. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  161. debugPrint("IDFA: \(idfa.uuidString)")
  162. QSLApi.getIDFA(newIDFA: idfa.uuidString)
  163. self.initGE()
  164. } else {
  165. debugPrint("无法获取idfa")
  166. self.initGE()
  167. }
  168. }
  169. } else {
  170. // 检查用户是否授权应用使用广告标识符
  171. if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
  172. // 获取 IDFA
  173. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  174. QSLApi.getIDFA(newIDFA: idfa.uuidString)
  175. debugPrint("IDFA: \(idfa)")
  176. self.initGE()
  177. } else {
  178. debugPrint("用户拒绝广告追踪")
  179. self.initGE()
  180. }
  181. }
  182. }
  183. }
  184. // 引力引擎初始化
  185. func initGE() {
  186. DispatchQueue.main.async {
  187. if gravityInstance == nil {
  188. let gravity = QSLGravityManager()
  189. gravity.initGE()
  190. }
  191. }
  192. }
  193. }