CMSCreateDocViewController.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // CMSCreateDocViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/7/8.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import Eureka
  15. import CocoaLumberjack
  16. class CMSCreateDocViewController: FormViewController {
  17. var category: CMSWrapOutCategoryList?
  18. var identityList:[IdentityV2] = []
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. TextRow.defaultCellUpdate = {
  22. cell,row in
  23. cell.textLabel?.font = setting_content_textFont
  24. cell.textLabel?.textColor = setting_content_textColor
  25. }
  26. ActionSheetRow<String>.defaultCellUpdate = {
  27. cell,row in
  28. cell.textLabel?.font = setting_content_textFont
  29. cell.textLabel?.textColor = setting_content_textColor
  30. }
  31. DateTimeRow.defaultCellUpdate = {
  32. cell,row in
  33. cell.textLabel?.font = setting_content_textFont
  34. cell.textLabel?.textColor = setting_content_textColor
  35. }
  36. ButtonRow.defaultCellUpdate = {
  37. cell,row in
  38. cell.textLabel?.font = setting_item_textFont
  39. cell.textLabel?.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
  40. }
  41. title = self.category?.categoryName
  42. loadDepartAndIdentity()
  43. }
  44. func loadDepartAndIdentity(){
  45. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(PersonContext.personContextKey, query: PersonContext.personInfoQuery, parameter: nil)
  46. Alamofire.request(url!).responseJSON(completionHandler: { response in
  47. debugPrint(response.result)
  48. switch response.result {
  49. case .success(let val):
  50. let person = Mapper<PersonV2>().map(JSONString: JSON(val)["data"].description)!
  51. if let identities = person.woIdentityList {
  52. self.identityList = identities
  53. }
  54. DispatchQueue.main.async {
  55. self.showInputUI()
  56. }
  57. case .failure(let err):
  58. DDLogError(err.localizedDescription)
  59. DispatchQueue.main.async {
  60. self.showError(title: "读取身份列表失败")
  61. }
  62. }
  63. })
  64. }
  65. func showInputUI(){
  66. form +++ Section("创建文档")
  67. <<< TextRow("title") {row in
  68. row.title = "文档标题"
  69. row.placeholder = "请输入文档标题"
  70. }.cellSetup({ (cell, row) in
  71. //
  72. })
  73. <<< ActionSheetRow<IdentityV2>("selectedIdentity") {
  74. $0.title = "用户身份"
  75. $0.selectorTitle = "请选择身份"
  76. $0.options = self.identityList
  77. if(self.identityList.count > 0){
  78. $0.value = self.identityList[0]
  79. }
  80. }.cellSetup({ (cell, row) in
  81. //cell.height = 50
  82. })
  83. +++ Section()
  84. <<< ButtonRow("createButton") { (row:ButtonRow) in
  85. row.title = "创建"
  86. }.onCellSelection({ (cell, row) in
  87. let titleRow:TextRow = self.form.rowBy(tag:"title")!
  88. let identityRow:ActionSheetRow<IdentityV2> = self.form.rowBy(tag:"selectedIdentity")!
  89. guard let title = titleRow.value else{
  90. self.showError(title: "请输入标题")
  91. return
  92. }
  93. guard let id = identityRow.value else {
  94. self.showError(title: "请选择身份")
  95. return
  96. }
  97. self.createDocument(title, id.distinguishedName!)
  98. })
  99. }
  100. func createDocument(_ title: String, _ identity: String) {
  101. debugPrint("创建文档:\(title), \(identity)")
  102. var json:[String: Any] = [:]
  103. json["title"] = title
  104. json["appId"] = self.category?.appId
  105. json["categoryId"] = self.category?.id
  106. json["categoryAlias"] = self.category?.categoryAlias
  107. json["categoryName"] = self.category?.categoryName
  108. json["creatorIdentity"] = identity
  109. json["docStatus"] = "draft"
  110. json["isNewDocument"] = true
  111. let doc = CMSCategoryItemData(JSON: json)
  112. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(CMSContext.cmsContextKey, query: CMSContext.cmsDocumentPost, parameter: nil)
  113. Alamofire.request(url!, method: .post, parameters: doc?.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  114. switch response.result {
  115. case .success(let val):
  116. let type = JSON(val)["type"]
  117. if type == "success" {
  118. let docId = JSON(val)["data"]["id"].string!
  119. DispatchQueue.main.async {
  120. self.showSuccess(title: "创建文档成功")
  121. self.performSegue(withIdentifier: "openDocument", sender: docId)
  122. }
  123. }else{
  124. DispatchQueue.main.async {
  125. DDLogError(JSON(val).description)
  126. self.showError(title: "创建文档失败")
  127. }
  128. }
  129. case .failure(let err):
  130. DispatchQueue.main.async {
  131. DDLogError(err.localizedDescription)
  132. self.showError(title: "创建文档失败")
  133. }
  134. }
  135. }
  136. }
  137. /*
  138. // MARK: - Navigation
  139. // In a storyboard-based application, you will often want to do a little preparation before navigation
  140. */
  141. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  142. if segue.identifier == "openDocument" {
  143. let destVC = segue.destination as! CMSItemDetailViewController
  144. destVC.documentId = sender as? String
  145. destVC.fromCreateDocVC = true
  146. }
  147. }
  148. }