CMSCreateDocViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 config: CMSAppConfig?
  19. var identityList:[IdentityV2] = []
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. TextRow.defaultCellUpdate = {
  23. cell,row in
  24. cell.textLabel?.font = setting_content_textFont
  25. cell.textLabel?.textColor = setting_content_textColor
  26. }
  27. ActionSheetRow<String>.defaultCellUpdate = {
  28. cell,row in
  29. cell.textLabel?.font = setting_content_textFont
  30. cell.textLabel?.textColor = setting_content_textColor
  31. }
  32. DateTimeRow.defaultCellUpdate = {
  33. cell,row in
  34. cell.textLabel?.font = setting_content_textFont
  35. cell.textLabel?.textColor = setting_content_textColor
  36. }
  37. ButtonRow.defaultCellUpdate = {
  38. cell,row in
  39. cell.textLabel?.font = setting_item_textFont
  40. cell.textLabel?.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
  41. }
  42. title = self.category?.categoryName
  43. if let ignoreTitle = self.config?.ignoreTitle, ignoreTitle == true {
  44. self.showLoading()
  45. }
  46. loadDepartAndIdentity()
  47. }
  48. func loadDepartAndIdentity(){
  49. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(PersonContext.personContextKey, query: PersonContext.personInfoQuery, parameter: nil)
  50. AF.request(url!).responseJSON(completionHandler: { response in
  51. debugPrint(response.result)
  52. switch response.result {
  53. case .success(let val):
  54. let person = Mapper<PersonV2>().map(JSONString: JSON(val)["data"].description)!
  55. if let identities = person.woIdentityList {
  56. self.identityList = identities
  57. }
  58. DispatchQueue.main.async {
  59. if let ignoreTitle = self.config?.ignoreTitle, ignoreTitle == true, self.identityList.count == 1 {
  60. self.createDocument("", self.identityList[0].distinguishedName!)
  61. }else {
  62. self.hideLoading()
  63. self.showInputUI()
  64. }
  65. }
  66. case .failure(let err):
  67. DDLogError(err.localizedDescription)
  68. DispatchQueue.main.async {
  69. self.showError(title: "读取身份列表失败")
  70. }
  71. }
  72. })
  73. }
  74. func showInputUI(){
  75. form +++ Section("创建文档")
  76. <<< TextRow("title") {row in
  77. row.hidden = Condition.function(["title"], { form in
  78. if let ignoreTitle = self.config?.ignoreTitle, ignoreTitle == true {
  79. return true
  80. }else {
  81. return false
  82. }
  83. })
  84. row.title = "文档标题"
  85. row.placeholder = "请输入文档标题"
  86. }.cellSetup({ (cell, row) in
  87. //
  88. })
  89. <<< ActionSheetRow<IdentityV2>("selectedIdentity") {
  90. $0.title = "用户身份"
  91. $0.selectorTitle = "请选择身份"
  92. $0.options = self.identityList
  93. if(self.identityList.count > 0){
  94. $0.value = self.identityList[0]
  95. }
  96. }.cellSetup({ (cell, row) in
  97. //cell.height = 50
  98. })
  99. +++ Section()
  100. <<< ButtonRow("createButton") { (row:ButtonRow) in
  101. row.title = "创建"
  102. }.onCellSelection({ (cell, row) in
  103. let titleRow:TextRow = self.form.rowBy(tag:"title")!
  104. let identityRow:ActionSheetRow<IdentityV2> = self.form.rowBy(tag:"selectedIdentity")!
  105. var title = ""
  106. if let ignoreTitle = self.config?.ignoreTitle, ignoreTitle == true {
  107. title = ""
  108. }else {
  109. guard let ctitle = titleRow.value else{
  110. self.showError(title: "请输入标题")
  111. return
  112. }
  113. title = ctitle
  114. }
  115. guard let id = identityRow.value else {
  116. self.showError(title: "请选择身份")
  117. return
  118. }
  119. self.createDocument(title, id.distinguishedName!)
  120. })
  121. }
  122. func createDocument(_ title: String, _ identity: String) {
  123. debugPrint("创建文档:\(title), \(identity)")
  124. if self.category?.workflowFlag != nil && self.category?.workflowFlag != "" {
  125. self.createProcess(title, identity: identity, processId: self.category!.workflowFlag!)
  126. }else {
  127. var json:[String: Any] = [:]
  128. json["title"] = title
  129. json["appId"] = self.category?.appId
  130. json["categoryId"] = self.category?.id
  131. json["categoryAlias"] = self.category?.categoryAlias
  132. json["categoryName"] = self.category?.categoryName
  133. json["creatorIdentity"] = identity
  134. json["docStatus"] = "draft"
  135. json["isNewDocument"] = true
  136. let doc = CMSCategoryItemData(JSON: json)
  137. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(CMSContext.cmsContextKey, query: CMSContext.cmsDocumentPost, parameter: nil)
  138. AF.request(url!, method: .post, parameters: doc?.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  139. switch response.result {
  140. case .success(let val):
  141. let type = JSON(val)["type"]
  142. if type == "success" {
  143. let docId = JSON(val)["data"]["id"].string!
  144. DispatchQueue.main.async {
  145. self.showSuccess(title: "创建文档成功")
  146. self.performSegue(withIdentifier: "openDocument", sender: docId)
  147. }
  148. }else{
  149. DispatchQueue.main.async {
  150. DDLogError(JSON(val).description)
  151. self.showError(title: "创建文档失败")
  152. }
  153. }
  154. case .failure(let err):
  155. DispatchQueue.main.async {
  156. DDLogError(err.localizedDescription)
  157. self.showError(title: "创建文档失败")
  158. }
  159. }
  160. }
  161. }
  162. }
  163. func createProcess(_ title:String,identity:String, processId: String){
  164. DDLogDebug("title = \(title),identity = \(identity)")
  165. let bean = CreateProcessCmsBean()
  166. bean.title = title
  167. bean.identity = identity
  168. let data = CmsDocData()
  169. let categoryId = self.category!.id!
  170. let appId = self.category!.appId!
  171. data.title = title
  172. data.creatorIdentity = identity
  173. data.isNewDocument = true
  174. data.appId = appId
  175. data.categoryId = categoryId
  176. data.docStatus = "draft"
  177. data.createTime = Date().toString("yyyy-MM-dd HH:mm:ss")
  178. data.categoryName = self.category!.categoryName
  179. data.categoryAlias = self.category!.categoryAlias
  180. let d = CreateProcessCmsData()
  181. d.cmsDocument = data
  182. bean.data = d
  183. let createURL = AppDelegate.o2Collect.generateURLWithAppContextKey(WorkContext.workContextKey, query: WorkContext.workCreateQuery, parameter: ["##id##":processId as AnyObject])
  184. if let ignoreTitle = self.config?.ignoreTitle, ignoreTitle == true, self.identityList.count == 1 {
  185. }else {
  186. self.showLoading(title: "创建中,请稍候...")
  187. }
  188. AF.request(createURL!,method:.post, parameters: bean.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  189. debugPrint(response.result)
  190. switch response.result {
  191. case .success(let val):
  192. let taskList = JSON(val)["data"][0]
  193. DDLogDebug(taskList.description)
  194. if let tasks = Mapper<TodoTask>().mapArray(JSONString:taskList["taskList"].debugDescription) , tasks.count > 0 {
  195. let taskStoryboard = UIStoryboard(name: "task", bundle: Bundle.main)
  196. let todoTaskDetailVC = taskStoryboard.instantiateViewController(withIdentifier: "todoTaskDetailVC") as! TodoTaskDetailViewController
  197. todoTaskDetailVC.todoTask = tasks[0]
  198. todoTaskDetailVC.backFlag = 5
  199. self.navigationController?.pushViewController(todoTaskDetailVC, animated: true)
  200. DispatchQueue.main.async {
  201. self.hideLoading()
  202. }
  203. } else {
  204. self.showError(title: "创建失败")
  205. }
  206. case .failure(let err):
  207. DDLogError(err.localizedDescription)
  208. self.showError(title: "创建失败")
  209. }
  210. }
  211. }
  212. /*
  213. // MARK: - Navigation
  214. // In a storyboard-based application, you will often want to do a little preparation before navigation
  215. */
  216. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  217. if segue.identifier == "openDocument" {
  218. let destVC = segue.destination as! CMSItemDetailViewController
  219. destVC.documentId = sender as? String
  220. destVC.fromCreateDocVC = true
  221. }
  222. }
  223. }