Commit 1f2e9216 authored by Alex朱枝文's avatar Alex朱枝文

文书逻辑重新调整,以及接口调试

parent 9a508c05
...@@ -63,7 +63,7 @@ class YHFilePreviewTool: NSObject { ...@@ -63,7 +63,7 @@ class YHFilePreviewTool: NSObject {
} }
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
guard let self = self, let presentingVC = self.targetVC else { guard let self = self, self.targetVC != nil else {
printLog("Target view controller is not available") printLog("Target view controller is not available")
return return
} }
......
...@@ -28,6 +28,12 @@ class YHResignDocumentDetailViewController: YHBaseViewController { ...@@ -28,6 +28,12 @@ class YHResignDocumentDetailViewController: YHBaseViewController {
case uploadAction(_ uploadName: String, _ isUploadButtonEnable: Bool = false, _ isSureButtonEnable: Bool = false, _ topMargin: CGFloat = 16) case uploadAction(_ uploadName: String, _ isUploadButtonEnable: Bool = false, _ isSureButtonEnable: Bool = false, _ topMargin: CGFloat = 16)
} }
enum ActionType {
case upload
case reupload
case delete
}
private var datas: [[RowType]] = [] private var datas: [[RowType]] = []
private lazy var viewModel = YHResignDocumentViewModel() private lazy var viewModel = YHResignDocumentViewModel()
private var detailModel: YHResignDocumentCompleteModel? private var detailModel: YHResignDocumentCompleteModel?
...@@ -37,7 +43,10 @@ class YHResignDocumentDetailViewController: YHBaseViewController { ...@@ -37,7 +43,10 @@ class YHResignDocumentDetailViewController: YHBaseViewController {
private let docId: Int private let docId: Int
private var name: String private var name: String
private let uploadMaxCount = 10 private let uploadMaxCount = 10
private var isEditUploadFiles: Bool = false // 跟踪编辑状态 上传 预览 删除等
private var isEditUploadFilesDidDisappear: Bool = false // 跟踪编辑状态 上传 预览 删除等时是否页面消失
init(_ docId: Int, _ name: String = "") { init(_ docId: Int, _ name: String = "") {
self.docId = docId self.docId = docId
self.name = name self.name = name
...@@ -121,7 +130,20 @@ class YHResignDocumentDetailViewController: YHBaseViewController { ...@@ -121,7 +130,20 @@ class YHResignDocumentDetailViewController: YHBaseViewController {
super.viewWillAppear(animated) super.viewWillAppear(animated)
let needLoading = isNeedShowLoading let needLoading = isNeedShowLoading
isNeedShowLoading = false isNeedShowLoading = false
requestData(isNeedLoading: needLoading) if isEditUploadFilesDidDisappear {
isEditUploadFiles = false
isEditUploadFilesDidDisappear = false
} else {
isEditUploadFiles = false
requestData(isNeedLoading: needLoading)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if isEditUploadFiles {
isEditUploadFilesDidDisappear = true
}
} }
deinit { deinit {
...@@ -278,15 +300,32 @@ extension YHResignDocumentDetailViewController: UITableViewDelegate, UITableView ...@@ -278,15 +300,32 @@ extension YHResignDocumentDetailViewController: UITableViewDelegate, UITableView
cell.setupCellInfo(title, cellType: cellType) cell.setupCellInfo(title, cellType: cellType)
cell.editButtonClickEvent = { [weak self] in cell.editButtonClickEvent = { [weak self] in
guard let self = self else { return } guard let self = self else { return }
self.isEditUploadFiles = true
let items: [YHCertificateEditItem] = editTypes.map { let items: [YHCertificateEditItem] = editTypes.map {
YHCertificateEditItem(type: $0, title: $0.editName()) YHCertificateEditItem(type: $0, title: $0.editName())
} }
YHCertificateEditSheetView.sheetView(items:items) { [weak self] editType in YHCertificateEditSheetView.sheetView(items:items) { [weak self] editType in
guard let self = self else { return } guard let self = self else { return }
if editType == .reupload { if editType == .reupload {
// let sheetView = YHDocumentUploadView.sheetView()
sheetView.maxSelectCount = 1
sheetView.uploadFilesBlock = { [weak self] fileUrl in
guard let self = self else { return }
printLog(fileUrl)
self.uploadFile(fileUrl, actionType: .reupload, oldUrl: urlStr)
}
sheetView.uploadImageBlock = { [weak self] imgArr in
guard let self = self else { return }
self.uploadImages(imgArr: imgArr, actionType: .reupload, oldUrl: urlStr)
}
sheetView.show()
} else if editType == .delete { // 删除 } else if editType == .delete { // 删除
// if let index = self.detailModel?.signedDocument.firstIndex(where: { model in
model.url == urlStr
}) {
self.detailModel?.signedDocument.remove(at: index)
self.updateUploadFilesSection()
}
} else if editType == .preview { // 预览 } else if editType == .preview { // 预览
guard URL(string: urlStr) != nil else { guard URL(string: urlStr) != nil else {
return return
...@@ -314,6 +353,7 @@ extension YHResignDocumentDetailViewController: UITableViewDelegate, UITableView ...@@ -314,6 +353,7 @@ extension YHResignDocumentDetailViewController: UITableViewDelegate, UITableView
guard let detailModel = self.detailModel else { guard let detailModel = self.detailModel else {
return return
} }
self.isEditUploadFiles = true
let count = detailModel.signedDocument.count let count = detailModel.signedDocument.count
if count >= 10 { if count >= 10 {
YHHUD.flash(message: "最多上传10个图片或文件") YHHUD.flash(message: "最多上传10个图片或文件")
...@@ -325,11 +365,11 @@ extension YHResignDocumentDetailViewController: UITableViewDelegate, UITableView ...@@ -325,11 +365,11 @@ extension YHResignDocumentDetailViewController: UITableViewDelegate, UITableView
sheetView.uploadFilesBlock = { [weak self] fileUrl in sheetView.uploadFilesBlock = { [weak self] fileUrl in
guard let self = self else { return } guard let self = self else { return }
printLog(fileUrl) printLog(fileUrl)
self.uploadFile(fileUrl) self.uploadFile(fileUrl, actionType: .upload, oldUrl: nil)
} }
sheetView.uploadImageBlock = { [weak self] imgArr in sheetView.uploadImageBlock = { [weak self] imgArr in
guard let self = self else { return } guard let self = self else { return }
self.uploadImages(imgArr: imgArr) self.uploadImages(imgArr: imgArr, actionType: .upload, oldUrl: nil)
} }
sheetView.show() sheetView.show()
} }
...@@ -493,12 +533,24 @@ extension YHResignDocumentDetailViewController { ...@@ -493,12 +533,24 @@ extension YHResignDocumentDetailViewController {
} }
} }
private func getFileExtenion(_ fileUrl: String) -> String {
var extenion = ""
if let url = URL(string: fileUrl) {
extenion = "." + url.pathExtension
}
return extenion
}
private func getUploadSection(status: YHResignDocumentStatus, model: YHResignDocumentCompleteModel) -> [RowType] { private func getUploadSection(status: YHResignDocumentStatus, model: YHResignDocumentCompleteModel) -> [RowType] {
var uploadSection: [RowType] = [] var uploadSection: [RowType] = []
if status == .pendingConfirmation { if status == .pendingConfirmation || status == .awaitingSignature {
uploadSection.append(.uploadHeader("上传已签署件")) uploadSection.append(.uploadHeader("上传已签署件"))
model.signedDocument.forEach { model.signedDocument.enumerated().forEach { [weak self] index, file in
uploadSection.append(.uploadFile($0.fileName, $0.url, [.preview, .reupload, .delete, .cancel])) guard let self = self else {
return
}
let extenionName = self.getFileExtenion(file.url)
uploadSection.append(.uploadFile(self.name + "\(index + 1)" + extenionName, file.url, [.preview, .reupload, .delete, .cancel]))
} }
var name = "上传已签署件" var name = "上传已签署件"
var isUploadEnable = true var isUploadEnable = true
...@@ -514,14 +566,55 @@ extension YHResignDocumentDetailViewController { ...@@ -514,14 +566,55 @@ extension YHResignDocumentDetailViewController {
} else { } else {
if model.signedDocument.count > 0 { if model.signedDocument.count > 0 {
uploadSection.append(.uploadHeader("上传已签署件")) uploadSection.append(.uploadHeader("上传已签署件"))
model.signedDocument.forEach { model.signedDocument.enumerated().forEach { [weak self] index, file in
uploadSection.append(.uploadFile($0.fileName, $0.url, [.preview, .cancel])) guard let self = self else {
return
}
let extenionName = self.getFileExtenion(file.url)
uploadSection.append(.uploadFile(self.name + "\(index + 1)" + extenionName, file.url, [.preview, .cancel]))
} }
} }
} }
return uploadSection return uploadSection
} }
private func updateUploadFilesSection() {
guard let model = detailModel else {
return
}
var uploadSection: [RowType] = []
uploadSection.append(.uploadHeader("上传已签署件"))
model.signedDocument.enumerated().forEach { [weak self] index, file in
guard let self = self else { return }
let extenionName = self.getFileExtenion(file.url)
uploadSection.append(.uploadFile(self.name + "\(index + 1)" + extenionName, file.url, [.preview, .reupload, .delete, .cancel]))
}
var name = "上传已签署件"
var isUploadEnable = true
let totalCount = model.signedDocument.count
let isSureButtonEnable = totalCount > 0
if totalCount >= uploadMaxCount {
name = "继续上传"
isUploadEnable = false
} else if totalCount > 0 {
name = "继续上传"
isUploadEnable = true
}
uploadSection.append(.uploadAction(name, isUploadEnable, isSureButtonEnable, isSureButtonEnable ? 4 : 16))
if let index = datas.firstIndex(where: { sectionArr in
return sectionArr.contains { rowType in
if case.uploadHeader = rowType {
return true
}
return false
}
}) {
datas[index] = uploadSection
}
tableView.reloadData()
}
private func getStatusButtonsConfig(docType: YHResignDocumentType, status: YHResignDocumentStatus, model: YHResignDocumentCompleteModel) -> YHResignDocumentStatusCell.ButtonsConfig { private func getStatusButtonsConfig(docType: YHResignDocumentType, status: YHResignDocumentStatus, model: YHResignDocumentCompleteModel) -> YHResignDocumentStatusCell.ButtonsConfig {
var config = YHResignDocumentStatusCell.ButtonsConfig() var config = YHResignDocumentStatusCell.ButtonsConfig()
var actions: [(() -> Void)] = [] var actions: [(() -> Void)] = []
...@@ -612,7 +705,7 @@ extension YHResignDocumentDetailViewController { ...@@ -612,7 +705,7 @@ extension YHResignDocumentDetailViewController {
} }
extension YHResignDocumentDetailViewController { extension YHResignDocumentDetailViewController {
private func uploadFile(_ fileUrl: URL) { private func uploadFile(_ fileUrl: URL, actionType: ActionType, oldUrl: String?) {
if let fileData = try? Data(contentsOf: fileUrl) { if let fileData = try? Data(contentsOf: fileUrl) {
// 将文件数据上传到服务器 // 将文件数据上传到服务器
let size = String(format: "%.2fM", Double(fileData.count) / (1024.0 * 1024.0)) let size = String(format: "%.2fM", Double(fileData.count) / (1024.0 * 1024.0))
...@@ -624,22 +717,21 @@ extension YHResignDocumentDetailViewController { ...@@ -624,22 +717,21 @@ extension YHResignDocumentDetailViewController {
YHHUD.hide() YHHUD.hide()
// 再调用业务接口 // 再调用业务接口
if let successUrl = successUrl, !successUrl.isEmpty { if let successUrl = successUrl, !successUrl.isEmpty {
let uploadModel = YHResignDocumentUploadSignedDocument() let uploadModel = YHResignSignedDocumentModel()
uploadModel.name = fileUrl.lastPathComponent
uploadModel.url = successUrl uploadModel.url = successUrl
self.viewModel.uploadSignedDoc(self.docId, type: .upload, [uploadModel]) { [weak self] success, error in let extenionName = self.getFileExtenion(successUrl)
guard let self = self else { return } uploadModel.fileName = self.name + "\((self.detailModel?.signedDocument.count ?? 0) + 1)" + extenionName
if success { if actionType == .upload {
YHHUD.flash(message: "上传成功") self.detailModel?.signedDocument.append(uploadModel)
} else { } else if actionType == .reupload {
var msg = "上传失败" if let index = self.detailModel?.signedDocument.firstIndex(where: {
if let errorMsg = error?.errorMsg, !errorMsg.isEmpty { $0.url == oldUrl && oldUrl != nil
msg = errorMsg }) {
} self.detailModel?.signedDocument[index] = uploadModel
YHHUD.flash(message: msg)
} }
self.requestData(isNeedLoading: true)
} }
self.updateUploadFilesSection()
return return
} }
var msg = "上传文件失败" var msg = "上传文件失败"
...@@ -653,19 +745,18 @@ extension YHResignDocumentDetailViewController { ...@@ -653,19 +745,18 @@ extension YHResignDocumentDetailViewController {
} }
} }
private func uploadImages(imgArr: [YHSelectImageItem]) { private func uploadImages(imgArr: [YHSelectImageItem], actionType: ActionType, oldUrl: String?) {
let group = DispatchGroup() let group = DispatchGroup()
var successArr: [YHResignDocumentUploadSignedDocument] = [] var successArr: [YHResignSignedDocumentModel] = []
var failArr: [YHSelectImageItem] = [] var failArr: [YHSelectImageItem] = []
YHHUD.show(.progress(message: "上传中...")) YHHUD.show(.progress(message: "上传中..."))
for item in imgArr { for item in imgArr {
if let image = item.data { if let image = item.data {
group.enter() group.enter()
self.uploadImage(image, imageName: item.name) { success, url in self.uploadImage(image) { success, url in
if success { if success {
let doc = YHResignDocumentUploadSignedDocument() let doc = YHResignSignedDocumentModel()
doc.name = item.name
doc.url = url doc.url = url
successArr.append(doc) successArr.append(doc)
} else { } else {
...@@ -686,15 +777,30 @@ extension YHResignDocumentDetailViewController { ...@@ -686,15 +777,30 @@ extension YHResignDocumentDetailViewController {
} else { } else {
YHHUD.flash(message: "\(successArr.count)张照片上传成功\n\(failArr.count)张照片上传失败") YHHUD.flash(message: "\(successArr.count)张照片上传成功\n\(failArr.count)张照片上传失败")
} }
if actionType == .upload {
self.viewModel.uploadSignedDoc(self.docId, type: .upload, successArr) { [weak self] success, error in let count = self.detailModel?.signedDocument.count ?? 0
guard let self = self else { return } successArr.enumerated().forEach { [weak self] index, file in
self.requestData(isNeedLoading: true) guard let self = self else { return }
let extenionName = self.getFileExtenion(file.url)
file.fileName = self.name + "\(count + 1 + index)" + extenionName
}
self.detailModel?.signedDocument.append(contentsOf: successArr)
} else if actionType == .reupload {
if let index = self.detailModel?.signedDocument.firstIndex(where: {
$0.url == oldUrl && oldUrl != nil
}) {
if let file = self.detailModel?.signedDocument[index], let newUrl = successArr.first?.url {
let extenionName = self.getFileExtenion(newUrl)
self.detailModel?.signedDocument[index].url = newUrl
self.detailModel?.signedDocument[index].fileName = self.name + "\(index)" + extenionName
}
}
} }
self.updateUploadFilesSection()
} }
} }
private func uploadImage(_ img: UIImage, imageName: String, callBack: @escaping (Bool, String) -> Void) { private func uploadImage(_ img: UIImage, callBack: @escaping (Bool, String) -> Void) {
// 先OSS上传得到URL // 先OSS上传得到URL
self.viewModel.uploadImage(img) { [weak self] url, error in self.viewModel.uploadImage(img) { [weak self] url, error in
guard self != nil else { return } guard self != nil else { return }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment