QSLBaseManager.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. var isLoadG = false
  17. private init() {}
  18. public func initConfig() {
  19. if let model = QSLCacheManager.getModel() {
  20. self.userModel = model
  21. QSLApi.updateToken(token: self.userModel.authToken)
  22. }
  23. initializeSystem()
  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. NotificationCenter.default.post(
  53. name: Notification.Name("QSLHomeUpdateCouponViewNoti"),
  54. object: nil,
  55. userInfo: ["showCoupon": false]
  56. )
  57. } else {
  58. self.userModel.vipEndTimestamp = nil
  59. }
  60. }
  61. // 是否登录状态
  62. func isLogin() -> Bool {
  63. if userModel.authToken != "" {
  64. return true
  65. }
  66. return false
  67. }
  68. // 是否为vip
  69. func isVip() -> Bool {
  70. if userModel.vipEndTimestamp?.compare(NSDate.now) == .orderedDescending {
  71. return true
  72. }
  73. return false
  74. }
  75. // 退出登录
  76. func logOut() {
  77. print("用户退出登录")
  78. QSLCacheManager.clearCache()
  79. QSLApi.updateToken(token: "")
  80. self.saveVipExpiredTime(time: 0)
  81. self.userModel.phone = ""
  82. self.userModel.authToken = ""
  83. self.userModel.userId = ""
  84. self.userModel.remark = "自己"
  85. self.userModel.memberModel = QSLMemberModel()
  86. // 发送通知
  87. NotificationCenter.default.post(name: QSLNotification.QSLLogout, object: nil)
  88. }
  89. }
  90. extension QSLBaseManager {
  91. // 检查漏单
  92. func initPayCheck() {
  93. if let order = QSLCacheManager.getOrderModel() {
  94. if order.receiptData.count > 0 {
  95. if !order.isFinish {
  96. QSLVipManager.shared.orderModel = order
  97. QSLVipManager.shared.openAppRePay(receiptData: order.receiptData) { status, number in
  98. }
  99. }
  100. }
  101. }
  102. }
  103. // 上报 clientId
  104. func uploadClientId(clientId: String) {
  105. QSLNetwork().request(.deviceInfoUpload(dict: ["clientId": clientId])) { response in
  106. debugPrint("上报设备信息成功")
  107. } fail: { code, error in
  108. debugPrint("上报设备信息失败")
  109. }
  110. }
  111. }
  112. extension QSLBaseManager {
  113. func initializeSystem() {
  114. // 获取 APP 名称
  115. if let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
  116. QSLApi.getAppName(appName: appName)
  117. } else {
  118. print("App名称未找到")
  119. }
  120. // 获取包名
  121. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  122. QSLApi.getPackageName(packageName: bundleIdentifier)
  123. debugPrint("Bundle ID: \(bundleIdentifier)")
  124. } else {
  125. debugPrint("Unable to retrieve bundle identifier")
  126. }
  127. // 获取版本号
  128. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  129. debugPrint("App Version: \(appVersion)")
  130. QSLApi.getAppVersionName(appVersionName: appVersion)
  131. let array = appVersion.components(separatedBy: ".")
  132. var appVersionCode = ""
  133. for i in array {
  134. appVersionCode.append(i)
  135. }
  136. if appVersionCode.count < 3 {
  137. appVersionCode.append("0")
  138. }
  139. QSLApi.getAppVersionCode(appVersionCode: Int(appVersionCode) ?? 100)
  140. } else {
  141. debugPrint("Unable to retrieve app version")
  142. }
  143. if let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String {
  144. debugPrint("当前的Build号: \(buildNumber)")
  145. }
  146. // 获取系统版本
  147. let osVersion = String(format: "%@%@", UIDevice.current.systemName, UIDevice.current.systemVersion)
  148. QSLApi.getOSVersion(osVersion: osVersion)
  149. // 获取 设置uuid 并更新
  150. if let uuid = UIDevice.current.identifierForVendor?.uuidString {
  151. QSLApi.getIDFV(newIDFV: uuid)
  152. debugPrint("IDFV ID: \(uuid)")
  153. } else {
  154. debugPrint("获取 IDFV ID 参数失败")
  155. }
  156. }
  157. func initIDFA() {
  158. DispatchQueue.global().asyncAfter(deadline: .now() + 0.5){
  159. if #available(iOS 14.0, *) {
  160. // 用户请求授权获得IDFA权限
  161. ATTrackingManager.requestTrackingAuthorization { status in
  162. if status == .authorized {
  163. debugPrint("授权成功")
  164. // 用户已授权
  165. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  166. debugPrint("IDFA: \(idfa.uuidString)")
  167. QSLApi.getIDFA(newIDFA: idfa.uuidString)
  168. self.initGE()
  169. } else {
  170. debugPrint("无法获取idfa")
  171. self.initGE()
  172. }
  173. }
  174. } else {
  175. // 检查用户是否授权应用使用广告标识符
  176. if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
  177. // 获取 IDFA
  178. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  179. QSLApi.getIDFA(newIDFA: idfa.uuidString)
  180. debugPrint("IDFA: \(idfa)")
  181. self.initGE()
  182. } else {
  183. debugPrint("用户拒绝广告追踪")
  184. self.initGE()
  185. }
  186. }
  187. }
  188. }
  189. // 引力引擎初始化
  190. func initGE() {
  191. isLoadG = true
  192. DispatchQueue.main.async {
  193. if gravityInstance == nil {
  194. let gravity = QSLGravityManager()
  195. gravity.initGE()
  196. }
  197. }
  198. //苹果初始化归因
  199. QSAppleAdHandle.initQSC()
  200. }
  201. }