PublishUploadManager.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // PublishUploadManager.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/7/21.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyJSON
  10. class PublishUploadManager: NSObject {
  11. private static let _sharedInstance = PublishUploadManager()
  12. private override init() {} // 私有化init方法
  13. class func shared() -> PublishUploadManager {
  14. return _sharedInstance
  15. }
  16. // 视频上传参数
  17. var mediaType: PublishMediaType = .image
  18. var videoPath: String = ""
  19. var coverImagePath: String = ""
  20. var videoImage: UIImage? {
  21. didSet {
  22. imageArr = [videoImage] as? Array<UIImage>
  23. }
  24. }
  25. // 图片上传参数
  26. var imageArr: Array<UIImage>?
  27. var imageAssetUrlArr: Array<String>?
  28. // 发布参数
  29. var selTopicIdArr: Array<String> = []
  30. var pubTitle: String = ""
  31. var pubContent: String = ""
  32. var locationStr: String = ""
  33. // 视频id,当type为video时必填
  34. var paraVideo: String = ""
  35. // 主图Url
  36. var majorImageUrl: String?
  37. // 发布图片のUrlArray
  38. var imageUrlArray: Array<String> = []
  39. // 记录当前是否发布完成
  40. var isPublishFinished: Bool = true
  41. // 图片上传时当前循环次数
  42. var curTimes: Int = 0
  43. // 是否正在上传视频封面
  44. var isUploadingVideoCover: Bool = false
  45. var uploadManager: AlivcShortVideoUploadManager?
  46. var progressView: PublishUploadProgressView?
  47. }
  48. // MARK: - 视频上传流程
  49. extension PublishUploadManager {
  50. // 设置视频上传参数
  51. func setVideoPath(_ mediaType: PublishMediaType, _ videoPath: String, _ coverImagePath: String, _ videoImage: UIImage) {
  52. self.mediaType = mediaType
  53. self.videoPath = videoPath
  54. self.coverImagePath = coverImagePath
  55. self.videoImage = videoImage
  56. // 新发布时还原管理类内部的变量
  57. self.isUploadingVideoCover = false
  58. }
  59. // 获取上传地址和凭证(视频)
  60. func communityVideoUploadAuthApi() {
  61. isPublishFinished = false
  62. var vTitle: String = ""
  63. if videoPath.count > 20 {
  64. vTitle = String(videoPath.suffix(20))
  65. } else {
  66. vTitle = videoPath
  67. }
  68. SwiftMoyaNetWorkServiceCommunity.shared().communityVideoUploadAuthApi(title: vTitle, filename: vTitle, cover_url: coverImagePath) {
  69. [weak self] (communityVideoAuthModel) -> (Void) in
  70. let communityVideoAuthModel = communityVideoAuthModel as? CommunityVideoAuthModel
  71. self?.paraVideo = communityVideoAuthModel?.videoId ?? ""
  72. self?.startUploadVideo(communityVideoAuthModel!)
  73. }
  74. }
  75. // 上传视频
  76. func startUploadVideo(_ authModel: CommunityVideoAuthModel) {
  77. let info = AliyunUploadSVideoInfo()
  78. uploadManager = AlivcShortVideoUploadManager.shared()
  79. uploadManager?.setCoverImagePath(coverImagePath, videoInfo: info, videoPath: videoPath)
  80. uploadManager?.managerDelegate = self
  81. uploadManager?.uploadAddress = authModel.uploadAddress
  82. uploadManager?.videoId = authModel.videoId
  83. uploadManager?.uploadAuth = authModel.uploadAuth
  84. uploadManager?.startUpload()
  85. }
  86. // 获取上传地址和凭证(图片)
  87. func videoCoverImageUploadAuth() {
  88. SwiftMoyaNetWorkServiceCommunity.shared().communityImageUploadAuth {
  89. [weak self] (communityImageAuthModel) -> (Void) in
  90. let communityImageAuthModel = communityImageAuthModel as? CommunityImageAuthModel
  91. let imgWidth = Int(self?.videoImage!.size.width ?? 0)
  92. let imgHeight = Int(self?.videoImage!.size.height ?? 0)
  93. let urlStr = String(format: "%@?%ld_%ld", communityImageAuthModel?.imageURL ?? "", imgWidth, imgHeight)
  94. // 设置主图
  95. self?.majorImageUrl = urlStr
  96. self?.uploadVideoCoverImage(communityImageAuthModel!)
  97. }
  98. }
  99. // 上传视频封面图
  100. func uploadVideoCoverImage(_ authModel: CommunityImageAuthModel) {
  101. self.isUploadingVideoCover = true
  102. uploadManager = AlivcShortVideoUploadManager.shared()
  103. uploadManager?.setCoverImagePath(coverImagePath, videoInfo: nil, videoPath: "")
  104. uploadManager?.managerDelegate = self
  105. uploadManager?.uploadAddress = authModel.uploadAddress
  106. uploadManager?.uploadAuth = authModel.uploadAuth
  107. uploadManager?.startUpload()
  108. }
  109. }
  110. // MARK: - 图片上传流程
  111. extension PublishUploadManager {
  112. // 设置图片上传参数
  113. func setImagesPath(_ imageArr: Array<UIImage>, _ imageUrlArr: Array<String>) {
  114. self.imageArr = imageArr
  115. self.imageAssetUrlArr = imageUrlArr
  116. // 新发布时置空管理类内部的imageUrl数组及循环次数
  117. self.imageUrlArray = []
  118. self.curTimes = 0
  119. }
  120. // 获取上传地址和凭证(图片)
  121. func communityImageUploadAuth(totalTimes: Int) {
  122. isPublishFinished = false
  123. SwiftMoyaNetWorkServiceCommunity.shared().communityImageUploadAuth {
  124. [weak self] (communityImageAuthModel) -> (Void) in
  125. let communityImageAuthModel = communityImageAuthModel as? CommunityImageAuthModel
  126. let curImage: UIImage = self?.imageArr![totalTimes] ?? UIImage()
  127. let imgWidth = Int(curImage.size.width)
  128. let imgHeight = Int(curImage.size.height)
  129. let urlStr = String(format: "%@?%ld_%ld", communityImageAuthModel?.imageURL ?? "", imgWidth, imgHeight)
  130. self?.imageUrlArray.append(urlStr)
  131. self?.startUploadImage(communityImageAuthModel!, totalTimes)
  132. }
  133. }
  134. // 上传图片
  135. func startUploadImage(_ authModel: CommunityImageAuthModel, _ totalTimes: Int) {
  136. self.curTimes = totalTimes
  137. uploadManager = AlivcShortVideoUploadManager.shared()
  138. uploadManager?.setCoverImagePath(self.imageAssetUrlArr?[totalTimes], videoInfo: nil, videoPath: "")
  139. uploadManager?.managerDelegate = self
  140. uploadManager?.uploadAddress = authModel.uploadAddress
  141. uploadManager?.uploadAuth = authModel.uploadAuth
  142. uploadManager?.startUpload()
  143. }
  144. }
  145. // MARK: - 上传过程监听
  146. extension PublishUploadManager: AlivcShortVideoUploadManagerDelegate {
  147. // 上传进度回调
  148. func uploadManager(_ manager: AlivcShortVideoUploadManager!, updateProgress progress: CGFloat) {
  149. NXLLog("-------------------上传进度回调 - progress == \(progress)")
  150. DispatchQueue.main.async(execute: {
  151. if self.mediaType == .image {
  152. NXLLog("\n--------\(self.curTimes) uploadProgress == \(progress)")
  153. } else {
  154. self.progressView?.uploadProgress = Float(progress)
  155. }
  156. })
  157. }
  158. // 上传状态回调
  159. func uploadManager(_ manager: AlivcShortVideoUploadManager!, uploadStatusChangedTo newStatus: AlivcUploadStatus) {
  160. switch newStatus {
  161. case AlivcUploadStatus.failure:
  162. DispatchQueue.main.async(execute: {
  163. SwiftProgressHUD.shared().showText("上传失败!")
  164. self.isPublishFinished = true
  165. self.progressView?.curUploadStatus = .failure
  166. })
  167. case AlivcUploadStatus.success:
  168. DispatchQueue.main.async(execute: {
  169. if self.mediaType == .image {
  170. // 图片上传流程
  171. self.curTimes += 1
  172. // 更新进度
  173. NXLLog("----uploadProgress == \(Float(self.curTimes)/Float(self.imageArr!.count))")
  174. self.progressView?.uploadProgress = Float(self.curTimes/self.imageArr!.count)
  175. if self.curTimes < self.imageArr?.count ?? 0 {
  176. // 继续上传
  177. self.communityImageUploadAuth(totalTimes: self.curTimes)
  178. } else {
  179. // 图片上传成功,去发布
  180. // 设置主图
  181. self.majorImageUrl = self.imageUrlArray[0]
  182. self.progressView?.curUploadStatus = .success
  183. self.communityPublishApi()
  184. }
  185. } else {
  186. // 视频上传流程
  187. if self.isUploadingVideoCover {
  188. // 上传封面图の返回
  189. self.isUploadingVideoCover = false
  190. // 视频上传成功,去发布
  191. self.progressView?.curUploadStatus = .success
  192. self.communityPublishApi()
  193. } else {
  194. // 上传视频の返回
  195. self.videoCoverImageUploadAuth()
  196. }
  197. }
  198. })
  199. default:
  200. break
  201. }
  202. }
  203. }
  204. // MARK: - 发布流程
  205. extension PublishUploadManager {
  206. // 设置发布上传参数
  207. func setPublishParas(_ selTopicIdArr: Array<String>, _ pubTitle: String, _ pubContent: String, _ locationStr: String) {
  208. self.selTopicIdArr = selTopicIdArr
  209. self.pubTitle = pubTitle
  210. self.pubContent = pubContent
  211. self.locationStr = locationStr
  212. }
  213. // 发布Api
  214. private func communityPublishApi() {
  215. var typeStr: String = ""
  216. if mediaType == .image {
  217. typeStr = "image"
  218. } else {
  219. typeStr = "video"
  220. }
  221. let topicJsonStr = JSON(selTopicIdArr).description
  222. let imgsJsonStr = JSON(imageUrlArray).description
  223. NXLLog("----mediaType == \(typeStr)\n----pubTitle = \(pubTitle)\n----topicJsonStr == \(topicJsonStr)\n----imgsJsonStr == \(imgsJsonStr)")
  224. SwiftMoyaNetWorkServiceCommunity.shared().communityPublishApi(type: typeStr, img: majorImageUrl ?? "", topic_ids: topicJsonStr, video: paraVideo, title: pubTitle, content: pubContent, location: locationStr, imgs: imgsJsonStr) {
  225. [weak self] (communityPublishModel) -> (Void) in
  226. let communityPublishModel = communityPublishModel as? CommunityPublishModel
  227. self?.publishSuccessAction(communityPublishModel!)
  228. }
  229. }
  230. private func publishSuccessAction(_ pubModel: CommunityPublishModel) {
  231. var typeStr: String = ""
  232. if mediaType == .image {
  233. typeStr = "image"
  234. } else {
  235. typeStr = "video"
  236. }
  237. VirusViewModel.shared.publishVirueRecordAddApi(postId: pubModel.postId, postType: typeStr, title: pubTitle, content: pubContent, postCover: majorImageUrl)
  238. // 发布成功
  239. isPublishFinished = true
  240. progressView?.hide()
  241. progressView?.attachedView?.isHidden = true
  242. // progressView?.curUploadStatus = UploadStatus.failure
  243. }
  244. }