123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- //
- // PublishEditController.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/6/17.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 编辑发布Vc
- import UIKit
- import RxSwift
- import SwiftyJSON
- import Photos
- @objc(PublishMediaType)
- enum PublishMediaType: Int {
- case image = 0
- case video = 1
- }
- class PublishEditController: BaseViewController {
-
- @objc var mediaType: PublishMediaType = .image
- @objc var videoPath: String = ""
- @objc var coverImagePath: String = ""
- @objc var videoSize: CGSize = CGSize()
- @objc var videoImage: UIImage? {
- didSet {
- imageArr = [videoImage] as? Array<UIImage>
- }
- }
-
- var imgCount: Int = 0
- var majorImageUrl: String?
- var imageUrlArray: Array<String> = []
- var imageArr: Array<UIImage>? {
- didSet {
- imgCount = self.imageArr?.count ?? 0
- }
- }
-
- // 选中的话题
- var selTopicModelArr: Array<CommunityTopicDataModel> = []
- // 已选话题id
- var selTopicIdArr: Array<String> = []
- // 已选话题name
- var selTopicName: String = ""
- // 标题
- var pubTitle: String = ""
- // 内容
- var pubContent: String = ""
- // 位置
- var locationStr: String = ""
- // 视频id,当type为video时必填
- var paraVideo: String = ""
-
- var uploadManager: AlivcShortVideoUploadManager?
- var vUploadFinished: Bool?
-
- // MARK: 控制器生命周期
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupData()
-
- // imageUrlArray每次进入页面需置空
- imageUrlArray = []
- rightButton.isEnabled = false
- rightButton.backgroundColor = kd8d8d8Color
-
- if mediaType == .video {
- subLabel.text = "视频正在上传中(0/1)..."
- communityVideoUploadAuthApi()
- print("----上传视频")
- }
- if mediaType == .image {
- subLabel.text = "图片正在上传中(0/\(imageArr?.count ?? 0))..."
- uploadAllImages(totalTimes: 0)
- }
- }
-
- override func viewWillAppear(_ animated: Bool) {
-
- // let serialQueue = DispatchQueue(label: "serial_queue")
- // serialQueue.async {
- // print("----startUpLoad\n----\(Thread.current)")
- // self.uploadAllImages(totalTimes: 0)
- // }
-
- // DispatchQueue.main.async {
- // print("----startUpLoad\n----\(Thread.current)")
- // self.uploadAllImages(totalTimes: 0)
- // }
- }
-
- override func setupViews() {
- self.view.backgroundColor = kffffffColor
-
- navigationBar.title = ""
- navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_black")!)
- navigationBar.onClickLeftButton = {
- [weak self] in
- AlertSheetView.alert(title: "是否退出内容发布?退出后你所编辑的内容无法恢复!", cancelTitle: "取消", sureTitle: "确认", cancelBlock: { (popupView, index, string) in
-
- }, confirmBlock: {
- (popupView, index, string) in
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DismissFromPublishEditVc"), object: nil)
- })
- }
-
- view.addSubview(rightButton)
- rightButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight+8)
- make.right.equalTo(-14)
- make.width.equalTo(64)
- make.height.equalTo(26)
- }
-
- view.addSubview(progressBackView)
- progressBackView.addSubview(progressView)
- progressBackView.addSubview(subLabel)
-
- progressBackView.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kNavBarTotalHeight)
- make.left.right.equalToSuperview()
- make.height.equalTo(38)
- }
- progressView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.height.equalTo(3)
- }
- subLabel.snp.makeConstraints { (make) in
- make.left.equalTo(14)
- make.centerY.equalToSuperview()
- make.height.equalTo(20)
- }
-
- view.addSubview(tableView)
- tableView.snp.makeConstraints { (make) in
- make.top.equalTo(progressBackView.snp_bottom)
- make.left.right.bottom.equalToSuperview()
- }
- }
-
- override func setupData() {
-
- }
-
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
- tableView.separatorStyle = .none
- tableView.backgroundColor = kffffffColor
- tableView.dataSource = self
- tableView.delegate = self
- tableView.estimatedRowHeight = 0.000001
- tableView.estimatedSectionFooterHeight = 0.000001
- tableView.estimatedSectionHeaderHeight = 0.000001
- return tableView
- }()
-
- private lazy var rightButton: UIButton = {
- let rightButton = UIButton(type: UIButton.ButtonType.custom)
- rightButton.setTitle("发布", for: UIControl.State.normal)
- rightButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
- rightButton.titleLabel?.font = kMediumFont13
- rightButton.cornerRadius = 16
- rightButton.masksToBounds = true
- rightButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- self?.communityPublishApi()
- }).disposed(by: disposeBag)
- return rightButton
- }()
-
- private lazy var progressBackView: UIView = {
- let progressBackView = UIView()
- progressBackView.backgroundColor = kffffffColor
- return progressBackView
- }()
-
- private lazy var progressView: UIProgressView = {
- let progressView = UIProgressView(progressViewStyle: .default)
- progressView.setProgress(0, animated: false)
- progressView.progressTintColor = k62CC74Color //进度颜色
- progressView.trackTintColor = kd8d8d8Color //剩余进度颜色
- return progressView
- }()
-
- private lazy var subLabel: UILabel = {
- let subLabel = UILabel()
- subLabel.textColor = k333333Color
- subLabel.font = kRegularFont14
- subLabel.textAlignment = .left
- return subLabel
- }()
-
- }
- // MARK: - tableView dataSource && delegate
- extension PublishEditController : UITableViewDelegate, UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 5
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- switch indexPath.row {
- case 0:
- let picCell = PublishEditAddPicCell.cellWith(tableView: tableView, indexPath: indexPath)
- picCell.pubImageArr = imageArr
- if mediaType == .video {
- picCell.maxImageCount = 1
- }
- picCell.reloadData()
- picCell.choosePicBlock = {
- [weak self] in
- print("\n----选取图片")
- }
- picCell.delPicTransBlock = {
- [weak self] (picIdx) in
- self?.imageArr?.remove(at: picIdx!)
- self?.imageUrlArray.remove(at: picIdx!)
- self?.tableView.reloadData()
- }
- return picCell
- case 1:
- let titleCell = PublishEditTitleCell.cellWith(tableView: tableView, indexPath: indexPath)
- titleCell.topicTextClosure = {
- [weak self] (text) in
- self?.pubTitle = text
- }
- return titleCell
- case 2:
- let desCell = PublishEditDescribeCell.cellWith(tableView: tableView, indexPath: indexPath)
- desCell.commentTextViewClosure = {
- [weak self] (text) in
- self?.pubContent = text
- }
- return desCell
- case 3:
- let topicCell = PublishEditAddTopicCell.cellWith(tableView: tableView, indexPath: indexPath)
- topicCell.isTopicsNull = selTopicIdArr.count == 0 ? true : false
- topicCell.selTopicName = selTopicName
- return topicCell
- case 4:
- let addrCell = PublishEditAddAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
- addrCell.location = locationStr
- return addrCell
- default:
- return UITableViewCell()
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- switch indexPath.row {
- case 3:
- let vc = PublishAddTopicController()
- vc.selTopicModelArr = self.selTopicModelArr
- vc.selTopicsClosure = {
- [weak self] (topicMdlArr) in
- self?.selTopicModelArr = topicMdlArr
- self?.selTopicName = ""
- self?.selTopicIdArr.removeAll()
-
- for (index,topicModel) in (topicMdlArr.enumerated()) {
- let strTopicId: String = String(format: "%d", topicModel.id ?? 0)
- self?.selTopicIdArr.append(strTopicId)
-
- if index == 0 {
- self?.selTopicName.append(topicModel.name!)
- } else {
- self?.selTopicName.append(", \(topicModel.name!)")
- }
- }
- self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- }
- self.navigationController?.pushViewController(vc, animated: true)
- case 4:
- let vc = PublishAddAddressController()
- vc.selectLocationClosure = {
- [weak self] (address) in
- self?.locationStr = address
- self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- }
- self.navigationController?.pushViewController(vc, animated: true)
- default:
- break
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- switch indexPath.row {
- case 0:
- return 130
- case 1,3,4:
- return 50
- case 2:
- return 190
- default:
- return UITableView.automaticDimension
- }
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 0
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- return nil
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.000001
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return nil
- }
- }
- // MARK: - 逻辑处理
- extension PublishEditController {
-
- 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)")
- self?.progressView.setProgress(Float(curTimes/(self?.imageArr!.count)!), animated: true)
- }
- if curTimes == self?.imageArr!.count {
- self?.subLabel.text = "图片上传成功!"
- } else {
- self?.subLabel.text = "图片正在上传中(\(curTimes)/\(self?.imageArr?.count ?? 0))..."
- }
-
- if curTimes < self?.imageArr?.count ?? 0 {
- self?.uploadAllImages(totalTimes: curTimes)
- } else {
- self?.rightButton.isEnabled = true
- self?.rightButton.backgroundColor = k62CC74Color
- }
- }
- }
-
- // 发布Api
- func communityPublishApi() {
-
- if pubTitle.count > 20 {
- SwiftProgressHUD.shared().showText("标题最多输入20个字符")
- return
- }
-
- if pubContent.count == 0 {
- SwiftProgressHUD.shared().showText("还没输入描述内容")
- return
- }
-
- if selTopicIdArr.count == 0 {
- SwiftProgressHUD.shared().showText("还没选择话题哟")
- return
- }
-
- 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)
-
-
- let sucVc = PublishSuccessController()
- sucVc.postId = pubModel.postId
- sucVc.bean = pubModel.bean
- sucVc.H5UrlStr = pubModel.h5url
- sucVc.imgUrl = majorImageUrl
- sucVc.titleStr = pubTitle
- sucVc.contentStr = pubContent
- // 暂给已选首个
- sucVc.topicStr = selTopicModelArr[0].name ?? ""
-
- let userMdl = UserModel.shared().getModel()
- sucVc.nameStr = userMdl?.username
- sucVc.avatarStr = userMdl?.avatarurl
- sucVc.uid = userMdl?.uid
- sucVc.type = typeStr
-
- self.navigationController?.pushViewController(sucVc, animated: true)
- }
-
- }
- // MARK: - 视频相关处理
- extension PublishEditController: AlivcShortVideoUploadManagerDelegate {
-
- // 获取上传地址和凭证
- 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: {
- self.progressView.progress = Float(progress)
- })
- }
-
- // 上传状态回调
- func uploadManager(_ manager: AlivcShortVideoUploadManager!, uploadStatusChangedTo newStatus: AlivcUploadStatus) {
- switch newStatus {
- case AlivcUploadStatus.failure:
- DispatchQueue.main.async(execute: {
- self.subLabel.text = "上传失败!"
- 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
-
- self?.vUploadFinished = true
- self?.subLabel.text = "上传成功!"
- self?.rightButton.isEnabled = true
- self?.rightButton.backgroundColor = k62CC74Color
- }
- }
-
- }
|