mirror of
https://github.com/hub-team/OnlineChatSdk-SwiftPM.git
synced 2026-04-15 00:54:05 +00:00
update
This commit is contained in:
@@ -10,10 +10,11 @@ import Foundation
|
|||||||
|
|
||||||
open class ChatApi {
|
open class ChatApi {
|
||||||
|
|
||||||
private func post(_ url: String, _ params: Dictionary<String, Any>, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
|
private func post(_ url: String, _ token: String, _ params: Dictionary<String, Any>, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
|
||||||
guard let url = URL(string: url) else { return }
|
guard let url = URL(string: url) else { return }
|
||||||
var request = URLRequest(url: url)
|
var request = URLRequest(url: url)
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
|
request.addValue(token, forHTTPHeaderField: "X-Token")
|
||||||
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else { return }
|
guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else { return }
|
||||||
request.httpBody = httpBody
|
request.httpBody = httpBody
|
||||||
@@ -23,8 +24,8 @@ open class ChatApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func send(_ token: String, _ method: String, _ params: Dictionary<String, Any>, callback: @escaping (NSDictionary?) -> Void) {
|
public func send(_ token: String, _ method: String, _ params: Dictionary<String, Any>, callback: @escaping (NSDictionary?) -> Void) {
|
||||||
let url = "https://admin.verbox.ru/api/chat/\(token)/\(method)"
|
let url = "https://admin.verbox.ru/json/v1.0/\(method)"
|
||||||
post(url, params) { (data, response, error) in
|
post(url, token, params) { (data, response, error) in
|
||||||
guard let data = data else { return }
|
guard let data = data else { return }
|
||||||
do {
|
do {
|
||||||
let json = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
|
let json = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
|
||||||
@@ -34,10 +35,9 @@ open class ChatApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func messages(_ token: String, params: Dictionary<String, Any>, callback: @escaping (NSDictionary?) -> Void) {
|
public func messages(_ token: String, params: Dictionary<String, Any>, callback: @escaping (NSDictionary?) -> Void) {
|
||||||
send(token, "message", params, callback: callback)
|
send(token, "chat/message/getList", params, callback: callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static func getNewMessages(_ token: String, _ clientId: String, callback: @escaping (NSDictionary?) -> Void) {
|
public static func getNewMessages(_ token: String, _ clientId: String, callback: @escaping (NSDictionary?) -> Void) {
|
||||||
let dtFormatter = DateFormatter()
|
let dtFormatter = DateFormatter()
|
||||||
dtFormatter.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
|
dtFormatter.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
|
||||||
|
|||||||
@@ -2,7 +2,50 @@ import Foundation
|
|||||||
|
|
||||||
class ChatApiMessagesWrapper {
|
class ChatApiMessagesWrapper {
|
||||||
|
|
||||||
init(_ response: Dictionary<String, Any>) {
|
private var result: Dictionary<String, Any>?
|
||||||
print("ChatApiMessagesWrapper : \(response.debugDescription)")
|
private var dataArray: Array<Dictionary<String, Any>>?
|
||||||
|
private var data: Dictionary<String, Any>?
|
||||||
|
private var messages: Array<Dictionary<String, Any>>?
|
||||||
|
|
||||||
|
init(_ response: NSDictionary) {
|
||||||
|
self.result = response as? Dictionary<String, Any>
|
||||||
|
self.dataArray = []
|
||||||
|
self.data = [:]
|
||||||
|
self.messages = []
|
||||||
|
if response["result"] == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.dataArray = response["result"] as? Array<Dictionary<String, Any>>
|
||||||
|
if self.dataArray == nil || self.dataArray?.count == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.data = dataArray![0]
|
||||||
|
if self.data == nil || self.data!["messages"] == nil {
|
||||||
|
self.data = [:]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.messages = self.data!["messages"] as? Array<Dictionary<String, Any>>
|
||||||
|
if self.messages == nil {
|
||||||
|
self.messages = []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public func getMessages() -> NSArray {
|
||||||
|
self.messages! as NSArray
|
||||||
|
}
|
||||||
|
|
||||||
|
public func setMessages(_ messages: NSArray) {
|
||||||
|
self.messages = messages as? Array<Dictionary<String, Any>>
|
||||||
|
}
|
||||||
|
|
||||||
|
public func getResult() -> NSDictionary {
|
||||||
|
self.data!["messages"] = self.messages
|
||||||
|
if self.dataArray?.count == 0 {
|
||||||
|
self.dataArray = [self.data!]
|
||||||
|
} else {
|
||||||
|
self.dataArray![0] = self.data!
|
||||||
|
}
|
||||||
|
self.result?["result"] = self.dataArray
|
||||||
|
return self.result! as NSDictionary
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,6 +56,6 @@ class ChatConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func getConfigString(_ key: String) -> String {
|
private func getConfigString(_ key: String) -> String {
|
||||||
self.config.value(forKey: key) as! String
|
self.config.value(forKey: key) != nil ? self.config.value(forKey: key) as! String : ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,26 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
private var didFinish: Bool = false
|
private var didFinish: Bool = false
|
||||||
private var widgetUrl: String = ""
|
private var widgetUrl: String = ""
|
||||||
|
|
||||||
|
private static func getUnreadedMessagesCallback(_ result: NSDictionary) -> NSDictionary {
|
||||||
|
let resultWrapper = ChatApiMessagesWrapper(result)
|
||||||
|
if resultWrapper.getMessages().count == 0 {
|
||||||
|
return resultWrapper.getResult()
|
||||||
|
}
|
||||||
|
var unreadedMessages: Array<NSDictionary> = []
|
||||||
|
for message: NSDictionary in resultWrapper.getMessages() as! Array<NSDictionary> {
|
||||||
|
if message.value(forKey: "isVisibleForClient") != nil {
|
||||||
|
if (message.value(forKey: "isVisibleForClient") as! Int) == 1 {
|
||||||
|
unreadedMessages.append(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if unreadedMessages.count == 0 {
|
||||||
|
return resultWrapper.getResult()
|
||||||
|
}
|
||||||
|
resultWrapper.setMessages(unreadedMessages as NSArray)
|
||||||
|
return resultWrapper.getResult()
|
||||||
|
}
|
||||||
|
|
||||||
private static func getUnreadedMessages(_ startDate: String, callback: @escaping (NSDictionary?) -> Void) {
|
private static func getUnreadedMessages(_ startDate: String, callback: @escaping (NSDictionary?) -> Void) {
|
||||||
let token = ChatConfig.getApiToken()
|
let token = ChatConfig.getApiToken()
|
||||||
if token == "" {
|
if token == "" {
|
||||||
@@ -54,9 +74,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
]
|
]
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
let dtFormat = ChatDateFormatter()
|
|
||||||
let currentDate = NSDate() as Date
|
|
||||||
|
|
||||||
ChatApi().messages(token, params: [
|
ChatApi().messages(token, params: [
|
||||||
"client": [
|
"client": [
|
||||||
"clientId": clientId
|
"clientId": clientId
|
||||||
@@ -65,28 +82,41 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
"status": "unreaded",
|
"status": "unreaded",
|
||||||
"dateRange": [
|
"dateRange": [
|
||||||
"start": startDate,
|
"start": startDate,
|
||||||
"stop": dtFormat.string(from: currentDate)
|
"stop": ChatDateFormatter().getCurrent()
|
||||||
]
|
]
|
||||||
] as [String: Any], callback: {(result) in
|
] as [String: Any], callback: {(result) in
|
||||||
|
callback( ChatController.getUnreadedMessagesCallback(result!) )
|
||||||
ChatApiMessagesWrapper(result as! Dictionary<String, Any>)
|
|
||||||
|
|
||||||
callback([
|
|
||||||
"success": false,
|
|
||||||
"error": [
|
|
||||||
"code": 0,
|
|
||||||
"descr": "Не реализовано"
|
|
||||||
]
|
|
||||||
])
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func getUnreadedMessages(callback: @escaping (NSDictionary?) -> Void) {
|
public static func getUnreadedMessages(callback: @escaping (NSDictionary?) -> Void) {
|
||||||
|
let startDate = ChatDateFormatter().string(from: Date(timeIntervalSince1970: TimeInterval(Int(NSDate().timeIntervalSince1970) - 86400 * 14)))
|
||||||
|
ChatController.getUnreadedMessages(startDate, callback: callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func getNewMessagesCallback(_ result: NSDictionary) -> NSDictionary {
|
||||||
|
let resultWrapper = ChatApiMessagesWrapper(result)
|
||||||
|
if resultWrapper.getMessages().count == 0 {
|
||||||
|
return resultWrapper.getResult()
|
||||||
|
}
|
||||||
|
let lastMessage = resultWrapper.getMessages()[resultWrapper.getMessages().count - 1] as! NSDictionary
|
||||||
|
let lastDate = ChatDateFormatter().date(from: lastMessage["dateTime"] as! String)
|
||||||
|
let nextDate = Date(timeIntervalSince1970: TimeInterval( Int(lastDate!.timeIntervalSince1970) + 1 ))
|
||||||
|
ChatConfig.setLastDateTimeNewMessage( ChatDateFormatter().string(from: nextDate) )
|
||||||
|
return resultWrapper.getResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func getNewMessages(callback: @escaping (NSDictionary?) -> Void) {
|
public static func getNewMessages(callback: @escaping (NSDictionary?) -> Void) {
|
||||||
|
let startDate = ChatConfig.getLastDateTimeNewMessage()
|
||||||
|
if startDate == "" {
|
||||||
|
self.getUnreadedMessages(callback: {(result) in
|
||||||
|
callback( ChatController.getNewMessagesCallback(result!) )
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
self.getUnreadedMessages(startDate, callback: {(result) in
|
||||||
|
callback( ChatController.getNewMessagesCallback(result!) )
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override public func loadView() {
|
override public func loadView() {
|
||||||
@@ -331,4 +361,4 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
open func getContactsCallback(_ data: NSDictionary) {
|
open func getContactsCallback(_ data: NSDictionary) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class ChatDateFormatter : DateFormatter {
|
class ChatDateFormatter : DateFormatter {
|
||||||
|
|
||||||
override init() {
|
override init() {
|
||||||
super.init()
|
super.init()
|
||||||
self.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
|
self.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
|
||||||
@@ -12,4 +13,8 @@ class ChatDateFormatter : DateFormatter {
|
|||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func getCurrent() -> String {
|
||||||
|
self.string(from: NSDate() as Date)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user