KeyboardApi.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // KeyboardApi.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/28.
  6. //
  7. import Foundation
  8. import UIKit
  9. import AdSupport
  10. import AppTrackingTransparency
  11. enum NetworkEnvironment {
  12. case local
  13. case dev
  14. case prod
  15. }
  16. struct KeyboardApi {
  17. static let env: NetworkEnvironment = .prod
  18. static let localUrl = "http://192.168.10.113:8880"
  19. static let devUrl = "http://42.193.245.11"
  20. static let prodUrl = "https://project-api.atmob.com"
  21. static func getBaseUrl() -> String {
  22. switch KeyboardApi.env {
  23. case .local:
  24. return KeyboardApi.localUrl
  25. case .dev:
  26. return KeyboardApi.devUrl
  27. case .prod:
  28. return KeyboardApi.prodUrl
  29. }
  30. }
  31. static var params: [String:Any] = [
  32. "channelName":"appstore",
  33. "idfa":"",
  34. "idfv":"",
  35. "packageName":"com.qihuan.zhuiaijianpan",
  36. "appPlatform":2,
  37. "appVersionName":"1.0.0",
  38. "appVersionCode":100,
  39. "authToken": ""
  40. ]
  41. static func updateAppVersionName(appVersionName: String) {
  42. params["appVersionName"] = appVersionName
  43. }
  44. static func updateAppVersionCode(appVersionCode: Int) {
  45. params["appVersionCode"] = appVersionCode
  46. }
  47. static func updateIDFV(idfv: String) {
  48. params["idfv"] = idfv
  49. }
  50. static func updateIDFA(idfa: String) {
  51. params["idfa"] = idfa
  52. }
  53. static func updateAuthToken(authToken: String) {
  54. params["authToken"] = authToken
  55. }
  56. // 初始化基本参数
  57. static func initParams() {
  58. // 获取版本号
  59. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  60. debugPrint("App Version: \(appVersion)")
  61. KeyboardApi.updateAppVersionName(appVersionName: appVersion)
  62. let array = appVersion.components(separatedBy: ".")
  63. var appVersionCode = ""
  64. for i in array {
  65. appVersionCode.append(i)
  66. }
  67. if appVersionCode.count < 3 {
  68. appVersionCode.append("0")
  69. }
  70. KeyboardApi.updateAppVersionCode(appVersionCode: Int(appVersionCode) ?? 100)
  71. } else {
  72. debugPrint("Unable to retrieve app version")
  73. }
  74. // 获取 设置uuid 并更新
  75. // if let uuid = UIDevice.current.identifierForVendor?.uuidString {
  76. // KeyboardApi.updateIDFV(idfv: uuid)
  77. // debugPrint("IDFV ID: \(uuid)")
  78. // } else {
  79. // debugPrint("获取 IDFV ID 参数失败")
  80. // }
  81. if let idfv = KeyboardSharedDataManager.shared.getInitIDFV() {
  82. KeyboardApi.updateIDFV(idfv: idfv)
  83. debugPrint("IDFV ID: \(idfv)")
  84. } else {
  85. debugPrint("获取 IDFV ID 参数失败")
  86. }
  87. // 获取idfa
  88. // initIDFA()
  89. if let idfa = KeyboardSharedDataManager.shared.getInitIDFA() {
  90. KeyboardApi.updateIDFA(idfa: idfa)
  91. debugPrint("IDFA ID: \(idfa)")
  92. } else {
  93. debugPrint("获取 IDFA ID 参数失败")
  94. }
  95. // 获取token
  96. if let authToken = KeyboardSharedDataManager.shared.getToken() {
  97. KeyboardApi.updateAuthToken(authToken: authToken)
  98. }
  99. }
  100. // 初始化idfa
  101. static func initIDFA() {
  102. DispatchQueue.global().asyncAfter(deadline: .now() + 0.1){
  103. if #available(iOS 14.0, *) {
  104. // 用户请求授权获得IDFA权限
  105. ATTrackingManager.requestTrackingAuthorization { status in
  106. if status == .authorized {
  107. debugPrint("授权成功")
  108. // 用户已授权
  109. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  110. debugPrint("IDFA: \(idfa.uuidString)")
  111. KeyboardApi.updateIDFA(idfa: idfa.uuidString)
  112. } else {
  113. debugPrint("无法获取idfa")
  114. }
  115. }
  116. } else {
  117. // 检查用户是否授权应用使用广告标识符
  118. if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
  119. // 获取 IDFA
  120. let idfa = ASIdentifierManager.shared().advertisingIdentifier
  121. KeyboardApi.updateIDFA(idfa: idfa.uuidString)
  122. debugPrint("IDFA: \(idfa)")
  123. } else {
  124. debugPrint("用户拒绝广告追踪")
  125. }
  126. }
  127. }
  128. }
  129. }
  130. extension KeyboardApi {
  131. // 获取键盘人设列表
  132. static let character_list = "/project/keyboard/v1/character/list"
  133. // 获取键盘列表
  134. static let keyboard_list = "/project/keyboard/v1/keyboard/list"
  135. // 选择键盘
  136. static let keyboard_choose = "/project/keyboard/v1/keyboard/choose"
  137. // 获取开场白列表
  138. static let prologue_list = "/project/keyboard/v1/keyboard/prologue/list"
  139. // 获取用户信息
  140. static let user_info = "/project/keyboard/v1/user/info"
  141. }
  142. extension KeyboardApi {
  143. // 超会回
  144. static let chat_super_reply = "/project/keyboard/v1/chat/superReply"
  145. // 超会说
  146. static let chat_super_speak = "/project/keyboard/v1/chat/superSpeak"
  147. // 开场白
  148. static let chat_super_prologue = "/project/keyboard/v1/chat/prologue"
  149. }