O2Collect.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. return "\(wsProtocol)\(communicateNode.host ?? ""):\(communicateNode.port ?? 20020)\(communicateNode.context ?? "/x_message_assemble_communicate")/ws/collaboration?x-token=\(O2AuthSDK.shared.myInfo()?.token ?? "")"
  75. }
  76. return ""
  77. }
  78. /**
  79. 前台请求H5页面路径生成方法
  80. - parameter webAppContextkey: webAppContext上下文
  81. - parameter query: query路径
  82. - parameter parameter: 参数字典
  83. - returns: 返回请求的实际URL地址
  84. */
  85. func genrateURLWithWebContextKey(_ webAppContextkey:String,query:String,parameter:[String:AnyObject]?,covertd:Bool=true) -> String?{
  86. let webAPI = O2AuthSDK.shared.centerServerInfo()?.webServer
  87. var baseURL = "".appendingFormat("%@://%@:%@/%@/%@", webAPI?.httpProtocol ?? "http",(webAPI?.host)!,String((webAPI?.port)!),webAppContextkey,query)
  88. if let param = parameter {
  89. for (key,value) in param {
  90. baseURL = baseURL.replacingOccurrences(of: key, with:value as! String)
  91. }
  92. }
  93. if covertd {
  94. return self.generateTimestampWithURL(baseURL)?.urlEncoded
  95. }else{
  96. return self.generateTimestampWithURL(baseURL)
  97. }
  98. }
  99. func genrateURLWithWebContextKey2(_ query:String,parameter:[String:AnyObject]?,covertd:Bool=true) -> String?{
  100. let webAPI = O2AuthSDK.shared.centerServerInfo()?.webServer
  101. var baseURL = "".appendingFormat("%@://%@:%@/%@", webAPI?.httpProtocol ?? "http",(webAPI?.host)!,String((webAPI?.port)!),query)
  102. if let param = parameter {
  103. for (key,value) in param {
  104. baseURL = baseURL.replacingOccurrences(of: key, with:value as! String)
  105. }
  106. }
  107. if covertd {
  108. return self.generateTimestampWithURL(baseURL)?.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
  109. }else{
  110. return self.generateTimestampWithURL(baseURL)
  111. }
  112. }
  113. /**
  114. 对指定的请求路径进行替换生成新的URL
  115. - parameter baseURL: 带有query参数URL
  116. - parameter requestQuery: 实际的Query上下文参数
  117. - returns: 实际的URL
  118. */
  119. func setRequestQuery(_ baseURL:String,requestQuery:String)-> String?{
  120. //return [baseUrl stringByReplacingOccurrencesOfString:T_QUERY withString:requestQuery];
  121. return baseURL.replacingOccurrences(of: O2Collect.T_QUERY, with: requestQuery)
  122. }
  123. /**
  124. 给请求的URL增加参数
  125. - parameter baseURL: 带有参数的URL
  126. - parameter requestParameter: 参数key.value值对
  127. - returns: 将参数设置为了实际值的URL
  128. */
  129. func setRequestParameter(_ baseURL:String,requestParameter:[String:AnyObject]?) -> String?{
  130. var t_url = baseURL
  131. if let param = requestParameter {
  132. for (key,value) in param {
  133. t_url = t_url.replacingOccurrences(of: key, with: value as! String)
  134. }
  135. }
  136. return t_url
  137. }
  138. /**
  139. 生成时间戳
  140. - parameter baseURL: 需要加入时间戳的URL
  141. - returns: 加入时间戳的URL
  142. */
  143. func generateTimestampWithURL(_ baseURL:String) -> String? {
  144. var resultURL:String?
  145. if baseURL.contains("?") {
  146. resultURL = baseURL.appending("&" + RandomString.sharedInstance.getRandomStringOfLength(length: 10))
  147. }else{
  148. resultURL = baseURL.appending("?" + RandomString.sharedInstance.getRandomStringOfLength(length: 10))
  149. }
  150. return resultURL
  151. }
  152. }