O2IM.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // O2IM.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/4.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import Promises
  10. import CocoaLumberjack
  11. //心跳消息
  12. let o2_im_ws_heartbeat = "heartbeat"
  13. let o2_im_conversation_type_single = "single"
  14. let o2_im_conversation_type_group = "group"
  15. //消息分类
  16. let o2_im_msg_type_text = "text"
  17. let o2_im_msg_type_emoji = "emoji"
  18. let o2_im_msg_type_image = "image"
  19. let o2_im_msg_type_audio = "audio"
  20. let o2_im_msg_type_location = "location"
  21. let o2_im_msg_type_file = "file"
  22. //消息body
  23. let o2_im_msg_body_image = "[图片]"
  24. let o2_im_msg_body_audio = "[语音]"
  25. let o2_im_msg_body_video = "[视频]"
  26. let o2_im_msg_body_location = "[位置]"
  27. let o2_im_msg_body_file = "[文件]"
  28. let messageWidth: CGFloat = 176
  29. //表情的字符串转化为O2Emoji.bundle里面的图片路径 [01] -> im_emotion_01
  30. func o2ImEmojiPath(emojiBody: String) -> String {
  31. if emojiBody.length == 4 {
  32. let s = emojiBody.subString(from: 1, to: 3)
  33. return "im_emotion_\(s)"
  34. }
  35. return ""
  36. }
  37. class O2IMFileManager {
  38. static let shared: O2IMFileManager = {
  39. return O2IMFileManager()
  40. }()
  41. private let communicateAPI = {
  42. return OOMoyaProvider<CommunicateAPI>()
  43. }()
  44. private init() { }
  45. //根据id下载文件,并返回文件的本地url
  46. func getFileLocalUrl(fileId: String, fileExtension: String) -> Promise<URL> {
  47. return Promise { fulfill, reject in
  48. let url = self.localFilePath(fileId: fileId, ext: fileExtension)
  49. if FileUtil.share.fileExist(filePath: url.path) {
  50. fulfill(url)
  51. } else {
  52. self.communicateAPI.request(.imDownloadFullFile(fileId, fileExtension), completion: { result in
  53. switch result {
  54. case .success(_):
  55. DDLogError("下载成功。。。。。\(fileId)")
  56. fulfill(url)
  57. break
  58. case .failure(let err):
  59. DDLogError(err.localizedDescription)
  60. reject(err)
  61. break
  62. }
  63. })
  64. }
  65. }
  66. }
  67. func localFilePath(fileId: String, ext: String) -> URL {
  68. return FileUtil.share.cacheDir().appendingPathComponent("\(fileId).\(ext)")
  69. }
  70. //音频文件存储地址
  71. func getRecorderPath(type: RecordType) -> String {
  72. var recorderPath = FileUtil.share.cacheDir()
  73. recorderPath.appendPathComponent("o2im")
  74. //目录不存在就创建
  75. DDLogDebug("开始创建目录\(recorderPath.path)")
  76. FileUtil.share.createDirectory(path: recorderPath.path)
  77. let now:Date = Date()
  78. let dateFormatter = DateFormatter()
  79. dateFormatter.dateFormat = "yyyy-MM-dd-hh-mm-ss"
  80. let fileName = (type == RecordType.Caf) ? "\(dateFormatter.string(from: now))-MySound.caf" : "\(dateFormatter.string(from: now))-MySound.mp3"
  81. recorderPath.appendPathComponent(fileName)
  82. return recorderPath.path
  83. }
  84. }
  85. enum RecordType :String {
  86. case Caf = "caf"
  87. case MP3 = "mp3"
  88. }