TodoTaskDetailViewController.swift 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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 ObjectMapper
  14. import CocoaLumberjack
  15. import Photos
  16. //import QuickLook
  17. //import IQKeyboardManagerSwift
  18. struct TodoTaskJS {
  19. static let DATA_TASK = "JSON.encode(layout.app.appForm.businessData.task);"
  20. static let DATA_READ = "JSON.encode(layout.app.appForm.businessData.read);"
  21. static let DATA_OPINION = "JSON.encode(layout.app.appForm.getOpinion());"
  22. static let DATA_CONTROL = "JSON.encode(layout.app.appForm.businessData.control);"
  23. static let DATA_WORK = "JSON.encode(layout.app.appForm.businessData.work);"
  24. static let DATA_BUSINESS = "JSON.encode(layout.app.appForm.getData());"
  25. static let CHECK_FORM = "layout.app.appForm.formValidation(null, null)"
  26. static let CLOSE_WORK = "layout.app.appForm.finishOnMobile()"
  27. }
  28. class TodoTaskDetailViewController: BaseWebViewUIViewController {
  29. @IBOutlet weak var progress: UIProgressView!
  30. @IBOutlet weak var webViewContainer: UIView!
  31. private lazy var viewModel: WorkViewModel = {
  32. return WorkViewModel()
  33. }()
  34. //是否是已办
  35. open var isWorkCompeleted: Bool = false
  36. open var workId: String?
  37. var toolbarView: UIToolbar!
  38. var taskProcess = TaskProcess()
  39. let group = DispatchGroup()
  40. /// backFlag = 1来自MainTask,backFlag = 2来自TodoTask 3是show dis
  41. var backFlag: Int = 0
  42. var loadUrl: String?
  43. var isJSExecuted: Bool = true
  44. var hasToolbar: Bool = false
  45. //任务模式
  46. var todoTask: TodoTask? {
  47. didSet {
  48. var url: String?
  49. if let workCompletedId = todoTask?.workCompleted, workCompletedId != "" {
  50. url = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoedDestopQuery, parameter: ["##workCompletedId##": workCompletedId as AnyObject])
  51. self.isWorkCompeleted = true
  52. self.workId = workCompletedId
  53. } else if let workId = todoTask?.work, workId != "" {
  54. url = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoDesktopQuery, parameter: ["##workid##": workId as AnyObject])
  55. self.isWorkCompeleted = false
  56. self.workId = workId
  57. }
  58. self.loadUrl = url
  59. }
  60. }
  61. var todoData: TodoTaskData? {
  62. didSet {
  63. var url: String?
  64. if let workCompletedId = todoData?.workCompleted, workCompletedId != "" {
  65. url = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoedDestopQuery, parameter: ["##workCompletedId##": workCompletedId as AnyObject])
  66. self.isWorkCompeleted = true
  67. self.workId = workCompletedId
  68. } else if let workId = todoData?.work, workId != "" {
  69. url = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoDesktopQuery, parameter: ["##workid##": workId as AnyObject])
  70. self.isWorkCompeleted = false
  71. self.workId = workId
  72. }
  73. self.loadUrl = url
  74. }
  75. }
  76. //草稿模式
  77. var draft: ProcessDraftBean? {
  78. didSet {
  79. if let json = draft?.toJSONString() {
  80. self.loadUrl = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.todoDraftQuery, parameter: ["##draft##": json as AnyObject])
  81. }
  82. }
  83. }
  84. var myTask: [String: AnyObject]?
  85. var myRead: [String: AnyObject]?
  86. var myControl: [String: AnyObject]?
  87. var myNewControls: [WorkNewActionItem] = []
  88. var moreActionMenus: O2WorkMoreActionSheet? = nil
  89. // 上传附件使用的两个参数
  90. private var uploadSite: String = ""
  91. private var uploadParam: String = ""
  92. // 替换附件的时候需要用的附件id
  93. private var replaceAttachmentId = ""
  94. private var isReplaceAttachment = false //是替换还是上传
  95. override func viewDidLoad() {
  96. super.viewDidLoad()
  97. // NotificationCenter.default.addObserver(self, selector: #selector(showKey), name: UIResponder.keyboardDidShowNotification, object: nil)
  98. // 返回按钮重新定义
  99. self.navigationItem.hidesBackButton = true
  100. self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_fanhui"), style: .plain, target: self, action: #selector(closeForBackBtn)) // closeForBackBtn
  101. self.navigationItem.leftItemsSupplementBackButton = true
  102. // // 文档查看器
  103. // self.qlController.dataSource = qlController
  104. // self.qlController.delegate = qlController
  105. //toolbar
  106. self.toolbarView = UIToolbar(frame: CGRect(x: 0, y: self.view.height - 44, width: self.view.width, height: 44))
  107. if let todo = todoTask {
  108. if let title = todo.title, !title.trim().isEmpty {
  109. self.title = title
  110. }else if let pname = todo.processName {
  111. self.title = pname
  112. }
  113. }else if let tododata = todoData {
  114. if let title = tododata.title, !title.trim().isEmpty {
  115. self.title = title
  116. }else if let pname = tododata.processName {
  117. self.title = pname
  118. }
  119. }
  120. //添加工作页面特殊的js处理
  121. addScriptMessageHandler(key: "closeWork", handler: self)
  122. addScriptMessageHandler(key: "appFormLoaded", handler: self)
  123. addScriptMessageHandler(key: "uploadAttachment", handler: self)
  124. addScriptMessageHandler(key: "uploadAttachmentForDatagrid", handler: self)
  125. addScriptMessageHandler(key: "downloadAttachment", handler: self)
  126. addScriptMessageHandler(key: "replaceAttachment", handler: self)
  127. addScriptMessageHandler(key: "replaceAttachmentForDatagrid", handler: self)
  128. addScriptMessageHandler(key: "openDocument", handler: self)
  129. self.theWebView()
  130. //水印
  131. // let waterView = WaterMarkView.addWaterMarkView(waterMarkText: "你的姓名")
  132. // self.view.addSubview(waterView)
  133. // self.view.layer.masksToBounds = true // 裁剪 因为水印是一个很大的view
  134. }
  135. @objc private func showKey() {
  136. // if let contr = self.webView.inputAccessoryViewController, let views = contr.view.subviews {
  137. // for v in views {
  138. // DDLogDebug("view : \(v.description)")
  139. // let toolbar = findToolbar(myview: v)
  140. // if toolbar != nil && toolbar!.isKind(of: UIToolbar.self){
  141. // DDLogDebug("找到uitoolbar")
  142. // let bar = toolbar as! UIToolbar
  143. // for item in bar.items ?? [] {
  144. // DDLogDebug("设置颜色。。。。。")
  145. // item.tintColor = base_color
  146. // }
  147. // }
  148. // }
  149. // } else {
  150. // DDLogDebug("没有找到 inputAccessoryViewController")
  151. // }
  152. for window in UIApplication.shared.windows {
  153. DDLogDebug("window:\(window.description)")
  154. if window.isKind(of: UIWindow.self) {
  155. DDLogDebug("enter.........")
  156. for v in window.subviews {
  157. DDLogDebug("view : \(v.description)")
  158. if v.description.hasPrefix("<UIInputSetContainerView") {
  159. let toolbar = findToolbar(myview: v)
  160. if toolbar != nil && toolbar!.isKind(of: UIToolbar.self){
  161. DDLogDebug("找到uitoolbar")
  162. let bar = toolbar as! UIToolbar
  163. for item in bar.items ?? [] {
  164. DDLogDebug("设置颜色。。。。。")
  165. item.tintColor = base_color
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. private func findToolbar(myview: UIView) -> UIView? {
  174. for v in myview.subviews {
  175. DDLogDebug("找了:\(v.description)")
  176. if let ac = v.inputAccessoryViewController {
  177. DDLogDebug("input accessory view.........\(ac.view.description)")
  178. if ac.view.isKind(of: UIToolbar.self) {
  179. return ac.view
  180. } else {
  181. return findToolbar(myview: ac.view)
  182. }
  183. } else if let c = v.inputViewController {
  184. DDLogDebug("input view.........\(c.view.description)")
  185. if c.view.isKind(of: UIToolbar.self) {
  186. return c.view
  187. } else {
  188. return findToolbar(myview: c.view)
  189. }
  190. } else {
  191. if v.isKind(of: UIToolbar.self) {
  192. return v
  193. } else {
  194. return findToolbar(myview: v)
  195. }
  196. }
  197. }
  198. return nil
  199. }
  200. override func viewWillAppear(_ animated: Bool) {
  201. super.viewWillAppear(animated)
  202. // IQKeyboardManager.shared.enable = false
  203. //监控进度
  204. webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
  205. if #available(iOS 13.0, *) {
  206. DispatchQueue.main.async {
  207. self.navigationController?.navigationBar.setNeedsLayout()
  208. }
  209. }
  210. }
  211. override func viewWillDisappear(_ animated: Bool) {
  212. super.viewWillDisappear(animated)
  213. webView.removeObserver(self, forKeyPath: "estimatedProgress")
  214. // IQKeyboardManager.shared.enable = true
  215. }
  216. override func theWebView() {
  217. super.theWebView()
  218. self.webViewContainer.addSubview(self.webView)
  219. self.webView.translatesAutoresizingMaskIntoConstraints = false
  220. 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)
  221. 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)
  222. 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)
  223. 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)
  224. self.webViewContainer.addConstraints([top, bottom, trailing, leading])
  225. webView.navigationDelegate = self
  226. webView.uiDelegate = self
  227. DDLogDebug("url:\(String(describing: loadUrl))")
  228. if let url = loadUrl {
  229. if let urlR = URL(string: url) {
  230. let req = URLRequest(url: urlR)
  231. webView.load(req)
  232. }else {
  233. webView.loadHTMLString("<h2>没有获取到正确的URL!</h2>", baseURL: nil)
  234. }
  235. } else {
  236. webView.loadHTMLString("<h2>没有获取到正确的URL!</h2>", baseURL: nil)
  237. }
  238. webView.allowsBackForwardNavigationGestures = true
  239. }
  240. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
  241. if keyPath == "estimatedProgress" {
  242. progress.isHidden = webView.estimatedProgress == 1
  243. progress.setProgress(Float(webView.estimatedProgress), animated: true)
  244. }
  245. }
  246. override func didReceiveMemoryWarning() {
  247. super.didReceiveMemoryWarning()
  248. // Dispose of any resources that can be recreated.
  249. }
  250. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  251. if segue.identifier == "showTodoProcessSegue" {
  252. let destVC = segue.destination as! TodoTaskProcessViewController
  253. //传递到下一步
  254. destVC.backFlag = backFlag
  255. destVC.taskProcess = self.taskProcess
  256. }
  257. }
  258. /**
  259. 提交后返回此处,在此执行是返回首页还是待办处理页
  260. - parameter segue:
  261. */
  262. @IBAction func processBackMe(_ segue: UIStoryboardSegue) {
  263. goBack()
  264. }
  265. //MARK: - private func
  266. @objc func closeForBackBtn() {
  267. DDLogDebug("点击关闭按钮了。。。。。。。。。")
  268. //调用 js的 关闭当前工作的 函数 js会做新建检查工作
  269. self.webView.evaluateJavaScript(TodoTaskJS.CLOSE_WORK, completionHandler: { (data, err) in
  270. DDLogDebug("执行关闭js了。。 data:\(String(describing: data)) err:\(String(describing: err))")
  271. guard err == nil else {
  272. self.goBack()
  273. return
  274. }
  275. })
  276. }
  277. private var goBackDoing = false
  278. @objc func goBack() {
  279. if (goBackDoing) {
  280. DDLogInfo("多次执行/////////goBack()")
  281. return
  282. }
  283. goBackDoing = true
  284. DDLogDebug("backFlag = \(backFlag)")
  285. switch backFlag {
  286. case 1:
  287. self.performSegue(withIdentifier: "backMainTask", sender: nil)
  288. break
  289. case 2:
  290. self.performSegue(withIdentifier: "backToTodoTask", sender: nil)
  291. break
  292. //5是处理内容管理创建过来的流程 因为有一个创建页面 所以需要跳两层回去
  293. case 4, 5:
  294. if let index = self.navigationController?.viewControllers.firstIndex(of: self) {
  295. DDLogDebug("返回两层。。。。。")
  296. if let secVC = self.navigationController?.viewControllers.get(at: index - 2) {
  297. self.navigationController?.popToViewController(secVC, animated: true)
  298. } else {
  299. DDLogError("返回两层 错误 没有获取到VC。。。。。")
  300. self.navigationController?.popViewController(animated: true)
  301. }
  302. } else {
  303. DDLogError("返回两层 错误 当前index。。。。。")
  304. self.navigationController?.popViewController(animated: true)
  305. }
  306. break
  307. default: // 3,4都用隐藏 除非删除 删除结束有特殊处理了。
  308. self.navigationController?.popViewController(animated: true)
  309. break
  310. }
  311. }
  312. @objc func itemBtnDocDeleteAction() {
  313. DDLogDebug("btnDeleteDoc Click")
  314. showDefaultConfirm(title: "提示", message: "确认要删除这个文档吗,删除后无法恢复?", okHandler: { (action) in
  315. self.showLoading(title: "删除中...")
  316. self.viewModel.deleteWork(workId: self.workId!).then { (result) in
  317. self.showSuccess(title: "删除成功")
  318. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
  319. // 删除之后没有这个工作了,所以直接返回列表 防止返回到已办的TodoedTaskViewController
  320. if self.backFlag == 4 {
  321. self.backFlag = 2
  322. }
  323. self.goBack()
  324. })
  325. }.catch { (err) in
  326. DDLogError(err.localizedDescription)
  327. self.showError(title: "删除失败")
  328. }
  329. })
  330. }
  331. @objc func itemBtnDocSaveAction() {
  332. DDLogDebug("btnSaveDoc Click")
  333. self.showLoading(title: "保存中...")
  334. self.setupData()
  335. group.notify(queue: DispatchQueue.main) {
  336. if self.isJSExecuted {
  337. self.viewModel.saveWorkData(workId: self.taskProcess.workId!, data: self.taskProcess.businessDataDict!).then { (result) in
  338. self.showSuccess(title: "保存成功")
  339. }.catch { (err) in
  340. DDLogError(err.localizedDescription)
  341. self.showError(title: "保存失败")
  342. }
  343. } else {
  344. self.showError(title: "保存失败")
  345. }
  346. }
  347. }
  348. //提供给TodoTaskProcessViewController使用的 提交之前也要验证一次表单,根据传入的路由和意见来判断表单
  349. @objc func checkFormBeforeProcessSubmit(routeName: String, opinion: String, callback: @escaping (Bool) -> Void) {
  350. let js = "layout.appForm.formValidation('\(routeName)', '\(opinion)')"
  351. DDLogDebug("执行验证:\(js)")
  352. webView.evaluateJavaScript(js) { (data, err) in
  353. if let str = data {
  354. if str is Bool {
  355. callback((str as! Bool))
  356. } else {
  357. let isVaild = str as? String
  358. if isVaild == "true" {
  359. callback(true)
  360. } else {
  361. callback(false)
  362. }
  363. }
  364. } else {
  365. DDLogError("没有返回值。。。。。。。。。")
  366. callback(false)
  367. }
  368. }
  369. }
  370. @objc func itemBtnNextProcessAction() {
  371. DDLogDebug("btnNext Process")
  372. //校验表单
  373. webView.evaluateJavaScript(TodoTaskJS.CHECK_FORM) { (data, err) in
  374. if let str = data {
  375. let isVaild = str as! Bool
  376. if isVaild == true {
  377. self.setupData()
  378. self.group.notify(queue: DispatchQueue.main, execute: {
  379. self.performSegue(withIdentifier: "showTodoProcessSegue", sender: nil)
  380. })
  381. } else {
  382. DDLogError("表单验证失败。。。。。。。。。。。。")
  383. self.showError(title: "表单验证失败,请正确填写表单内容")
  384. }
  385. } else {
  386. DDLogError("没有返回值。。。。。。。。。")
  387. self.showError(title: "表单验证失败,请正确填写表单内容")
  388. }
  389. }
  390. }
  391. @objc func itemBtnReadDocAction() {
  392. DDLogDebug("readButtonAction")
  393. // let url = AppDelegate.o2Collect.generateURLWithAppContextKey(ReadContext.readContextKey, query: ReadContext.readProcessing, parameter: ["##id##": (todoTask?.id)! as AnyObject])
  394. // self.showLoading(title: "提交中...")
  395. // AF.request(url!, method: .post, parameters: myRead, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  396. // switch response.result {
  397. // case .success(let val):
  398. // DDLogDebug(JSON(val).description)
  399. // let json = JSON(val)
  400. // if json["type"] == "success" {
  401. // self.showSuccess(title: "提交成功")
  402. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
  403. // self.goBack()
  404. // })
  405. // } else {
  406. // DDLogError(json["message"].description)
  407. // self.showError(title: "提交失败")
  408. // }
  409. // case .failure(let err):
  410. // DDLogError(err.localizedDescription)
  411. // self.showError(title: "提交失败")
  412. // }
  413. // }
  414. }
  415. @objc func itemBtnRetractDocAction() {
  416. DDLogDebug("撤回开始。。。")
  417. // let url = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskedContext.taskedContextKey, query: TaskedContext.taskedRetractQuery, parameter: ["##work##": (self.workId)! as AnyObject])
  418. // self.showLoading(title: "提交中...")
  419. // AF.request(url!, method: .put, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
  420. // switch response.result {
  421. // case .success(let val):
  422. // DDLogDebug(JSON(val).description)
  423. // let json = JSON(val)
  424. // if json["type"] == "success" {
  425. // self.showSuccess(title: "提交成功")
  426. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
  427. // self.goBack()
  428. // })
  429. // } else {
  430. // DDLogError(json["message"].description)
  431. // self.showError(title: "提交失败")
  432. // }
  433. // case .failure(let err):
  434. // DDLogError(err.localizedDescription)
  435. // self.showError(title: "提交失败")
  436. // }
  437. // }
  438. }
  439. // 网页加载完成后,获取表单数据 判断是什么表单 待办 待阅 已办 已阅
  440. private func loadDataFromWork() {
  441. // 加载read 对象 如果是待阅工作 设置已阅时需要用到
  442. group.enter()
  443. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  444. DDLogDebug("执行 \(TodoTaskJS.DATA_READ)")
  445. self.webView.evaluateJavaScript(TodoTaskJS.DATA_READ, completionHandler: { (data, err) in
  446. if err == nil && data != nil {
  447. let json = JSON.init(parseJSON: data as! String)
  448. self.myRead = json.dictionaryObject! as [String: AnyObject]
  449. } else {
  450. DDLogError(String(describing: err))
  451. }
  452. self.group.leave()
  453. })
  454. }))
  455. // 加载control 是否能撤回
  456. group.enter()
  457. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  458. DDLogDebug("执行 \(TodoTaskJS.DATA_CONTROL)")
  459. self.webView.evaluateJavaScript(TodoTaskJS.DATA_CONTROL, completionHandler: { (data, err) in
  460. if err == nil && data != nil {
  461. let json = JSON.init(parseJSON: (data as! String))
  462. DDLogDebug("control: \(data as! String)")
  463. self.myControl = json.dictionaryObject! as [String: AnyObject]
  464. } else {
  465. DDLogError(String(describing: err))
  466. }
  467. self.group.leave()
  468. })
  469. }))
  470. group.notify(queue: DispatchQueue.main) {
  471. self.setupToolbarItems()
  472. }
  473. }
  474. //20190522 新版底部操作栏
  475. private func setupToolbarItemsNew() {
  476. var items: [UIBarButtonItem] = []
  477. let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
  478. if self.myNewControls.count > 0 {
  479. let action = self.myNewControls[0]
  480. let firstButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  481. firstButton.setTitle(action.text, for: .normal)
  482. firstButton.setTitleColor(base_color, for: .normal)
  483. firstButton.addTapGesture { (tap) in
  484. self.clickNewActionButton(action: action)
  485. }
  486. let firstButtonItem = UIBarButtonItem(customView: firstButton)
  487. items.append(spaceItem)
  488. items.append(firstButtonItem)
  489. items.append(spaceItem)
  490. }
  491. if self.myNewControls.count > 1 {
  492. let action = self.myNewControls[1]
  493. let secondButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  494. secondButton.setTitle(action.text, for: .normal)
  495. secondButton.setTitleColor(base_color, for: .normal)
  496. secondButton.addTapGesture { (tap) in
  497. self.clickNewActionButton(action: action)
  498. }
  499. let secondButtonItem = UIBarButtonItem(customView: secondButton)
  500. items.append(spaceItem)
  501. items.append(secondButtonItem)
  502. items.append(spaceItem)
  503. }
  504. // 更多按钮
  505. if self.myNewControls.count > 2 {
  506. let moreButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  507. moreButton.setImage(UIImage(named: "icon_more_s"), for: .normal)
  508. moreButton.addTapGesture { (tap) in
  509. self.moreActionMenus?.show()
  510. }
  511. let moreButtonItem = UIBarButtonItem(customView: moreButton)
  512. items.append(moreButtonItem)
  513. self.moreActionMenus = O2WorkMoreActionSheet(moreControls: self.myNewControls) { item in
  514. self.clickNewActionButton(action: item)
  515. }
  516. }
  517. if items.count > 0 {
  518. self.layoutBottomBar(items: items)
  519. }
  520. }
  521. //新版操作按钮点击动作
  522. private func clickNewActionButton(action: WorkNewActionItem) {
  523. DDLogDebug("click .....\(action.text)")
  524. let actionScript = action.actionScript
  525. if actionScript != "" {
  526. let jsExc = "layout.app.appForm._runCustomAction(\(actionScript))"
  527. DDLogDebug(jsExc)
  528. DispatchQueue.main.async {
  529. self.webView.evaluateJavaScript(jsExc) { (data, err) in
  530. DDLogDebug("actionScript excute finish!!!!")
  531. }
  532. }
  533. } else {
  534. let control = action.control
  535. switch control {
  536. case "allowDelete":
  537. self.itemBtnDocDeleteAction()
  538. break
  539. case "allowSave":
  540. self.itemBtnDocSaveAction()
  541. break
  542. case "allowProcessing":
  543. self.itemBtnNextProcessAction()
  544. break
  545. case "allowReadProcessing":
  546. self.itemBtnReadDocAction()
  547. break
  548. case "allowRetract":
  549. self.itemBtnRetractDocAction()
  550. break
  551. default:
  552. let jsExc = "layout.app.appForm[\"\(action.action)\"]()"
  553. DDLogDebug(jsExc)
  554. DispatchQueue.main.async {
  555. self.webView.evaluateJavaScript(jsExc) { (data, err) in
  556. DDLogDebug("actionScript excute finish!!!!")
  557. }
  558. }
  559. }
  560. }
  561. }
  562. private func setupToolbarItems() {
  563. DDLogDebug("setupToolbarItems 处理底部按钮, 根据control")
  564. if self.myNewControls.count > 0 { //新版操作按钮
  565. self.setupToolbarItemsNew()
  566. } else {
  567. var items: [UIBarButtonItem] = []
  568. if self.myControl != nil {
  569. let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
  570. if let allowDelete = self.myControl!["allowDelete"] as? Bool {
  571. if allowDelete { //删除工作
  572. DDLogDebug("删除工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  573. let deleteBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  574. deleteBtn.setTitle("删除", for: .normal)
  575. deleteBtn.setTitleColor(base_color, for: .normal)
  576. deleteBtn.addTapGesture { (tap) in
  577. self.itemBtnDocDeleteAction()
  578. }
  579. let deleteItem = UIBarButtonItem(customView: deleteBtn)
  580. items.append(spaceItem)
  581. items.append(deleteItem)
  582. items.append(spaceItem)
  583. }
  584. }
  585. if let allowSave = self.myControl!["allowSave"] as? Bool {
  586. if allowSave { // 保存工作
  587. DDLogDebug("保存工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  588. let saveBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  589. saveBtn.setTitle("保存", for: .normal)
  590. saveBtn.setTitleColor(base_color, for: .normal)
  591. saveBtn.addTapGesture { (tap) in
  592. self.itemBtnDocSaveAction()
  593. }
  594. let saveItem = UIBarButtonItem(customView: saveBtn)
  595. items.append(spaceItem)
  596. items.append(saveItem)
  597. items.append(spaceItem)
  598. }
  599. }
  600. if let allowProcessing = self.myControl!["allowProcessing"] as? Bool {
  601. if allowProcessing { // 待办工作
  602. DDLogDebug("待办工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  603. let processingBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  604. processingBtn.setTitle("继续流转", for: .normal)
  605. processingBtn.setTitleColor(base_color, for: .normal)
  606. processingBtn.addTapGesture { (tap) in
  607. self.itemBtnNextProcessAction()
  608. }
  609. let processingItem = UIBarButtonItem(customView: processingBtn)
  610. items.append(spaceItem)
  611. items.append(processingItem)
  612. items.append(spaceItem)
  613. }
  614. }
  615. if let allowReadProcessing = self.myControl!["allowReadProcessing"] as? Bool {
  616. if allowReadProcessing { // 待阅 工作
  617. DDLogDebug("待阅工作。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  618. let readBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  619. readBtn.setTitle("已阅", for: .normal)
  620. readBtn.setTitleColor(base_color, for: .normal)
  621. readBtn.addTapGesture { (tap) in
  622. self.itemBtnReadDocAction()
  623. }
  624. let readItem = UIBarButtonItem(customView: readBtn)
  625. items.append(spaceItem)
  626. items.append(readItem)
  627. items.append(spaceItem)
  628. }
  629. }
  630. if let allowRetract = self.myControl!["allowRetract"] as? Bool {
  631. if allowRetract { // 撤回
  632. DDLogDebug("可以撤回。。。。。。。。。。。。。。。。。。。。。。安装按钮")
  633. let retractBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  634. retractBtn.setTitle("撤回", for: .normal)
  635. retractBtn.setTitleColor(base_color, for: .normal)
  636. retractBtn.addTapGesture { (tap) in
  637. self.itemBtnRetractDocAction()
  638. }
  639. let retractItem = UIBarButtonItem(customView: retractBtn)
  640. items.append(spaceItem)
  641. items.append(retractItem)
  642. items.append(spaceItem)
  643. }
  644. }
  645. self.layoutBottomBar(items: items)
  646. NSLog("\(self.view.subviews)");
  647. } else {
  648. DDLogError("没有control 数据异常 按钮无法计算。。。。")
  649. }
  650. }
  651. }
  652. private func layoutBottomBar(items: [UIBarButtonItem]) {
  653. if items.count > 0 {
  654. self.toolbarView.items = items
  655. self.hasToolbar = true
  656. self.view.addSubview(self.toolbarView)
  657. self.toolbarView.translatesAutoresizingMaskIntoConstraints = false
  658. 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)
  659. self.toolbarView.addConstraint(heightC)
  660. 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)
  661. 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)
  662. 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)
  663. self.view.addConstraints([bottom, leading, trailing])
  664. self.view.constraints.forEach { (constraint) in
  665. if constraint.identifier == "webViewBottomConstraint" {
  666. self.view.removeConstraint(constraint)
  667. }
  668. }
  669. 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)
  670. self.view.addConstraint(webcTop)
  671. self.view.layoutIfNeeded()
  672. }
  673. }
  674. /**
  675. * 读取从页面载入的业务及流程数据,建立数据模型
  676. */
  677. func setupData() {
  678. group.enter()
  679. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  680. DDLogInfo("opinion queue .....")
  681. self.webView.evaluateJavaScript(TodoTaskJS.DATA_OPINION, completionHandler: { (data, err) in
  682. if err == nil && data != nil {
  683. let opinion = data as! String
  684. DDLogInfo("opinion: \(opinion)")
  685. if opinion == "\"\"" {
  686. self.taskProcess.opinion = ""
  687. } else {
  688. let json = JSON.init(parseJSON: opinion)
  689. let oJson = json.dictionaryObject as [String: AnyObject]?
  690. let op = oJson?["opinion"] as? String
  691. self.taskProcess.opinion = op
  692. }
  693. } else {
  694. DDLogError(String(describing: err))
  695. }
  696. self.group.leave()
  697. })
  698. }))
  699. group.enter()
  700. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  701. DDLogDebug("taskQueue 1")
  702. self.webView.evaluateJavaScript(TodoTaskJS.DATA_TASK) { (data, err) in
  703. if err == nil && data != nil {
  704. DDLogDebug("taskQueue complete")
  705. let json = JSON.init(parseJSON: data as! String)
  706. self.taskProcess.taskDict = json.dictionaryObject! as [String: AnyObject]
  707. self.taskProcess.taskId = self.taskProcess.taskDict!["id"] as? String
  708. self.taskProcess.decisonList = self.taskProcess.taskDict!["routeNameList"] as? [String]
  709. } else {
  710. DDLogError(String(describing: err))
  711. self.isJSExecuted = false
  712. }
  713. self.group.leave()
  714. }
  715. }))
  716. group.enter()
  717. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  718. DDLogDebug("workQueue 1")
  719. self.webView.evaluateJavaScript(TodoTaskJS.DATA_WORK) { (data, err) in
  720. if err == nil && data != nil {
  721. DDLogDebug("workQueue complete")
  722. let json = JSON.init(parseJSON: data as! String)
  723. self.taskProcess.workDict = json.dictionaryObject! as [String: AnyObject]
  724. self.taskProcess.workId = self.taskProcess.workDict!["id"] as? String
  725. } else {
  726. DDLogError(String(describing: err))
  727. self.isJSExecuted = false
  728. }
  729. self.group.leave()
  730. }
  731. }))
  732. group.enter()
  733. DispatchQueue.main.async(group: group, execute: DispatchWorkItem(block: {
  734. DDLogDebug("businessQueue 1")
  735. self.webView.evaluateJavaScript(TodoTaskJS.DATA_BUSINESS) { (data, err) in
  736. if err == nil && data != nil {
  737. DDLogDebug("businessQueue complete")
  738. let json = JSON.init(parseJSON: data as! String)
  739. self.taskProcess.businessDataDict = json.dictionaryObject! as [String: AnyObject]
  740. //do {
  741. //}catch{
  742. //DDLogError("set routeNameList Error")
  743. //}
  744. } else {
  745. DDLogError(String(describing: err))
  746. self.isJSExecuted = false
  747. }
  748. self.group.leave()
  749. }
  750. }))
  751. }
  752. // ios系统文件选择器
  753. private func chooseFileWithDocumentPicker() {
  754. let documentTypes = ["public.content",
  755. "public.text",
  756. "public.source-code",
  757. "public.image",
  758. "public.audiovisual-content",
  759. "com.adobe.pdf",
  760. "com.apple.keynote.key",
  761. "com.microsoft.word.doc",
  762. "com.microsoft.excel.xls",
  763. "com.microsoft.powerpoint.ppt"]
  764. let picker = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
  765. picker.delegate = self
  766. self.present(picker, animated: true, completion: nil)
  767. }
  768. // 相册选择图片
  769. private func chooseImageWithBSImagePicker() {
  770. self.choosePhotoWithImagePicker { (fileName, imageData) in
  771. if self.isReplaceAttachment {
  772. self.replaceAttachmentToO2OA(data: imageData, fileName: fileName)
  773. } else {
  774. self.uploadAttachmentToO2OA(data: imageData, fileName: fileName)
  775. }
  776. }
  777. }
  778. // 上传文件到o2oa服务器
  779. private func uploadAttachmentToO2OA(data: Data, fileName: String) {
  780. DDLogInfo("开始上传附件,site: \(self.uploadSite) param:\(self.uploadParam)")
  781. DispatchQueue.main.async {
  782. self.showLoading(title: "上传中...")
  783. }
  784. self.viewModel.uploadAttachment(workId: self.workId!, site: self.uploadSite, fileName: fileName, fileData: data).then { (idData) in
  785. DispatchQueue.main.async {
  786. if self.uploadParam != "" {
  787. DDLogDebug("执行了 layout.appForm.uploadedAttachmentDatagrid")
  788. let callJS = "layout.appForm.uploadedAttachmentDatagrid(\"\(self.uploadSite)\", \"\(idData.id ?? "")\", \"\(self.uploadParam)\")"
  789. self.webView.evaluateJavaScript(callJS, completionHandler: { (result, err) in
  790. })
  791. } else {
  792. DDLogDebug("执行了 layout.appForm.uploadedAttachment")
  793. let callJS = "layout.appForm.uploadedAttachment(\"\(self.uploadSite)\", \"\(idData.id ?? "")\")"
  794. self.webView.evaluateJavaScript(callJS, completionHandler: { (result, err) in
  795. })
  796. }
  797. self.showSuccess(title: "上传成功")
  798. self.uploadSite = ""
  799. self.uploadParam = ""
  800. }
  801. }.catch { (err) in
  802. DDLogError(err.localizedDescription)
  803. self.showError(title: "上传失败")
  804. self.uploadSite = ""
  805. self.uploadParam = ""
  806. }
  807. }
  808. // 替换附件的时候 上传附件到服务器
  809. private func replaceAttachmentToO2OA(data: Data, fileName: String) {
  810. DispatchQueue.main.async {
  811. self.showLoading(title: "上传中...")
  812. }
  813. self.viewModel.replaceAttachment(id: self.replaceAttachmentId, workId: self.workId!, site: self.uploadSite, fileName: fileName, fileData: data).then { (idData) in
  814. DispatchQueue.main.async {
  815. if self.uploadParam == "" {
  816. let callJS = "layout.appForm.replacedAttachment(\"\(self.uploadSite)\", \"\(self.replaceAttachmentId)\")"
  817. self.webView.evaluateJavaScript(callJS, completionHandler: { (result, err) in
  818. })
  819. } else {
  820. let callJS = "layout.appForm.replacedAttachmentDatagrid(\"\(self.uploadSite)\", \"\(self.replaceAttachmentId)\", \"\(self.uploadParam)\")"
  821. self.webView.evaluateJavaScript(callJS, completionHandler: { (result, err) in
  822. })
  823. }
  824. self.showSuccess(title: "替换成功")
  825. self.uploadSite = ""
  826. self.uploadParam = ""
  827. self.replaceAttachmentId = ""
  828. }
  829. }.catch { (err) in
  830. DispatchQueue.main.async {
  831. DDLogError(err.localizedDescription)
  832. self.showError(title: "替换失败")
  833. self.uploadSite = ""
  834. self.uploadParam = ""
  835. self.replaceAttachmentId = ""
  836. }
  837. }
  838. }
  839. }
  840. //MARK: - extension
  841. extension TodoTaskDetailViewController: WKNavigationDelegate, WKUIDelegate {
  842. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  843. DDLogDebug("didStartProvisionalNavigation")
  844. }
  845. func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
  846. DDLogDebug("didCommit")
  847. }
  848. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  849. DDLogDebug("didFinish")
  850. }
  851. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  852. DDLogDebug("didFail")
  853. DDLogError(error.localizedDescription)
  854. self.showError(title: "工作加载异常!")
  855. }
  856. func webViewDidClose(_ webView: WKWebView) {
  857. DDLogInfo("h5执行了window.close()")
  858. self.goBack()
  859. }
  860. }
  861. // MARK: - ios系统文件选择器delegate
  862. extension TodoTaskDetailViewController: UIDocumentPickerDelegate {
  863. func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  864. if urls.count > 0 {
  865. let file = urls[0] //单选
  866. if file.startAccessingSecurityScopedResource() { //访问权限
  867. let fileName = file.lastPathComponent
  868. if let data = try? Data(contentsOf: file) {
  869. if self.isReplaceAttachment {
  870. self.replaceAttachmentToO2OA(data: data, fileName: fileName)
  871. } else {
  872. self.uploadAttachmentToO2OA(data: data, fileName: fileName)
  873. }
  874. } else {
  875. self.showError(title: "读取文件失败")
  876. }
  877. } else {
  878. self.showError(title: "没有获取文件的权限")
  879. }
  880. }
  881. }
  882. }
  883. // MARK: - 拍照delegate
  884. extension TodoTaskDetailViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
  885. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
  886. if let image = info[.editedImage] as? UIImage {
  887. let fileName = "\(UUID().uuidString).png"
  888. let newData = image.pngData()!
  889. if self.isReplaceAttachment {
  890. self.replaceAttachmentToO2OA(data: newData, fileName: fileName)
  891. } else {
  892. self.uploadAttachmentToO2OA(data: newData, fileName: fileName)
  893. }
  894. } else {
  895. DDLogError("没有选择到图片!")
  896. }
  897. picker.dismiss(animated: true, completion: nil)
  898. }
  899. }
  900. // MARK: - O2WKScriptMessageHandlerImplement
  901. extension TodoTaskDetailViewController: O2WKScriptMessageHandlerImplement {
  902. func userController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  903. let name = message.name
  904. switch name {
  905. case "closeWork":
  906. DDLogError("执行了closeWork。。。。。。。。。。")
  907. self.goBack()
  908. break
  909. case "appFormLoaded":
  910. DDLogDebug("appFormLoaded 当前方法已经弃用。。。。")
  911. // if let newControls = (message.body as? NSString) {
  912. // let str = newControls as String
  913. // DDLogDebug("appFormLoaded , controls :\(str)")
  914. // if str != "true" {
  915. // myNewControls.removeAll()
  916. // if let controls = [WorkNewActionItem].deserialize(from: str) {
  917. // controls.forEach { (item) in
  918. // if item != nil {
  919. // myNewControls.append(item!)
  920. // }
  921. // }
  922. // }
  923. // }
  924. // }
  925. // self.loadDataFromWork()
  926. break
  927. case "uploadAttachment":
  928. self.uploadSite = ""
  929. self.uploadParam = ""
  930. ZonePermissions.requestImagePickerAuthorization(callback: { (zoneStatus) in
  931. if zoneStatus == ZoneAuthorizationStatus.zAuthorizationStatusAuthorized {
  932. // let site = (message.body as! NSDictionary)["site"]
  933. if let body = (message.body as? NSDictionary), let site = body["site"] as? String {
  934. self.uploadAttachment(site)
  935. }else {
  936. self.showError(title: "参数传入错误,无法上传!")
  937. }
  938. } else {
  939. //显示
  940. self.gotoApplicationSettings(alertMessage: "需要照片允许访问权限,是否跳转到手机设置页面开启相机权限?")
  941. }
  942. })
  943. break
  944. case "uploadAttachmentForDatagrid":
  945. DDLogDebug("进入了 uploadAttachmentForDatagrid")
  946. ZonePermissions.requestImagePickerAuthorization(callback: { (zoneStatus) in
  947. if zoneStatus == ZoneAuthorizationStatus.zAuthorizationStatusAuthorized {
  948. // let site = (message.body as! NSDictionary)["site"]
  949. if let body = (message.body as? NSDictionary), let site = body["site"] as? String, let param = body["param"] as? String {
  950. self.uploadAttachment(site, param: param)
  951. }else {
  952. self.showError(title: "参数传入错误,无法上传!")
  953. }
  954. } else {
  955. //显示
  956. self.gotoApplicationSettings(alertMessage: "需要照片允许访问权限,是否跳转到手机设置页面开启相机权限?")
  957. }
  958. })
  959. break
  960. case "downloadAttachment":
  961. // let attachmentId = (message.body as! NSDictionary)["id"]
  962. if let body = (message.body as? NSDictionary), let attachmentId = body["id"] as? String {
  963. self.downloadAttachment(attachmentId)
  964. }else {
  965. self.showError(title: "参数传入错误,无法预览!")
  966. }
  967. break
  968. case "replaceAttachment":
  969. // let attachmentId = (message.body as! NSDictionary)["id"] as! String
  970. // let site = (message.body as! NSDictionary)["site"] as? String
  971. if let body = (message.body as? NSDictionary), let attachmentId = body["id"] as? String, let site = body["site"] as? String {
  972. self.replaceAttachment(attachmentId, site)
  973. }else {
  974. self.showError(title: "参数传入错误,无法替换!")
  975. }
  976. break
  977. case "replaceAttachmentForDatagrid":
  978. if let body = (message.body as? NSDictionary), let attachmentId = body["id"] as? String, let site = body["site"] as? String, let param = body["param"] as? String {
  979. self.replaceAttachment(attachmentId, site, param: param)
  980. }else {
  981. self.showError(title: "参数传入错误,无法替换!")
  982. }
  983. break
  984. case "openDocument":
  985. let url = (message.body as! NSString)
  986. self.downloadDocumentAndPreview(String(url))
  987. break
  988. default:
  989. DDLogError("未知方法名:\(name)!")
  990. break
  991. }
  992. }
  993. //上传附件
  994. private func uploadAttachment(_ site: String, param: String = "") {
  995. DDLogDebug("uploadAttachment site: \(site) param: \(param)")
  996. self.uploadSite = site
  997. self.uploadParam = param
  998. let arr = [
  999. UIAlertAction(title: "文件", style: .default, handler: { (action) in
  1000. self.isReplaceAttachment = false
  1001. self.chooseFileWithDocumentPicker()
  1002. }),
  1003. UIAlertAction(title: "照片", style: .default, handler: { (action) in
  1004. self.isReplaceAttachment = false
  1005. self.chooseImageWithBSImagePicker()
  1006. }),
  1007. UIAlertAction(title: "拍照", style: .default, handler: { (action) in
  1008. self.isReplaceAttachment = false
  1009. self.takePhoto(delegate: self)
  1010. }),
  1011. ]
  1012. self.showSheetAction(title: L10n.alert, message: "请选择入口", actions: arr)
  1013. //
  1014. //
  1015. // self.choosePhotoWithImagePicker { (fileName, imageData) in
  1016. // DispatchQueue.main.async {
  1017. // self.showLoading(title: "上传中...")
  1018. // }
  1019. // self.viewModel.uploadAttachment(workId: self.workId!, site: site, fileName: fileName, fileData: imageData).then { (idData) in
  1020. // DispatchQueue.main.async {
  1021. // //ProgressHUD.showSuccess("上传成功")
  1022. // if param != "" {
  1023. // DDLogDebug("执行了 layout.appForm.uploadedAttachmentDatagrid")
  1024. // let callJS = "layout.appForm.uploadedAttachmentDatagrid(\"\(site)\", \"\(idData.id ?? "")\", \"\(param)\")"
  1025. // self.webView.evaluateJavaScript(callJS, completionHandler: { (result, err) in
  1026. //
  1027. // })
  1028. // } else {
  1029. // DDLogDebug("执行了 layout.appForm.uploadedAttachment")
  1030. // let callJS = "layout.appForm.uploadedAttachment(\"\(site)\", \"\(idData.id ?? "")\")"
  1031. // self.webView.evaluateJavaScript(callJS, completionHandler: { (result, err) in
  1032. //
  1033. // })
  1034. // }
  1035. //
  1036. // self.showSuccess(title: "上传成功")
  1037. // }
  1038. // }.catch { (err) in
  1039. // DDLogError(err.localizedDescription)
  1040. // self.showError(title: "上传失败")
  1041. // }
  1042. // }
  1043. }
  1044. /// 替换附件
  1045. private func replaceAttachment(_ attachmentId: String, _ site: String, param: String = "") {
  1046. DDLogInfo("replaceAttachment attachmentId: \(attachmentId) site: \(site) param: \(param)")
  1047. self.replaceAttachmentId = attachmentId
  1048. self.uploadSite = site
  1049. self.uploadParam = param
  1050. let arr = [
  1051. UIAlertAction(title: "文件", style: .default, handler: { (action) in
  1052. self.isReplaceAttachment = true
  1053. self.chooseFileWithDocumentPicker()
  1054. }),
  1055. UIAlertAction(title: "照片", style: .default, handler: { (action) in
  1056. self.isReplaceAttachment = true
  1057. self.chooseImageWithBSImagePicker()
  1058. }),
  1059. UIAlertAction(title: "拍照", style: .default, handler: { (action) in
  1060. self.isReplaceAttachment = true
  1061. self.takePhoto(delegate: self)
  1062. }),
  1063. ]
  1064. self.showSheetAction(title: L10n.alert, message: "请选择入口", actions: arr)
  1065. }
  1066. //下载预览附件
  1067. private func downloadAttachment(_ attachmentId: String) {
  1068. if isWorkCompeleted {
  1069. self.showLoading()
  1070. self.viewModel.getWorkcompletedAttachment(workcompleted: self.workId!, id: attachmentId).then { path in
  1071. self.hideLoading()
  1072. self.previewDoc(path: path)
  1073. }.catch { (err) in
  1074. DDLogError(err.localizedDescription)
  1075. DispatchQueue.main.async {
  1076. self.showError(title: "预览文件出错")
  1077. }
  1078. }
  1079. } else {
  1080. self.showLoading()
  1081. self.viewModel.getWorkAttachment(workId: self.workId!, id: attachmentId).then { path in
  1082. self.hideLoading()
  1083. self.previewDoc(path: path)
  1084. }.catch { (err) in
  1085. DDLogError(err.localizedDescription)
  1086. DispatchQueue.main.async {
  1087. self.showError(title: "预览文件出错")
  1088. }
  1089. }
  1090. }
  1091. }
  1092. /**
  1093. * 下载公文 并阅览
  1094. **/
  1095. private func downloadDocumentAndPreview(_ url: String) {
  1096. DDLogDebug("文档预览地址:\(url)")
  1097. self.showLoading()
  1098. // 文件地址
  1099. let localFileDestination: DownloadRequest.Destination = { _, response in
  1100. let documentsURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  1101. let fileURL = documentsURL.appendingPathComponent(response.suggestedFilename!)
  1102. // 有重名文件就删除重建
  1103. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  1104. }
  1105. AF.download(url, to: localFileDestination).response(completionHandler: { (response) in
  1106. if response.error == nil, let fileurl = response.fileURL?.path {
  1107. DDLogDebug("文件地址:\(fileurl)")
  1108. let newUrl = self.dealDocFileSaveAsDocx(fileUrl: response.fileURL!)
  1109. DDLogDebug("处理过的文件地址:\(newUrl.path)")
  1110. //打开文件
  1111. self.hideLoading()
  1112. self.previewDoc(path: newUrl.path)
  1113. } else {
  1114. let msg = response.error?.localizedDescription ?? ""
  1115. DDLogError("下载文件出错,\(msg)")
  1116. DispatchQueue.main.async {
  1117. self.showError(title: "预览文件出错")
  1118. }
  1119. }
  1120. })
  1121. }
  1122. //
  1123. //
  1124. // private func previewAttachment(_ url: String) {
  1125. // self.previewDoc(path: <#T##String#>)
  1126. // let currentURL = NSURL(fileURLWithPath: url)
  1127. // if QLPreviewController.canPreview(currentURL) {
  1128. // qlController.currentFileURLS.removeAll(keepingCapacity: true)
  1129. // qlController.currentFileURLS.append(currentURL)
  1130. // qlController.reloadData()
  1131. // if #available(iOS 10, *) {
  1132. // let navVC = ZLNormalNavViewController(rootViewController: qlController)
  1133. // qlController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: qlController, action: #selector(qlController.qlCloseWindow))
  1134. // self.presentVC(navVC)
  1135. // } else {
  1136. // self.pushVC(qlController)
  1137. // }
  1138. //
  1139. //
  1140. // } else {
  1141. // self.showError(title: "此文件无法预览,请在PC端查看")
  1142. // }
  1143. //
  1144. // }
  1145. //处理特殊情况 docx的文件有可能是doc 需要判断下文件信息头
  1146. private func dealDocFileSaveAsDocx(fileUrl: URL) -> URL {
  1147. if fileUrl.pathExtension == "docx" {
  1148. if let data = try? Data(contentsOf: fileUrl) {
  1149. let mimeType = Swime.mimeType(data: data)
  1150. if mimeType?.type == .msi {
  1151. let newURL = fileUrl.appendingPathExtension("doc")
  1152. do {
  1153. DDLogDebug("copy 了一个 文件。。。。。。")
  1154. try FileManager.default.copyItem(at: fileUrl, to: newURL)
  1155. return newURL
  1156. } catch {
  1157. DDLogError(error.localizedDescription)
  1158. }
  1159. }
  1160. }
  1161. }
  1162. return fileUrl
  1163. }
  1164. }