QSLBaseManager.swift 7.7 KB

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