TaskCreateViewController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // TaskCreateViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/7/29.
  6. // Copyright © 2016年 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 TaskCreateViewController: UIViewController {
  17. var process: AppProcess? {
  18. didSet {
  19. }
  20. }
  21. var identitys: [IdentityV2]?
  22. var task: TodoTask?
  23. var identityViews: [IdentitySelectView] = []
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "创建", style: .plain, target: self, action: #selector(create))
  27. title = process?.name
  28. // showInputUI()
  29. self.setupUI()
  30. }
  31. private func setupUI() {
  32. let title = self.initTitleAndConstraint()
  33. let height = title.frame.height + 20
  34. if let ids = self.identitys {
  35. var i = 0
  36. ids.forEach { (identity) in
  37. self.initIdentityView(id: identity, top: (height + i.toCGFloat * 94 + 20))
  38. i += 1
  39. }
  40. self.identityViews.first?.selectedIdentity()
  41. }
  42. }
  43. private func initIdentityView(id: IdentityV2, top: CGFloat) {
  44. let view = Bundle.main.loadNibNamed("IdentitySelectView", owner: self, options: nil)?.first as! IdentitySelectView
  45. view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 94)
  46. view.setUp(identity: id)
  47. self.identityViews.append(view)
  48. view.translatesAutoresizingMaskIntoConstraints = false
  49. self.view.addSubview(view)
  50. let top = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: view.superview!, attribute: .top, multiplier: 1, constant: top)
  51. let left = NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: view.superview!, attribute: .leading, multiplier: 1, constant: 0)
  52. let right = NSLayoutConstraint(item: view.superview!, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
  53. NSLayoutConstraint.activate([top, left, right])
  54. view.addTapGesture { (tap) in
  55. let id = view.id?.distinguishedName
  56. self.identityViews.forEach { (i) in
  57. if i.id?.distinguishedName == id {
  58. i.selectedIdentity()
  59. } else {
  60. i.deSelectedIdentity()
  61. }
  62. }
  63. }
  64. }
  65. private func initTitleAndConstraint() -> UILabel {
  66. //标题
  67. let title = UILabel()
  68. title.text = "请选择身份:"
  69. title.font = setting_content_textFont
  70. title.translatesAutoresizingMaskIntoConstraints = false
  71. self.view.addSubview(title)
  72. let top = NSLayoutConstraint(item: title, attribute: .top, relatedBy: .equal, toItem: title.superview!, attribute: .top, multiplier: 1, constant: 20)
  73. let left = NSLayoutConstraint(item: title, attribute: .leading, relatedBy: .equal, toItem: title.superview!, attribute: .leading, multiplier: 1, constant: 10)
  74. let right = NSLayoutConstraint(item: title.superview!, attribute: .trailing, relatedBy: .equal, toItem: title, attribute: .trailing, multiplier: 1, constant: 10)
  75. NSLayoutConstraint.activate([top, left, right])
  76. return title
  77. }
  78. //
  79. // func showInputUI() {
  80. // form +++ Section("创建流程")
  81. // //不需要标题
  82. //// <<< TextRow("title") {row in
  83. //// row.title = "标题"
  84. //// row.placeholder = "请输入标题"
  85. //// }.cellSetup({ (cell, row) in
  86. //// //cell.height = 50
  87. //// })
  88. //
  89. // <<< ActionSheetRow<IdentityV2>("selectedIdentity") {
  90. // $0.title = "用户身份"
  91. // $0.selectorTitle = "请选择身份?"
  92. // $0.options = self.identitys
  93. // if(self.identitys != nil && self.identitys!.count > 0) {
  94. // $0.value = self.identitys![0]
  95. // }
  96. // }.cellSetup({ (cell, row) in
  97. // //cell.height = 50
  98. // })
  99. //
  100. //
  101. // <<< DateTimeRow("createTime") { row in
  102. // row.title = "创建时间"
  103. // row.value = Date()
  104. // let formatter = DateFormatter()
  105. // formatter.locale = Locale.current
  106. // formatter.dateStyle = .long
  107. // row.dateFormatter = formatter
  108. // }.cellSetup({ (cell, row) in
  109. // //cell.height = 50
  110. // })
  111. //
  112. // +++ Section()
  113. // <<< ButtonRow("createButton") { (row: ButtonRow) in
  114. // row.title = "创建"
  115. // }.onCellSelection({ (cell, row) in
  116. //// let titleRow:TextRow = self.form.rowBy(tag:"title")!
  117. // let identityRow: ActionSheetRow<IdentityV2> = self.form.rowBy(tag: "selectedIdentity")!
  118. //// guard let title = titleRow.value else{
  119. //// self.showError(title: "请输入标题")
  120. //// return
  121. //// }
  122. // guard let id = identityRow.value else {
  123. // self.showError(title: "请选择身份")
  124. // return
  125. // }
  126. //
  127. // if let mode = self.process?.defaultStartMode, mode == O2.O2_Word_draft_mode {
  128. // self.createDraft(processId: self.process!.id!, identity: id.distinguishedName!)
  129. // } else {
  130. // self.createProcess(processId: self.process!.id!, identity: id.distinguishedName!)
  131. // }
  132. // })
  133. // }
  134. @objc func create() {
  135. guard let id = self.identityViews.first(where: { (view) -> Bool in
  136. view.selected == true
  137. }) else {
  138. self.showError(title: "请选择身份")
  139. return
  140. }
  141. if let mode = self.process?.defaultStartMode, mode == O2.O2_Word_draft_mode {
  142. self.createDraft(processId: self.process?.id ?? "", identity: id.id?.distinguishedName ?? "")
  143. } else {
  144. self.createProcess(processId: self.process?.id ?? "", identity: id.id?.distinguishedName ?? "")
  145. }
  146. }
  147. //开启流程 创建工作
  148. func createProcess(processId: String, identity: String) {
  149. let bean = CreateProcessBean()
  150. bean.title = ""//不需要标题
  151. bean.identity = identity
  152. let createURL = AppDelegate.o2Collect.generateURLWithAppContextKey(WorkContext.workContextKey, query: WorkContext.workCreateQuery, parameter: ["##id##": processId as AnyObject])
  153. self.showLoading(title: "创建中,请稍候...")
  154. AF.request(createURL!, method: .post, parameters: bean.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  155. debugPrint(response.result)
  156. switch response.result {
  157. case .success(let val):
  158. let taskList = JSON(val)["data"][0]
  159. DDLogDebug(taskList.description)
  160. if let tasks = Mapper<TodoTask>().mapArray(JSONString: taskList["taskList"].debugDescription), tasks.count > 0 {
  161. let taskStoryboard = UIStoryboard(name: "task", bundle: Bundle.main)
  162. let todoTaskDetailVC = taskStoryboard.instantiateViewController(withIdentifier: "todoTaskDetailVC") as! TodoTaskDetailViewController
  163. todoTaskDetailVC.todoTask = tasks[0]
  164. todoTaskDetailVC.backFlag = 1
  165. self.navigationController?.pushViewController(todoTaskDetailVC, animated: true)
  166. DispatchQueue.main.async {
  167. self.hideLoading()
  168. }
  169. } else {
  170. self.showError(title: "创建失败")
  171. }
  172. case .failure(let err):
  173. DDLogError(err.localizedDescription)
  174. self.showError(title: "创建失败")
  175. }
  176. }
  177. }
  178. //创建草稿
  179. private func createDraft(processId: String, identity: String) {
  180. let bean = CreateProcessBean()
  181. bean.title = ""
  182. bean.identity = identity
  183. let draftCreateUrl = AppDelegate.o2Collect.generateURLWithAppContextKey(WorkContext.workContextKey, query: WorkContext.draftWorkCreateQuery, parameter: ["##processId##": processId as AnyObject])
  184. self.showLoading(title: "创建中,请稍候...")
  185. AF.request(draftCreateUrl!, method: .post, parameters: bean.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  186. switch response.result {
  187. case .success(let val):
  188. let draftData = JSON(val)["data"]
  189. DDLogDebug(draftData.description)
  190. if let draft = Mapper<ProcessDraftBean>().map(JSONString: draftData["work"].debugDescription) {
  191. let taskStoryboard = UIStoryboard(name: "task", bundle: Bundle.main)
  192. let todoTaskDetailVC = taskStoryboard.instantiateViewController(withIdentifier: "todoTaskDetailVC") as! TodoTaskDetailViewController
  193. todoTaskDetailVC.draft = draft
  194. todoTaskDetailVC.backFlag = 1
  195. self.navigationController?.pushViewController(todoTaskDetailVC, animated: true)
  196. DispatchQueue.main.async {
  197. self.hideLoading()
  198. }
  199. } else {
  200. self.showError(title: "创建失败")
  201. }
  202. case .failure(let err):
  203. DDLogError(err.localizedDescription)
  204. self.showError(title: "创建失败")
  205. }
  206. }
  207. }
  208. }