123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- //
- // 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 imageAssetUrlArr: Array<String>?
-
- // 发布参数
- 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 isPublishFinished: Bool = true
- // 图片上传时当前循环次数
- var curTimes: Int = 0
- // 是否正在上传视频封面
- var isUploadingVideoCover: Bool = false
-
- var uploadManager: AlivcShortVideoUploadManager?
-
- var progressView: PublishUploadProgressView?
- }
- // MARK: - 视频上传流程
- extension PublishUploadManager {
-
- // 设置视频上传参数
- func setVideoPath(_ mediaType: PublishMediaType, _ videoPath: String, _ coverImagePath: String, _ videoImage: UIImage) {
- self.mediaType = mediaType
- self.videoPath = videoPath
- self.coverImagePath = coverImagePath
- self.videoImage = videoImage
- // 新发布时还原管理类内部的变量
- self.isUploadingVideoCover = false
- }
-
- // 获取上传地址和凭证(视频)
- func communityVideoUploadAuthApi() {
- isPublishFinished = false
- 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 videoCoverImageUploadAuth() {
- SwiftMoyaNetWorkServiceCommunity.shared().communityImageUploadAuth {
- [weak self] (communityImageAuthModel) -> (Void) in
- let communityImageAuthModel = communityImageAuthModel as? CommunityImageAuthModel
-
- let imgWidth = Int(self?.videoImage!.size.width ?? 0)
- let imgHeight = Int(self?.videoImage!.size.height ?? 0)
- let urlStr = String(format: "%@?%ld_%ld", communityImageAuthModel?.imageURL ?? "", imgWidth, imgHeight)
- // 设置主图
- self?.majorImageUrl = urlStr
-
- self?.uploadVideoCoverImage(communityImageAuthModel!)
- }
- }
-
- // 上传视频封面图
- func uploadVideoCoverImage(_ authModel: CommunityImageAuthModel) {
- self.isUploadingVideoCover = true
-
- uploadManager = AlivcShortVideoUploadManager.shared()
- uploadManager?.setCoverImagePath(coverImagePath, videoInfo: nil, videoPath: "")
- uploadManager?.managerDelegate = self
- uploadManager?.uploadAddress = authModel.uploadAddress
- uploadManager?.uploadAuth = authModel.uploadAuth
- uploadManager?.startUpload()
- }
-
- }
- // MARK: - 图片上传流程
- extension PublishUploadManager {
-
- // 设置图片上传参数
- func setImagesPath(_ imageArr: Array<UIImage>, _ imageUrlArr: Array<String>) {
- self.imageArr = imageArr
- self.imageAssetUrlArr = imageUrlArr
- // 新发布时置空管理类内部的imageUrl数组及循环次数
- self.imageUrlArray = []
- self.curTimes = 0
- }
-
- // 获取上传地址和凭证(图片)
- func communityImageUploadAuth(totalTimes: Int) {
- isPublishFinished = false
-
- SwiftMoyaNetWorkServiceCommunity.shared().communityImageUploadAuth {
- [weak self] (communityImageAuthModel) -> (Void) in
- let communityImageAuthModel = communityImageAuthModel as? CommunityImageAuthModel
-
- let curImage: UIImage = self?.imageArr![totalTimes] ?? UIImage()
- let imgWidth = Int(curImage.size.width)
- let imgHeight = Int(curImage.size.height)
- let urlStr = String(format: "%@?%ld_%ld", communityImageAuthModel?.imageURL ?? "", imgWidth, imgHeight)
- self?.imageUrlArray.append(urlStr)
-
- self?.startUploadImage(communityImageAuthModel!, totalTimes)
- }
- }
-
- // 上传图片
- func startUploadImage(_ authModel: CommunityImageAuthModel, _ totalTimes: Int) {
- self.curTimes = totalTimes
-
- uploadManager = AlivcShortVideoUploadManager.shared()
- uploadManager?.setCoverImagePath(self.imageAssetUrlArr?[totalTimes], videoInfo: nil, videoPath: "")
- uploadManager?.managerDelegate = self
- uploadManager?.uploadAddress = authModel.uploadAddress
- uploadManager?.uploadAuth = authModel.uploadAuth
- uploadManager?.startUpload()
- }
-
- }
- // MARK: - 上传过程监听
- extension PublishUploadManager: AlivcShortVideoUploadManagerDelegate {
-
- // 上传进度回调
- func uploadManager(_ manager: AlivcShortVideoUploadManager!, updateProgress progress: CGFloat) {
- NXLLog("-------------------上传进度回调 - progress == \(progress)")
- DispatchQueue.main.async(execute: {
- if self.mediaType == .image {
- NXLLog("\n--------\(self.curTimes) uploadProgress == \(progress)")
- } else {
- self.progressView?.uploadProgress = Float(progress)
- }
- })
- }
-
- // 上传状态回调
- func uploadManager(_ manager: AlivcShortVideoUploadManager!, uploadStatusChangedTo newStatus: AlivcUploadStatus) {
- switch newStatus {
- case AlivcUploadStatus.failure:
- DispatchQueue.main.async(execute: {
- SwiftProgressHUD.shared().showText("上传失败!")
- self.isPublishFinished = true
- self.progressView?.curUploadStatus = .failure
- })
-
- case AlivcUploadStatus.success:
- DispatchQueue.main.async(execute: {
- if self.mediaType == .image {
- // 图片上传流程
- self.curTimes += 1
-
- // 更新进度
- NXLLog("----uploadProgress == \(Float(self.curTimes)/Float(self.imageArr!.count))")
- self.progressView?.uploadProgress = Float(self.curTimes/self.imageArr!.count)
-
- if self.curTimes < self.imageArr?.count ?? 0 {
- // 继续上传
- self.communityImageUploadAuth(totalTimes: self.curTimes)
- } else {
- // 图片上传成功,去发布
- // 设置主图
- self.majorImageUrl = self.imageUrlArray[0]
- self.progressView?.curUploadStatus = .success
- self.communityPublishApi()
- }
-
- } else {
- // 视频上传流程
- if self.isUploadingVideoCover {
- // 上传封面图の返回
- self.isUploadingVideoCover = false
-
- // 视频上传成功,去发布
- self.progressView?.curUploadStatus = .success
- self.communityPublishApi()
-
- } else {
- // 上传视频の返回
- self.videoCoverImageUploadAuth()
- }
- }
- })
-
- default:
- break
- }
- }
- }
- // 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
- private func communityPublishApi() {
-
- var typeStr: String = ""
- if mediaType == .image {
- typeStr = "image"
- } else {
- typeStr = "video"
- }
-
- let topicJsonStr = JSON(selTopicIdArr).description
- let imgsJsonStr = JSON(imageUrlArray).description
-
- NXLLog("----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!)
- }
- }
-
- private 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)
- // 发布成功
- isPublishFinished = true
- progressView?.hide()
- progressView?.attachedView?.isHidden = true
-
- // progressView?.curUploadStatus = UploadStatus.failure
- }
- }
|