CMSItemDetailViewController.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // CMSItemDetailViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/12/9.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import Alamofire
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import QuickLook
  15. import CocoaLumberjack
  16. class CMSItemDetailViewController: BaseWebViewUIViewController {
  17. private let qlController = QLPreviewController()
  18. private var zonePickerView:ZonePickerView!
  19. fileprivate var currentFileURLS:[NSURL] = []
  20. private var window:UIWindow?
  21. var itemData:CMSCategoryItemData? {
  22. didSet {
  23. title = itemData?.title
  24. itemUrl = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.cmsItemDetailQuery, parameter: ["##documentId##":itemData?.id as AnyObject])!
  25. attachmentListUrl = AppDelegate.o2Collect.generateURLWithAppContextKey(CMSContext.cmsContextKey, query:CMSContext.cmsAttachmentListQuery, parameter: ["##documentId##":itemData?.id as AnyObject])!
  26. }
  27. }
  28. var documentId:String?{
  29. didSet {
  30. itemUrl = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.cmsItemDetailQuery, parameter: ["##documentId##":documentId as AnyObject])!
  31. attachmentListUrl = AppDelegate.o2Collect.generateURLWithAppContextKey(CMSContext.cmsContextKey, query:CMSContext.cmsAttachmentListQuery, parameter: ["##documentId##":documentId as AnyObject])!
  32. }
  33. }
  34. var publishItemInfo:CMSPublishInfo! {
  35. didSet {
  36. if let datas = publishItemInfo.data {
  37. self.window?.isHidden = datas.count > 0 ? false : true
  38. for infoData in datas {
  39. let m = ZonePickerModel()
  40. m.id = infoData.id
  41. m.name = infoData.name
  42. m.sourceObj = infoData
  43. attachModels.append(m)
  44. }
  45. }
  46. }
  47. }
  48. var attachModels:[ZonePickerModel] = []
  49. var attachmentListUrl = ""
  50. var itemUrl = ""
  51. override func viewWillAppear(_ animated: Bool) {
  52. super.viewWillAppear(animated)
  53. NotificationCenter.default.addObserver(self, selector: #selector(showAttachViewInController(_:)), name: NSNotification.Name("SHOW_ATTACH_OBJ"), object: nil)
  54. NotificationCenter.default.addObserver(self, selector: #selector(quitAttachViewInController(_:)), name: NSNotification.Name("QUIT_ATTACH_OBJ"), object: nil)
  55. }
  56. override func viewWillDisappear(_ animated: Bool) {
  57. super.viewWillDisappear(animated)
  58. NotificationCenter.default.removeObserver(self)
  59. self.window?.isHidden = true
  60. }
  61. override func viewDidLoad() {
  62. super.viewDidLoad()
  63. createButton()
  64. loadAttachmentList()
  65. theWebView()
  66. qlInit()
  67. }
  68. override func didReceiveMemoryWarning() {
  69. super.didReceiveMemoryWarning()
  70. // Dispose of any resources that can be recreated.
  71. }
  72. override func theWebView(){
  73. super.theWebView()
  74. //注入回复的回复函数
  75. webView.navigationDelegate = self
  76. webView.uiDelegate = self
  77. webView.allowsBackForwardNavigationGestures = true
  78. loadItemDetail()
  79. }
  80. private func qlInit(){
  81. // qlController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(qlCloseWindow))
  82. self.qlController.delegate = self
  83. self.qlController.dataSource = self
  84. }
  85. private func loadAttachmentList(){
  86. Alamofire.request(attachmentListUrl, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  87. switch response.result {
  88. case .success(let val):
  89. self.publishItemInfo = Mapper<CMSPublishInfo>().map(JSONObject: val)!
  90. case .failure(let err):
  91. DDLogError(err.localizedDescription)
  92. }
  93. }
  94. }
  95. private func loadItemDetail() {
  96. webView.load(Alamofire.request(itemUrl).request!)
  97. }
  98. @objc private func qlCloseWindow(){
  99. self.dismiss(animated: true, completion: {
  100. })
  101. }
  102. @objc private func quitAttachViewInController(_ noti:NSNotification){
  103. self.window?.isHidden = false
  104. }
  105. @objc private func showAttachViewInController(_ noti:NSNotification){
  106. if let obj = noti.object {
  107. let m = obj as! ZonePickerModel
  108. self.downloadFile(m, { (url) in
  109. //self.performSegue(withIdentifier: "showInQL", sender: url)
  110. self.currentFileURLS.removeAll(keepingCapacity: true)
  111. let currentURL = NSURL(fileURLWithPath: url)
  112. if QLPreviewController.canPreview(currentURL) {
  113. self.currentFileURLS.append(currentURL)
  114. self.qlController.reloadData()
  115. if #available(iOS 10, *) {
  116. let navVC = ZLNormalNavViewController(rootViewController: self.qlController)
  117. self.qlController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(self.qlCloseWindow))
  118. self.presentVC(navVC)
  119. }else{
  120. //if #available(iOS 9, *){
  121. self.zonePickerView.hidePickerView()
  122. let prController = CMSQLViewController()
  123. prController.delegate = self
  124. prController.dataSource = self
  125. self.pushVC(prController)
  126. //}
  127. }
  128. }
  129. })
  130. }
  131. }
  132. private func downloadFile(_ model:ZonePickerModel,_ completed:@escaping (_ localURLForFile:String) -> Void){
  133. let downURL = AppDelegate.o2Collect.generateURLWithAppContextKey(CMSContext.cmsContextKey, query: CMSContext.cmsAttachmentDownloadQuery, parameter: ["##id##":model.id as AnyObject])!
  134. let destination: DownloadRequest.DownloadFileDestination = { _, _ in
  135. let documentsURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  136. let fileURL = documentsURL.appendingPathComponent(model.name)
  137. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  138. }
  139. ProgressHUD.show("downloading...", interaction: false)
  140. Alamofire.download(downURL, to: destination).response { response in
  141. print(response)
  142. if response.error == nil, let filePath = response.destinationURL?.path {
  143. DispatchQueue.main.async {
  144. ProgressHUD.dismiss()
  145. }
  146. completed(filePath)
  147. }else{
  148. DispatchQueue.main.async {
  149. ProgressHUD.showError("文件下载出错")
  150. }
  151. }
  152. }
  153. }
  154. //创建一个显示列表附件的按钮
  155. private func createButton(){
  156. let width = SCREEN_WIDTH
  157. let height = SCREEN_HEIGHT
  158. let button = UIButton(frame: CGRect(x: 0,y: 0,width:30,height: 30))
  159. button.setImage(UIImage(named: "icon_attach"), for:.normal)
  160. button.addTarget(self, action: #selector(showAttachmentList(_:)), for: .touchUpInside)
  161. self.window = UIWindow(frame: CGRect(x: (width - 30) / 2, y: height - 60, width: 30, height: 30))
  162. self.window?.windowLevel = UIWindow.Level.alert + 1
  163. self.window?.backgroundColor = UIColor.white
  164. self.window?.layer.cornerRadius = 15
  165. self.window?.layer.masksToBounds = true
  166. self.window?.addSubview(button)
  167. self.window?.makeKeyAndVisible()
  168. }
  169. @objc func showAttachmentList(_ sender:UIButton){
  170. print("List AttachList")
  171. self.window?.isHidden = true
  172. self.zonePickerView = ZonePickerView()
  173. zonePickerView.models = attachModels
  174. zonePickerView.showPickerView()
  175. }
  176. }
  177. extension CMSItemDetailViewController:WKNavigationDelegate,WKUIDelegate {
  178. func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
  179. DDLogDebug("didFailProvisionalNavigation \(navigation) error = \(error)")
  180. }
  181. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  182. DDLogDebug("didStartProvisionalNavigation \(navigation)")
  183. }
  184. func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
  185. DDLogDebug("didCommit")
  186. }
  187. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  188. DDLogDebug("didFinish")
  189. //self.setupData()
  190. }
  191. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  192. DDLogDebug("didFail")
  193. DDLogError(error.localizedDescription)
  194. }
  195. }
  196. extension CMSItemDetailViewController:QLPreviewControllerDataSource,QLPreviewControllerDelegate{
  197. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  198. return self.currentFileURLS.count
  199. }
  200. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  201. return self.currentFileURLS[index]
  202. }
  203. func previewControllerWillDismiss(_ controller: QLPreviewController) {
  204. guard #available(iOS 10,*) else{
  205. self.showAttachmentList(UIButton(type: .custom))
  206. return
  207. }
  208. }
  209. }