TodoTaskDetailViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. //
  2. // TodoTaskDetailViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/7/31.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import Alamofire
  11. import AlamofireImage
  12. import AlamofireObjectMapper
  13. import SwiftyJSON
  14. import ObjectMapper
  15. import CocoaLumberjack
  16. struct TodoTaskJS {
  17. static let DATA_TASK = "JSON.encode(layout.appForm.businessData.task);"
  18. static let DATA_READ = "JSON.encode(layout.appForm.businessData.read);"
  19. static let DATA_OPINION = "JSON.encode(layout.appForm.getOpinion());"
  20. static let DATA_CONTROL = "JSON.encode(layout.appForm.businessData.control);"
  21. static let DATA_WORK_TITLE = "JSON.encode(layout.appForm.businessData.work.title);"
  22. static let DATA_WORK = "JSON.encode(layout.appForm.businessData.work);"
  23. static let DATA_BUSINESS = "JSON.encode(layout.appForm.getData());"
  24. static let CHECK_FORM = "layout.appForm.formValidation(null, null)"
  25. static func getDataWithJS(_ webView:UIWebView,jscode:String) -> [String:AnyObject] {
  26. let str = webView.stringByEvaluatingJavaScript(from: jscode)
  27. //let data = str?.dataUsingEncoding(NSUTF8StringEncoding)
  28. let json = JSON.init(parseJSON: str!)
  29. return json.dictionaryObject! as [String : AnyObject]
  30. }
  31. }
  32. class TodoTaskDetailViewController: BaseTaskWebViewController {
  33. @IBOutlet weak var progress: UIProgressView!
  34. @IBOutlet weak var webViewContainer: UIView!
  35. var toolbarView: UIToolbar!
  36. var taskProcess = TaskProcess()
  37. let group = DispatchGroup()
  38. /// backFlag = 1来自MainTask,backFlag = 2来自TodoTask 3是show dis
  39. var backFlag:Int = 0
  40. var loadUrl:String?
  41. var isJSExecuted:Bool = true
  42. var hasToolbar:Bool = false
  43. var todoTask:TodoTask? {
  44. didSet {
  45. var url:String?
  46. if let workId = todoTask?.work {
  47. url = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoDesktopQuery, parameter: ["##workid##":workId as AnyObject])
  48. self.isWorkCompeleted = false
  49. self.workId = workId
  50. }else if let workCompletedId = todoTask?.workCompleted {
  51. url = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoedDestopQuery, parameter: ["##workCompletedId##":workCompletedId as AnyObject])
  52. self.isWorkCompeleted = true
  53. self.workId = workCompletedId
  54. }
  55. self.loadUrl = url
  56. }
  57. }
  58. var myTask: [String : AnyObject]?
  59. var myRead: [String : AnyObject]?
  60. var myControl: [String : AnyObject]?
  61. var myTitle: String?
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. // 返回按钮重新定义
  65. self.navigationItem.hidesBackButton = true
  66. self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_fanhui"), style: .plain, target: self, action: #selector(goBack))
  67. self.navigationItem.leftItemsSupplementBackButton = true
  68. // 表单加载完成的代理
  69. self.loadedDelegate = self
  70. //toolbar
  71. self.toolbarView = UIToolbar(frame: CGRect(x: 0, y: self.view.height - 44, width: self.view.width, height: 44))
  72. self.automaticallyAdjustsScrollViewInsets = false
  73. myTitle = todoTask?.title
  74. if myTitle != nil {
  75. title = myTitle
  76. }
  77. self.theWebView()
  78. }
  79. override func viewWillAppear(_ animated: Bool) {
  80. super.viewWillAppear(animated)
  81. //监控进度
  82. self.addObserver(webView, forKeyPath: "estimatedProgress", options: .new, context: nil)
  83. }
  84. override func viewWillDisappear(_ animated: Bool) {
  85. super.viewWillDisappear(animated)
  86. self.removeObserver(webView, forKeyPath: "estimatedProgress")
  87. }
  88. override func theWebView(){
  89. super.theWebView()
  90. self.webViewContainer.addSubview(self.webView)
  91. self.webView.translatesAutoresizingMaskIntoConstraints = false
  92. let top = NSLayoutConstraint(item: self.webView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.webViewContainer, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
  93. let bottom = NSLayoutConstraint(item: self.webView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.webViewContainer, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
  94. let trailing = NSLayoutConstraint(item: self.webView, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.webViewContainer, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
  95. let leading = NSLayoutConstraint(item: self.webView, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.webViewContainer, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
  96. self.webViewContainer.addConstraints([top, bottom, trailing, leading])
  97. webView.navigationDelegate = self
  98. webView.uiDelegate = self
  99. webView.load(Alamofire.request(loadUrl!).request!)
  100. webView.allowsBackForwardNavigationGestures = true
  101. }
  102. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  103. if keyPath == "estimatedProgress" {
  104. progress.isHidden = webView.estimatedProgress == 1
  105. progress.setProgress(Float(webView.estimatedProgress), animated: true)
  106. }
  107. }
  108. override func didReceiveMemoryWarning() {
  109. super.didReceiveMemoryWarning()
  110. // Dispose of any resources that can be recreated.
  111. }
  112. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  113. if segue.identifier == "showTodoProcessSegue" {
  114. let destVC = segue.destination as! TodoTaskProcessViewController
  115. //传递到下一步
  116. destVC.backFlag = backFlag
  117. destVC.taskProcess = self.taskProcess
  118. }
  119. }
  120. /**
  121. 提交后返回此处,在此执行是返回首页还是待办处理页
  122. - parameter segue:
  123. */
  124. @IBAction func processBackMe(_ segue:UIStoryboardSegue){
  125. goBack()
  126. }
  127. //MARK: - private func
  128. @objc func goBack() {
  129. DDLogError("backFlag = \(backFlag)")
  130. switch backFlag {
  131. case 1:
  132. self.performSegue(withIdentifier: "backMainTask", sender: nil)
  133. break
  134. case 2:
  135. self.performSegue(withIdentifier: "backToTodoTask", sender: nil)
  136. break
  137. default: // 3,4都用隐藏 除非删除 删除结束有特殊处理了。
  138. self.navigationController?.popViewController(animated: true)
  139. break
  140. }
  141. }
  142. @objc func itemBtnDocDeleteAction() {
  143. DDLogDebug("btnDeleteDoc Click")
  144. showDefaultConfirm(title: "提示", message: "确认要删除这个文档吗,删除后无法恢复?", okHandler: { (action) in
  145. ProgressHUD.show("删除中...",interaction: false)
  146. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskContext.taskDataContextKey, query: TaskContext.taskWorkDeleteQuery, parameter: ["##id##":self.workId! as AnyObject])
  147. Alamofire.request(url!,method:.delete, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  148. switch response.result {
  149. case .success(let val):
  150. //DDLogDebug(val)
  151. let json = JSON(val)
  152. if json["type"] == "success" {
  153. ProgressHUD.showSuccess("删除成功")
  154. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
  155. // 删除之后没有这个工作了,所以直接返回列表 防止返回到已办的TodoedTaskViewController
  156. if self.backFlag == 4 {
  157. self.backFlag = 2
  158. }
  159. self.goBack()
  160. })
  161. }else{
  162. DDLogError(json.description)
  163. ProgressHUD.showError("删除失败")
  164. }
  165. case .failure(let err):
  166. DDLogError(err.localizedDescription)
  167. ProgressHUD.showError("删除失败")
  168. }
  169. }
  170. })
  171. }
  172. @objc func itemBtnDocSaveAction() {
  173. DDLogDebug("btnSaveDoc Click")
  174. ProgressHUD.show("保存中...",interaction: false)
  175. self.setupData()
  176. group.notify(queue: DispatchQueue.main) {
  177. if self.isJSExecuted {
  178. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskContext.taskDataContextKey, query: TaskContext.taskDataSaveQuery, parameter: ["##id##":self.taskProcess.workId! as AnyObject])
  179. Alamofire.request(url!,method:.put, parameters: self.taskProcess.businessDataDict!, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  180. switch response.result {
  181. case .success(let val):
  182. //DDLogDebug(val)
  183. let json = JSON(val)
  184. if json["type"] == "success" {
  185. ProgressHUD.showSuccess("保存成功")
  186. }else{
  187. DDLogError(json.description)
  188. ProgressHUD.showError("保存失败")
  189. }
  190. case .failure(let err):
  191. DDLogError(err.localizedDescription)
  192. ProgressHUD.showError("保存失败")
  193. }
  194. }
  195. }else{
  196. ProgressHUD.showError("保存失败")
  197. }
  198. }
  199. }
  200. @objc func itemBtnNextProcessAction() {
  201. DDLogDebug("btnNext Process")
  202. //校验表单
  203. webView.evaluateJavaScript(TodoTaskJS.CHECK_FORM) { (data, err) in
  204. if let str = data {
  205. let isVaild = str as! Bool
  206. if isVaild == true {
  207. self.setupData()
  208. self.group.notify(queue: DispatchQueue.main, execute: {
  209. self.performSegue(withIdentifier: "showTodoProcessSegue", sender: nil)
  210. })
  211. }else{
  212. DDLogError("表单验证失败。。。。。。。。。。。。")
  213. ProgressHUD.showError("表单验证失败,请正确填写表单内容")
  214. }
  215. }else {
  216. DDLogError("没有返回值。。。。。。。。。")
  217. ProgressHUD.showError("表单验证失败,请正确填写表单内容")
  218. }
  219. }
  220. // let str = self.todoWebView.stringByEvaluatingJavaScript(from: TodoTaskJS.CHECK_FORM)
  221. // //let str = "true"
  222. // if str == "true" {
  223. // DDLogDebug("next Step")
  224. // self.setupData()
  225. // self.performSegue(withIdentifier: "showTodoProcessSegue", sender: nil)
  226. // }
  227. }
  228. @objc func itemBtnReadDocAction() {
  229. DDLogDebug("readButtonAction")
  230. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(ReadContext.readContextKey, query: ReadContext.readProcessing, parameter: ["##id##":(todoTask?.id)! as AnyObject])
  231. ProgressHUD.show("提交中...")
  232. Alamofire.request(url!, method:.post, parameters: myRead, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  233. switch response.result {
  234. case .success(let val):
  235. DDLogDebug(JSON(val).description)
  236. let json = JSON(val)
  237. if json["type"]=="success"{
  238. ProgressHUD.showSuccess("提交成功")
  239. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
  240. self.goBack()
  241. })
  242. }else {
  243. DDLogError(json["message"].description)
  244. ProgressHUD.showError("提交失败")
  245. }
  246. case .failure(let err):
  247. DDLogError(err.localizedDescription)
  248. ProgressHUD.showError("提交失败")
  249. }
  250. }
  251. }
  252. @objc func itemBtnRetractDocAction() {
  253. DDLogDebug("撤回开始。。。")
  254. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskedContext.taskedContextKey, query: TaskedContext.taskedRetractQuery, parameter: ["##work##":(self.workId)! as AnyObject])
  255. ProgressHUD.show("提交中...")
  256. Alamofire.request(url!, method:.put, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  257. switch response.result {
  258. case .success(let val):
  259. DDLogDebug(JSON(val).description)
  260. let json = JSON(val)
  261. if json["type"]=="success"{
  262. ProgressHUD.showSuccess("提交成功")
  263. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
  264. self.goBack()
  265. })
  266. }else {
  267. DDLogError(json["message"].description)
  268. ProgressHUD.showError("提交失败")
  269. }
  270. case .failure(let err):
  271. DDLogError(err.localizedDescription)
  272. ProgressHUD.showError("提交失败")
  273. }
  274. }
  275. }
  276. // 网页加载完成后,获取表单数据 判断是什么表单 待办 待阅 已办 已阅
  277. private func loadDataFromWork() {
  278. // 加载read 对象 如果是待阅工作 设置已阅时需要用到
  279. group.enter()
  280. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  281. DDLogDebug("执行 \(TodoTaskJS.DATA_READ)")
  282. self.webView.evaluateJavaScript(TodoTaskJS.DATA_READ, completionHandler: { (data, err) in
  283. if err == nil && data != nil {
  284. let json = JSON.init(parseJSON: data as! String)
  285. self.myRead = json.dictionaryObject! as [String: AnyObject]
  286. }else {
  287. DDLogError(String(describing: err))
  288. }
  289. self.group.leave()
  290. })
  291. }))
  292. // 加载control 是否能撤回
  293. group.enter()
  294. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  295. DDLogDebug("执行 \(TodoTaskJS.DATA_CONTROL)")
  296. self.webView.evaluateJavaScript(TodoTaskJS.DATA_CONTROL, completionHandler: { (data, err) in
  297. if err == nil && data != nil {
  298. let json = JSON.init(parseJSON: (data as! String))
  299. DDLogDebug("control: \(data as! String)")
  300. self.myControl = json.dictionaryObject! as [String: AnyObject]
  301. }else {
  302. DDLogError(String(describing: err))
  303. }
  304. self.group.leave()
  305. })
  306. }))
  307. if myTitle == nil || myTitle!.trim().isEmpty {
  308. group.enter()
  309. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  310. DDLogDebug("执行 \(TodoTaskJS.DATA_WORK_TITLE)")
  311. self.webView.evaluateJavaScript(TodoTaskJS.DATA_WORK_TITLE, completionHandler: { (data, err) in
  312. if err == nil && data != nil {
  313. self.myTitle = data as! String
  314. self.title = self.myTitle
  315. }else {
  316. DDLogError(String(describing: err))
  317. }
  318. self.group.leave()
  319. })
  320. }))
  321. }
  322. group.notify(queue: DispatchQueue.main) {
  323. self.setupToolbarItems()
  324. }
  325. }
  326. private func setupToolbarItems() {
  327. DDLogDebug("setupToolbarItems 处理底部按钮, 根据control")
  328. var items: [UIBarButtonItem] = []
  329. if self.myControl != nil {
  330. let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
  331. if let allowDelete = self.myControl!["allowDelete"] as? Bool {
  332. if allowDelete { //删除工作
  333. DDLogDebug("删除工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  334. let deleteBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  335. deleteBtn.setTitle("删除", for: .normal)
  336. deleteBtn.setTitleColor(base_color, for: .normal)
  337. deleteBtn.addTapGesture { (tap) in
  338. self.itemBtnDocDeleteAction()
  339. }
  340. let deleteItem = UIBarButtonItem(customView: deleteBtn)
  341. items.append(spaceItem)
  342. items.append(deleteItem)
  343. items.append(spaceItem)
  344. }
  345. }
  346. if let allowSave = self.myControl!["allowSave"] as? Bool {
  347. if allowSave {// 保存工作
  348. DDLogDebug("保存工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  349. let saveBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  350. saveBtn.setTitle("保存", for: .normal)
  351. saveBtn.setTitleColor(base_color, for: .normal)
  352. saveBtn.addTapGesture { (tap) in
  353. self.itemBtnDocSaveAction()
  354. }
  355. let saveItem = UIBarButtonItem(customView: saveBtn)
  356. items.append(spaceItem)
  357. items.append(saveItem)
  358. items.append(spaceItem)
  359. }
  360. }
  361. if let allowProcessing = self.myControl!["allowProcessing"] as? Bool {
  362. if allowProcessing { // 待办工作
  363. DDLogDebug("待办工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  364. let processingBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  365. processingBtn.setTitle("继续流转", for: .normal)
  366. processingBtn.setTitleColor(base_color, for: .normal)
  367. processingBtn.addTapGesture { (tap) in
  368. self.itemBtnNextProcessAction()
  369. }
  370. let processingItem = UIBarButtonItem(customView: processingBtn)
  371. items.append(spaceItem)
  372. items.append(processingItem)
  373. items.append(spaceItem)
  374. }
  375. }
  376. if let allowReadProcessing = self.myControl!["allowReadProcessing"] as? Bool {
  377. if allowReadProcessing { // 待阅 工作
  378. DDLogDebug("待阅工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  379. let readBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  380. readBtn.setTitle("已阅", for: .normal)
  381. readBtn.setTitleColor(base_color, for: .normal)
  382. readBtn.addTapGesture { (tap) in
  383. self.itemBtnReadDocAction()
  384. }
  385. let readItem = UIBarButtonItem(customView: readBtn)
  386. items.append(spaceItem)
  387. items.append(readItem)
  388. items.append(spaceItem)
  389. }
  390. }
  391. if let allowRetract = self.myControl!["allowRetract"] as? Bool {
  392. if allowRetract { // 撤回
  393. DDLogDebug("可以撤回。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  394. let retractBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  395. retractBtn.setTitle("撤回", for: .normal)
  396. retractBtn.setTitleColor(base_color, for: .normal)
  397. retractBtn.addTapGesture { (tap) in
  398. self.itemBtnRetractDocAction()
  399. }
  400. let retractItem = UIBarButtonItem(customView: retractBtn)
  401. items.append(spaceItem)
  402. items.append(retractItem)
  403. items.append(spaceItem)
  404. }
  405. }
  406. if items.count > 0 {
  407. self.toolbarView.items = items
  408. self.hasToolbar = true
  409. self.view.addSubview(self.toolbarView)
  410. self.toolbarView.translatesAutoresizingMaskIntoConstraints = false
  411. let heightC = NSLayoutConstraint(item: self.toolbarView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0.0, constant: 44)
  412. self.toolbarView.addConstraint(heightC)
  413. let bottom = NSLayoutConstraint(item: self.toolbarView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
  414. let trailing = NSLayoutConstraint(item: self.toolbarView, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
  415. let leading = NSLayoutConstraint(item: self.toolbarView, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
  416. self.view.addConstraints([bottom, leading, trailing])
  417. self.view.constraints.forEach { (constraint) in
  418. if constraint.identifier == "webViewBottomConstraint" {
  419. self.view.removeConstraint(constraint)
  420. }
  421. }
  422. let webcTop = NSLayoutConstraint(item: self.webViewContainer, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.toolbarView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
  423. self.view.addConstraint(webcTop)
  424. self.view.layoutIfNeeded()
  425. }
  426. NSLog("\(self.view.subviews)");
  427. }else {
  428. DDLogError("没有control 数据异常 按钮无法计算。。。。")
  429. }
  430. }
  431. /**
  432. * 读取从页面载入的业务及流程数据,建立数据模型
  433. */
  434. func setupData(){
  435. group.enter()
  436. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  437. DDLogInfo("opinion queue .....")
  438. self.webView.evaluateJavaScript(TodoTaskJS.DATA_OPINION, completionHandler: { (data, err) in
  439. if err == nil && data != nil {
  440. let opinion = data as! String
  441. DDLogInfo("opinion: \(opinion)")
  442. if opinion == "\"\"" {
  443. self.taskProcess.opinion = ""
  444. }else {
  445. let json = JSON.init(parseJSON: opinion)
  446. let oJson = json.dictionaryObject as? [String : AnyObject]
  447. let op = oJson!["opinion"] as? String
  448. self.taskProcess.opinion = op
  449. }
  450. }else {
  451. DDLogError(String(describing: err))
  452. }
  453. self.group.leave()
  454. })
  455. }))
  456. group.enter()
  457. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  458. DDLogDebug("taskQueue 1")
  459. self.webView.evaluateJavaScript(TodoTaskJS.DATA_TASK) { (data, err) in
  460. if err == nil && data != nil {
  461. DDLogDebug("taskQueue complete")
  462. let json = JSON.init(parseJSON: data as! String)
  463. self.taskProcess.taskDict = json.dictionaryObject! as [String : AnyObject]
  464. self.taskProcess.taskId = self.taskProcess.taskDict!["id"] as? String
  465. self.taskProcess.decisonList = self.taskProcess.taskDict!["routeNameList"] as? [String]
  466. }else{
  467. DDLogError(String(describing: err))
  468. self.isJSExecuted = false
  469. }
  470. self.group.leave()
  471. }
  472. }))
  473. group.enter()
  474. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  475. DDLogDebug("workQueue 1")
  476. self.webView.evaluateJavaScript(TodoTaskJS.DATA_WORK) { (data, err) in
  477. if err == nil && data != nil {
  478. DDLogDebug("workQueue complete")
  479. let json = JSON.init(parseJSON: data as! String)
  480. self.taskProcess.workDict = json.dictionaryObject! as [String : AnyObject]
  481. self.taskProcess.workId = self.taskProcess.workDict!["id"] as? String
  482. }else{
  483. DDLogError(String(describing: err))
  484. self.isJSExecuted = false
  485. }
  486. self.group.leave()
  487. }
  488. }))
  489. group.enter()
  490. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  491. DDLogDebug("businessQueue 1")
  492. self.webView.evaluateJavaScript(TodoTaskJS.DATA_BUSINESS) { (data, err) in
  493. if err == nil && data != nil {
  494. DDLogDebug("businessQueue complete")
  495. let json = JSON.init(parseJSON: data as! String)
  496. self.taskProcess.businessDataDict = json.dictionaryObject! as [String : AnyObject]
  497. //do {
  498. //}catch{
  499. //DDLogError("set routeNameList Error")
  500. //}
  501. }else{
  502. DDLogError(String(describing: err))
  503. self.isJSExecuted = false
  504. }
  505. self.group.leave()
  506. }
  507. }))
  508. }
  509. }
  510. //MARK: - extension
  511. extension TodoTaskDetailViewController: O2WorkFormLoadedDelegate {
  512. func workFormLoaded() {
  513. DDLogInfo("表单页面加载完成, 开始判断工作,生成操作按钮!")
  514. self.loadDataFromWork()
  515. }
  516. }
  517. extension TodoTaskDetailViewController:WKNavigationDelegate,WKUIDelegate {
  518. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  519. DDLogDebug("didStartProvisionalNavigation")
  520. }
  521. func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
  522. DDLogDebug("didCommit")
  523. }
  524. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  525. DDLogDebug("didFinish")
  526. }
  527. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  528. DDLogDebug("didFail")
  529. DDLogError(error.localizedDescription)
  530. self.showError(title: "工作加载异常!")
  531. }
  532. // func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  533. // print("navigationAction \(navigationAction)")
  534. // decisionHandler(WKNavigationActionPolicy.allow)
  535. // }
  536. //
  537. // func webView(_ webView: WKWebView, commitPreviewingViewController previewingViewController: UIViewController) {
  538. // print("previewingViewController \(previewingViewController)")
  539. // }
  540. //
  541. // func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
  542. // print("didFailProvisionalNavigation navigation \(navigation)")
  543. // }
  544. //
  545. // func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  546. //
  547. // }
  548. //
  549. // func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
  550. // print("runJavaScriptAlertPanelWithMessage message \(message)")
  551. // completionHandler()
  552. // }
  553. //
  554. // func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
  555. // print("runJavaScriptConfirmPanelWithMessage message \(message)")
  556. // completionHandler(true)
  557. // }
  558. // func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
  559. // print("runJavaScriptTextInputPanelWithPrompt message \(prompt)")
  560. // completionHandler("")
  561. // }
  562. //
  563. //
  564. // func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  565. // print(navigation)
  566. // }
  567. //
  568. // func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
  569. // print("navigationResponse navigationResponse \(navigationResponse)")
  570. // decisionHandler(.allow)
  571. // }
  572. }