O2IM.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. let o2_im_msg_type_process = "process"
  23. let o2_im_msg_type_cms = "cms"
  24. //消息body
  25. let o2_im_msg_body_image = "[图片]"
  26. let o2_im_msg_body_audio = "[语音]"
  27. let o2_im_msg_body_video = "[视频]"
  28. let o2_im_msg_body_location = "[位置]"
  29. let o2_im_msg_body_file = "[文件]"
  30. let o2_im_msg_body_process = "[流程工作]"
  31. let o2_im_msg_body_cms = "[信息文章]"
  32. let messageWidth: CGFloat = 176
  33. //表情的字符串转化为O2Emoji.bundle里面的图片路径 [01] -> im_emotion_01
  34. func o2ImEmojiPath(emojiBody: String) -> String {
  35. if emojiBody.length == 4 {
  36. let s = emojiBody.subString(from: 1, to: 3)
  37. return "im_emotion_\(s)"
  38. }
  39. return ""
  40. }
  41. class O2IMFileManager {
  42. static let shared: O2IMFileManager = {
  43. return O2IMFileManager()
  44. }()
  45. private let communicateAPI = {
  46. return OOMoyaProvider<CommunicateAPI>()
  47. }()
  48. private init() { }
  49. //根据id下载文件,并返回文件的本地url
  50. func getFileLocalUrl(fileId: String, fileExtension: String) -> Promise<URL> {
  51. return Promise { fulfill, reject in
  52. let url = self.localFilePath(fileId: fileId, ext: fileExtension)
  53. if FileUtil.share.fileExist(filePath: url.path) {
  54. fulfill(url)
  55. } else {
  56. self.communicateAPI.request(.imDownloadFullFile(fileId, fileExtension), completion: { result in
  57. switch result {
  58. case .success(_):
  59. DDLogError("下载成功。。。。。\(fileId)")
  60. fulfill(url)
  61. break
  62. case .failure(let err):
  63. DDLogError(err.localizedDescription)
  64. reject(err)
  65. break
  66. }
  67. })
  68. }
  69. }
  70. }
  71. func localFilePath(fileId: String, ext: String) -> URL {
  72. return FileUtil.share.cacheDir().appendingPathComponent("\(fileId).\(ext)")
  73. }
  74. //音频文件存储地址
  75. func getRecorderPath(type: RecordType) -> String {
  76. var recorderPath = FileUtil.share.cacheDir()
  77. recorderPath.appendPathComponent("o2im")
  78. //目录不存在就创建
  79. DDLogDebug("开始创建目录\(recorderPath.path)")
  80. FileUtil.share.createDirectory(path: recorderPath.path)
  81. let now:Date = Date()
  82. let dateFormatter = DateFormatter()
  83. dateFormatter.dateFormat = "yyyy-MM-dd-hh-mm-ss"
  84. let fileName = (type == RecordType.Caf) ? "\(dateFormatter.string(from: now))-MySound.caf" : "\(dateFormatter.string(from: now))-MySound.mp3"
  85. recorderPath.appendPathComponent(fileName)
  86. return recorderPath.path
  87. }
  88. }
  89. enum RecordType :String {
  90. case Caf = "caf"
  91. case MP3 = "mp3"
  92. }