PublishUploadManager.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // 发布参数
  28. var selTopicIdArr: Array<String> = []
  29. var pubTitle: String = ""
  30. var pubContent: String = ""
  31. var locationStr: String = ""
  32. // 视频id,当type为video时必填
  33. var paraVideo: String = ""
  34. // 主图Url
  35. var majorImageUrl: String?
  36. // 发布图片のUrlArray
  37. var imageUrlArray: Array<String> = []
  38. var uploadManager: AlivcShortVideoUploadManager?
  39. var progressView: PublishUploadProgressView?
  40. // 记录当前是否发布完成
  41. var isPublishFinished: Bool = true
  42. }
  43. // MARK: - 视频上传流程
  44. extension PublishUploadManager: AlivcShortVideoUploadManagerDelegate {
  45. // 设置视频上传参数
  46. func setVideoPath(_ mediaType: PublishMediaType, _ videoPath: String, _ coverImagePath: String, _ videoImage: UIImage) {
  47. self.mediaType = mediaType
  48. self.videoPath = videoPath
  49. self.coverImagePath = coverImagePath
  50. self.videoImage = videoImage
  51. }
  52. // 获取上传地址和凭证
  53. func communityVideoUploadAuthApi() {
  54. isPublishFinished = false
  55. var vTitle: String = ""
  56. if videoPath.count > 20 {
  57. vTitle = String(videoPath.suffix(20))
  58. } else {
  59. vTitle = videoPath
  60. }
  61. SwiftMoyaNetWorkServiceCommunity.shared().communityVideoUploadAuthApi(title: vTitle, filename: vTitle, cover_url: coverImagePath) {
  62. [weak self] (communityVideoAuthModel) -> (Void) in
  63. let communityVideoAuthModel = communityVideoAuthModel as? CommunityVideoAuthModel
  64. self?.paraVideo = communityVideoAuthModel?.videoId ?? ""
  65. self?.startUploadVideo(communityVideoAuthModel!)
  66. }
  67. }
  68. // 上传视频
  69. func startUploadVideo(_ authModel: CommunityVideoAuthModel) {
  70. let info = AliyunUploadSVideoInfo()
  71. uploadManager = AlivcShortVideoUploadManager.shared()
  72. uploadManager?.setCoverImagePath(coverImagePath, videoInfo: info, videoPath: videoPath)
  73. uploadManager?.managerDelegate = self
  74. uploadManager?.uploadAddress = authModel.uploadAddress
  75. uploadManager?.videoId = authModel.videoId
  76. uploadManager?.uploadAuth = authModel.uploadAuth
  77. uploadManager?.startUpload()
  78. }
  79. // 上传进度回调
  80. func uploadManager(_ manager: AlivcShortVideoUploadManager!, updateProgress progress: CGFloat) {
  81. DispatchQueue.main.async(execute: {
  82. print("\n--------uploadProgress == \(progress)")
  83. // self.progressView.progress = Float(progress)
  84. self.progressView?.uploadProgress = Float(progress)
  85. })
  86. }
  87. // 上传状态回调
  88. func uploadManager(_ manager: AlivcShortVideoUploadManager!, uploadStatusChangedTo newStatus: AlivcUploadStatus) {
  89. switch newStatus {
  90. case AlivcUploadStatus.failure:
  91. DispatchQueue.main.async(execute: {
  92. SwiftProgressHUD.shared().showText("上传失败!")
  93. })
  94. case AlivcUploadStatus.success:
  95. // 上传封面图
  96. self.uploadVideoCoverImage()
  97. default:
  98. break
  99. }
  100. }
  101. // 上传视频封面图
  102. func uploadVideoCoverImage() {
  103. SwiftMoyaNetWorkServiceConfig.shared().configUploadSingleImgWithoutHudApi(imageArray: [videoImage!]) {
  104. [weak self] (imgUrl) -> (Void) in
  105. // 设置主图
  106. self?.majorImageUrl = imgUrl as? String
  107. print("------视频上传成功,去发布")
  108. self?.progressView?.curUploadStatus = .success
  109. self?.communityPublishApi()
  110. }
  111. }
  112. }
  113. // MARK: - 图片上传流程
  114. extension PublishUploadManager {
  115. // 设置图片上传参数
  116. func setImagesPath(_ imageArr: Array<UIImage>) {
  117. self.imageArr = imageArr
  118. }
  119. // 上传图片
  120. func uploadAllImages(totalTimes: Int) {
  121. isPublishFinished = false
  122. var curTimes: Int = totalTimes
  123. print("-----StartUpload,totalTimes == \(totalTimes)")
  124. SwiftMoyaNetWorkServiceConfig.shared().configUploadSingleImgWithoutHudApi(imageArray: [self.imageArr![totalTimes]]) {
  125. [weak self] (imgUrl) -> (Void) in
  126. let urlStr: String = imgUrl as! String
  127. self?.imageUrlArray.append(urlStr)
  128. if curTimes == 0 {
  129. // 设置主图
  130. self?.majorImageUrl = urlStr
  131. }
  132. print("-----FinishUpload,Times == \(curTimes)")
  133. curTimes += 1
  134. // 更新进度
  135. DispatchQueue.main.async {
  136. print("----\(Thread.current)")
  137. print("----uploadProgress == \(Float(curTimes)/Float((self?.imageArr!.count)!))")
  138. self?.progressView?.uploadProgress = Float(curTimes/(self?.imageArr!.count)!)
  139. }
  140. if curTimes < self?.imageArr?.count ?? 0 {
  141. self?.uploadAllImages(totalTimes: curTimes)
  142. } else {
  143. print("------图片上传成功,去发布")
  144. self?.progressView?.curUploadStatus = .success
  145. self?.communityPublishApi()
  146. }
  147. }
  148. }
  149. }
  150. // MARK: - 发布流程
  151. extension PublishUploadManager {
  152. // 设置发布上传参数
  153. func setPublishParas(_ selTopicIdArr: Array<String>, _ pubTitle: String, _ pubContent: String, _ locationStr: String) {
  154. self.selTopicIdArr = selTopicIdArr
  155. self.pubTitle = pubTitle
  156. self.pubContent = pubContent
  157. self.locationStr = locationStr
  158. }
  159. // 发布Api
  160. private func communityPublishApi() {
  161. var typeStr: String = ""
  162. if mediaType == .image {
  163. typeStr = "image"
  164. } else {
  165. typeStr = "video"
  166. }
  167. let topicJsonStr = JSON(selTopicIdArr).description
  168. let imgsJsonStr = JSON(imageUrlArray).description
  169. print("----mediaType == \(typeStr)\n----pubTitle = \(pubTitle)\n----topicJsonStr == \(topicJsonStr)\n----imgsJsonStr == \(imgsJsonStr)")
  170. SwiftMoyaNetWorkServiceCommunity.shared().communityPublishApi(type: typeStr, img: majorImageUrl ?? "", topic_ids: topicJsonStr, video: paraVideo, title: pubTitle, content: pubContent, location: locationStr, imgs: imgsJsonStr) {
  171. [weak self] (communityPublishModel) -> (Void) in
  172. let communityPublishModel = communityPublishModel as? CommunityPublishModel
  173. self?.publishSuccessAction(communityPublishModel!)
  174. }
  175. }
  176. private func publishSuccessAction(_ pubModel: CommunityPublishModel) {
  177. var typeStr: String = ""
  178. if mediaType == .image {
  179. typeStr = "image"
  180. } else {
  181. typeStr = "video"
  182. }
  183. VirusViewModel.shared.publishVirueRecordAddApi(postId: pubModel.postId, postType: typeStr, title: pubTitle, content: pubContent, postCover: majorImageUrl)
  184. print("\n----------发布成功!!!!!")
  185. isPublishFinished = true
  186. // progressView?.hide()
  187. progressView?.curUploadStatus = UploadStatus.failure
  188. }
  189. }