O2Collect.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // O2Collect.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/6/29.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import Alamofire
  10. import SwiftyJSON
  11. import CocoaLumberjack
  12. class O2Collect{
  13. static let T_QUERY = "#QUERY#"
  14. //变量
  15. var moduleWebContextDict:[String:String]?
  16. var moduleHostAndWebContextDict:[String:JSON]?
  17. var webModuleHostAndWebContextDict:[String:JSON]?
  18. var collectUnitData: O2BindUnitModel?
  19. init(){
  20. self.moduleWebContextDict = [:]
  21. self.moduleHostAndWebContextDict = [:]
  22. self.webModuleHostAndWebContextDict = [:]
  23. }
  24. /**
  25. 生成API调用URL
  26. - parameter appContextKey: appContextKey
  27. - parameter scheme: 请求应用协议
  28. - parameter query: 查询路径
  29. - parameter parameter: 请求参数
  30. - returns: 生成的URL
  31. */
  32. func generateURLWithAppContextKey(_ appContextKey:String,scheme:String,query:String,parameter:[String:AnyObject]?,coverted:Bool=true,generateTime:Bool=true) -> String?{
  33. let nodeAPI = O2AuthSDK.shared.centerServerInfo()?.assembles![appContextKey]
  34. var baseURL = "".appendingFormat("%@://%@:%@%@/%@", nodeAPI?.httpProtocol ?? "http",(nodeAPI?.host)!,String((nodeAPI?.port)!),(nodeAPI?.context)!,query)
  35. if let t_parameter = parameter {
  36. for (key,value) in t_parameter {
  37. baseURL = baseURL.replacingOccurrences(of: key, with:value as! String)
  38. }
  39. }
  40. //return self.generateTimestampWithURL(baseURL)!
  41. //加入时间截
  42. if coverted{
  43. if(generateTime){
  44. baseURL = self.generateTimestampWithURL(baseURL)!
  45. }
  46. return baseURL.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
  47. }else{
  48. if(generateTime){
  49. baseURL = self.generateTimestampWithURL(baseURL)!
  50. }
  51. return baseURL
  52. }
  53. }
  54. /**
  55. 生成API调用URL
  56. - parameter appContextKey: appContextKey
  57. - parameter scheme: "http"
  58. - parameter query: 查询路径
  59. - parameter parameter: 请求参数
  60. - returns: 生成的URL
  61. */
  62. func generateURLWithAppContextKey(_ appContextKey:String,query:String,parameter:[String:AnyObject]?,coverted:Bool=true,generateTime:Bool=true) -> String?{
  63. return self.generateURLWithAppContextKey(appContextKey, scheme: "http", query: query, parameter: parameter,coverted: coverted,generateTime: generateTime)
  64. }
  65. /**
  66. 生成websocket连接地址
  67. */
  68. func generateWebsocketURL() -> String {
  69. if let webServer = O2AuthSDK.shared.centerServerInfo()?.webServer, let communicateNode = O2AuthSDK.shared.centerServerInfo()?.assembles?["x_message_assemble_communicate"] {
  70. var wsProtocol = "ws://"
  71. if webServer.httpProtocol == "https" {
  72. wsProtocol = "wss://"
  73. }
  74. let tokenName = O2AuthSDK.shared.tokenName()
  75. return "\(wsProtocol)\(communicateNode.host ?? ""):\(communicateNode.port ?? 20020)\(communicateNode.context ?? "/x_message_assemble_communicate")/ws/collaboration?\(tokenName)=\(O2AuthSDK.shared.myInfo()?.token ?? "")"
  76. }
  77. return ""
  78. }
  79. /**
  80. 前台请求H5页面路径生成方法
  81. - parameter webAppContextkey: webAppContext上下文
  82. - parameter query: query路径
  83. - parameter parameter: 参数字典
  84. - returns: 返回请求的实际URL地址
  85. */
  86. func genrateURLWithWebContextKey(_ webAppContextkey:String,query:String,parameter:[String:AnyObject]?,covertd:Bool=true) -> String?{
  87. let webAPI = O2AuthSDK.shared.centerServerInfo()?.webServer
  88. var baseURL = "".appendingFormat("%@://%@:%@/%@/%@", webAPI?.httpProtocol ?? "http",(webAPI?.host)!,String((webAPI?.port)!),webAppContextkey,query)
  89. if let param = parameter {
  90. for (key,value) in param {
  91. baseURL = baseURL.replacingOccurrences(of: key, with:value as! String)
  92. }
  93. }
  94. if covertd {
  95. return self.generateTimestampWithURL(baseURL)?.urlEncoded
  96. }else{
  97. return self.generateTimestampWithURL(baseURL)
  98. }
  99. }
  100. func genrateURLWithWebContextKey2(_ query:String,parameter:[String:AnyObject]?,covertd:Bool=true) -> String?{
  101. let webAPI = O2AuthSDK.shared.centerServerInfo()?.webServer
  102. var baseURL = "".appendingFormat("%@://%@:%@/%@", webAPI?.httpProtocol ?? "http",(webAPI?.host)!,String((webAPI?.port)!),query)
  103. if let param = parameter {
  104. for (key,value) in param {
  105. baseURL = baseURL.replacingOccurrences(of: key, with:value as! String)
  106. }
  107. }
  108. if covertd {
  109. return self.generateTimestampWithURL(baseURL)?.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
  110. }else{
  111. return self.generateTimestampWithURL(baseURL)
  112. }
  113. }
  114. /**
  115. 对指定的请求路径进行替换生成新的URL
  116. - parameter baseURL: 带有query参数URL
  117. - parameter requestQuery: 实际的Query上下文参数
  118. - returns: 实际的URL
  119. */
  120. func setRequestQuery(_ baseURL:String,requestQuery:String)-> String?{
  121. //return [baseUrl stringByReplacingOccurrencesOfString:T_QUERY withString:requestQuery];
  122. return baseURL.replacingOccurrences(of: O2Collect.T_QUERY, with: requestQuery)
  123. }
  124. /**
  125. 给请求的URL增加参数
  126. - parameter baseURL: 带有参数的URL
  127. - parameter requestParameter: 参数key.value值对
  128. - returns: 将参数设置为了实际值的URL
  129. */
  130. func setRequestParameter(_ baseURL:String,requestParameter:[String:AnyObject]?) -> String?{
  131. var t_url = baseURL
  132. if let param = requestParameter {
  133. for (key,value) in param {
  134. t_url = t_url.replacingOccurrences(of: key, with: value as! String)
  135. }
  136. }
  137. return t_url
  138. }
  139. /**
  140. 生成时间戳
  141. - parameter baseURL: 需要加入时间戳的URL
  142. - returns: 加入时间戳的URL
  143. */
  144. func generateTimestampWithURL(_ baseURL:String) -> String? {
  145. var resultURL:String?
  146. if baseURL.contains("?") {
  147. resultURL = baseURL.appending("&" + RandomString.sharedInstance.getRandomStringOfLength(length: 10))
  148. }else{
  149. resultURL = baseURL.appending("?" + RandomString.sharedInstance.getRandomStringOfLength(length: 10))
  150. }
  151. return resultURL
  152. }
  153. }