|
@@ -0,0 +1,219 @@
|
|
|
+//
|
|
|
+// PublishUploadManager.swift
|
|
|
+// RainbowPlanet
|
|
|
+//
|
|
|
+// Created by Christopher on 2019/7/21.
|
|
|
+// Copyright © 2019 RainbowPlanet. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import UIKit
|
|
|
+import SwiftyJSON
|
|
|
+
|
|
|
+class PublishUploadManager: NSObject {
|
|
|
+
|
|
|
+ private static let _sharedInstance = PublishUploadManager()
|
|
|
+
|
|
|
+ private override init() {} // 私有化init方法
|
|
|
+
|
|
|
+ class func shared() -> PublishUploadManager {
|
|
|
+ return _sharedInstance
|
|
|
+ }
|
|
|
+
|
|
|
+ // 视频上传参数
|
|
|
+ var mediaType: PublishMediaType = .image
|
|
|
+ var videoPath: String = ""
|
|
|
+ var coverImagePath: String = ""
|
|
|
+ var videoImage: UIImage? {
|
|
|
+ didSet {
|
|
|
+ imageArr = [videoImage] as? Array<UIImage>
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 图片上传参数
|
|
|
+ var imageArr: Array<UIImage>?
|
|
|
+
|
|
|
+ // 发布参数
|
|
|
+ var selTopicIdArr: Array<String> = []
|
|
|
+ var pubTitle: String = ""
|
|
|
+ var pubContent: String = ""
|
|
|
+ var locationStr: String = ""
|
|
|
+
|
|
|
+ // 视频id,当type为video时必填
|
|
|
+ var paraVideo: String = ""
|
|
|
+ // 主图Url
|
|
|
+ var majorImageUrl: String?
|
|
|
+ // 发布图片のUrlArray
|
|
|
+ var imageUrlArray: Array<String> = []
|
|
|
+
|
|
|
+ var uploadManager: AlivcShortVideoUploadManager?
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 视频上传流程
|
|
|
+extension PublishUploadManager: AlivcShortVideoUploadManagerDelegate {
|
|
|
+
|
|
|
+ // 设置视频上传参数
|
|
|
+ func setVideoPath(_ mediaType: PublishMediaType, _ videoPath: String, _ coverImagePath: String, _ videoImage: UIImage) {
|
|
|
+ self.mediaType = mediaType
|
|
|
+ self.videoPath = videoPath
|
|
|
+ self.coverImagePath = coverImagePath
|
|
|
+ self.videoImage = videoImage
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取上传地址和凭证
|
|
|
+ func communityVideoUploadAuthApi() {
|
|
|
+
|
|
|
+ var vTitle: String = ""
|
|
|
+ if videoPath.count > 20 {
|
|
|
+ vTitle = String(videoPath.suffix(20))
|
|
|
+ } else {
|
|
|
+ vTitle = videoPath
|
|
|
+ }
|
|
|
+ SwiftMoyaNetWorkServiceCommunity.shared().communityVideoUploadAuthApi(title: vTitle, filename: vTitle, cover_url: coverImagePath) {
|
|
|
+ [weak self] (communityVideoAuthModel) -> (Void) in
|
|
|
+ let communityVideoAuthModel = communityVideoAuthModel as? CommunityVideoAuthModel
|
|
|
+ self?.paraVideo = communityVideoAuthModel?.videoId ?? ""
|
|
|
+ self?.startUploadVideo(communityVideoAuthModel!)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传视频
|
|
|
+ func startUploadVideo(_ authModel: CommunityVideoAuthModel) {
|
|
|
+ let info = AliyunUploadSVideoInfo()
|
|
|
+ uploadManager = AlivcShortVideoUploadManager.shared()
|
|
|
+ uploadManager?.setCoverImagePath(coverImagePath, videoInfo: info, videoPath: videoPath)
|
|
|
+ uploadManager?.managerDelegate = self
|
|
|
+ uploadManager?.uploadAddress = authModel.uploadAddress
|
|
|
+ uploadManager?.videoId = authModel.videoId
|
|
|
+ uploadManager?.uploadAuth = authModel.uploadAuth
|
|
|
+ uploadManager?.startUpload()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传进度回调
|
|
|
+ func uploadManager(_ manager: AlivcShortVideoUploadManager!, updateProgress progress: CGFloat) {
|
|
|
+ DispatchQueue.main.async(execute: {
|
|
|
+ print("\n--------uploadProgress == \(progress)")
|
|
|
+// self.progressView.progress = Float(progress)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传状态回调
|
|
|
+ func uploadManager(_ manager: AlivcShortVideoUploadManager!, uploadStatusChangedTo newStatus: AlivcUploadStatus) {
|
|
|
+ switch newStatus {
|
|
|
+ case AlivcUploadStatus.failure:
|
|
|
+ DispatchQueue.main.async(execute: {
|
|
|
+ SwiftProgressHUD.shared().showText("上传失败!")
|
|
|
+ })
|
|
|
+
|
|
|
+ case AlivcUploadStatus.success:
|
|
|
+ // 上传封面图
|
|
|
+ self.uploadVideoCoverImage()
|
|
|
+
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传视频封面图
|
|
|
+ func uploadVideoCoverImage() {
|
|
|
+ SwiftMoyaNetWorkServiceConfig.shared().configUploadSingleImgWithoutHudApi(imageArray: [videoImage!]) {
|
|
|
+ [weak self] (imgUrl) -> (Void) in
|
|
|
+ // 设置主图
|
|
|
+ self?.majorImageUrl = imgUrl as? String
|
|
|
+
|
|
|
+ print("------视频上传成功,去发布")
|
|
|
+ self?.communityPublishApi()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 图片上传流程
|
|
|
+extension PublishUploadManager {
|
|
|
+
|
|
|
+ // 设置图片上传参数
|
|
|
+ func setImagesPath(_ imageArr: Array<UIImage>) {
|
|
|
+ self.imageArr = imageArr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传图片
|
|
|
+ func uploadAllImages(totalTimes: Int) {
|
|
|
+
|
|
|
+ var curTimes: Int = totalTimes
|
|
|
+
|
|
|
+ print("-----StartUpload,totalTimes == \(totalTimes)")
|
|
|
+ SwiftMoyaNetWorkServiceConfig.shared().configUploadSingleImgWithoutHudApi(imageArray: [self.imageArr![totalTimes]]) {
|
|
|
+ [weak self] (imgUrl) -> (Void) in
|
|
|
+ let urlStr: String = imgUrl as! String
|
|
|
+ self?.imageUrlArray.append(urlStr)
|
|
|
+ if curTimes == 0 {
|
|
|
+ // 设置主图
|
|
|
+ self?.majorImageUrl = urlStr
|
|
|
+ }
|
|
|
+
|
|
|
+ print("-----FinishUpload,Times == \(curTimes)")
|
|
|
+ curTimes += 1
|
|
|
+ // 更新进度
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ print("----\(Thread.current)")
|
|
|
+ print("----uploadImgProgress == \(curTimes/(self?.imageArr!.count)!)")
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if curTimes < self?.imageArr?.count ?? 0 {
|
|
|
+ self?.uploadAllImages(totalTimes: curTimes)
|
|
|
+ } else {
|
|
|
+ // FIXME: 图片上传成功,去发布
|
|
|
+ print("------图片上传成功,去发布")
|
|
|
+ self?.communityPublishApi()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 发布流程
|
|
|
+extension PublishUploadManager {
|
|
|
+
|
|
|
+ // 设置发布上传参数
|
|
|
+ func setPublishParas(_ selTopicIdArr: Array<String>, _ pubTitle: String, _ pubContent: String, _ locationStr: String) {
|
|
|
+ self.selTopicIdArr = selTopicIdArr
|
|
|
+ self.pubTitle = pubTitle
|
|
|
+ self.pubContent = pubContent
|
|
|
+ self.locationStr = locationStr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发布Api
|
|
|
+ func communityPublishApi() {
|
|
|
+
|
|
|
+ var typeStr: String = ""
|
|
|
+ if mediaType == .image {
|
|
|
+ typeStr = "image"
|
|
|
+ } else {
|
|
|
+ typeStr = "video"
|
|
|
+ }
|
|
|
+
|
|
|
+ let topicJsonStr = JSON(selTopicIdArr).description
|
|
|
+ let imgsJsonStr = JSON(imageUrlArray).description
|
|
|
+
|
|
|
+ print("----mediaType == \(typeStr)\n----pubTitle = \(pubTitle)\n----topicJsonStr == \(topicJsonStr)\n----imgsJsonStr == \(imgsJsonStr)")
|
|
|
+
|
|
|
+ SwiftMoyaNetWorkServiceCommunity.shared().communityPublishApi(type: typeStr, img: majorImageUrl ?? "", topic_ids: topicJsonStr, video: paraVideo, title: pubTitle, content: pubContent, location: locationStr, imgs: imgsJsonStr) {
|
|
|
+ [weak self] (communityPublishModel) -> (Void) in
|
|
|
+ let communityPublishModel = communityPublishModel as? CommunityPublishModel
|
|
|
+ self?.publishSuccessAction(communityPublishModel!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func publishSuccessAction(_ pubModel: CommunityPublishModel) {
|
|
|
+ var typeStr: String = ""
|
|
|
+ if mediaType == .image {
|
|
|
+ typeStr = "image"
|
|
|
+ } else {
|
|
|
+ typeStr = "video"
|
|
|
+ }
|
|
|
+
|
|
|
+ VirusViewModel.shared.publishVirueRecordAddApi(postId: pubModel.postId, postType: typeStr, title: pubTitle, content: pubContent, postCover: majorImageUrl)
|
|
|
+
|
|
|
+ print("\n----------发布成功!!!!!")
|
|
|
+ }
|
|
|
+}
|