SampleEditionManger.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // SampleEditionManger.swift
  3. // O2Platform
  4. // 演示版本 管理器
  5. // Created by FancyLou on 2021/7/30.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import CocoaLumberjack
  9. class SampleEditionManger {
  10. static let shared: SampleEditionManger = {
  11. return SampleEditionManger()
  12. }()
  13. private init(){
  14. initConfig()
  15. }
  16. var unitList: [O2BindUnitModel] = []
  17. private var currentUnit: O2BindUnitModel? = nil
  18. func initConfig() {
  19. readUnitListFromInfoplist()
  20. currentUnit = readCurrentServer()
  21. }
  22. /// 切换环境 需要重启应用
  23. func setCurrent(unit: O2BindUnitModel) {
  24. currentUnit = unit
  25. O2UserDefaults.shared.sampleUnit = unit
  26. }
  27. /// 获取当前环境
  28. func getCurrentUnit() -> O2BindUnitModel {
  29. if let unit = currentUnit {
  30. return unit
  31. }
  32. let unit = O2BindUnitModel()
  33. unit.id = "sample"
  34. unit.centerContext = "/x_program_center"
  35. unit.centerHost = "sample.o2oa.net"
  36. unit.centerPort = 40030
  37. unit.httpProtocol = "https"
  38. unit.name = "演示环境"
  39. return unit
  40. }
  41. /// 读取Info.plist 文件中 o2SampleServerList 数据
  42. private func readUnitListFromInfoplist() {
  43. if let infoPath = Bundle.main.path(forResource: "Info", ofType: "plist"), let dic = NSDictionary(contentsOfFile: infoPath) {
  44. if let list = dic["o2SampleServerList"] as? NSArray {
  45. for item in list {
  46. if let o2Server = item as? NSDictionary {
  47. let id = o2Server["id"] as? String
  48. let name = o2Server["name"] as? String
  49. let centerHost = o2Server["centerHost"] as? String
  50. let centerContext = o2Server["centerContext"] as? String
  51. let centerPort = o2Server["centerPort"] as? Int
  52. let httpProtocol = o2Server["httpProtocol"] as? String
  53. let unit = O2BindUnitModel()
  54. unit.id = id
  55. unit.centerContext = centerContext
  56. unit.centerHost = centerHost
  57. unit.centerPort = centerPort
  58. unit.httpProtocol = httpProtocol
  59. unit.name = name
  60. DDLogDebug("unit : \(unit.description)")
  61. unitList.append(unit)
  62. }
  63. }
  64. }
  65. }
  66. }
  67. /// 读取当前绑定的演示服务器
  68. private func readCurrentServer() -> O2BindUnitModel {
  69. if let unit = O2UserDefaults.shared.sampleUnit {
  70. return unit
  71. } else {
  72. let unit = unitList[0]
  73. O2UserDefaults.shared.sampleUnit = unit
  74. return unit
  75. }
  76. }
  77. }