BBSSubjectDetailViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // BBSSubjectDetailViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/11/8.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import Alamofire
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import CocoaLumberjack
  14. class BBSSubjectDetailViewController: BaseWebViewUIViewController {
  15. @IBOutlet weak var progressView: UIProgressView!
  16. @IBOutlet weak var webViewContainer: UIView!
  17. @IBOutlet weak var attachmentBtn: UIButton!
  18. @IBOutlet weak var replyBtn: UIButton!
  19. var loadUrl:String?
  20. var subject:BBSSubjectData? {
  21. didSet {
  22. loadUrl = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.bbsItemDetailQuery, parameter: ["##subjectId##":subject?.id as AnyObject])
  23. }
  24. }
  25. private lazy var viewModel: BBSViewModel = {
  26. return BBSViewModel()
  27. }()
  28. private var attachmentList: [O2BBSSubjectAttachmentInfo] = []
  29. override func viewWillAppear(_ animated: Bool) {
  30. //监控进度
  31. webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
  32. }
  33. override func viewWillDisappear(_ animated: Bool) {
  34. webView.removeObserver(self, forKeyPath: "estimatedProgress")
  35. }
  36. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  37. if keyPath == "estimatedPrgress" {
  38. progressView.isHidden = webView.estimatedProgress == 1
  39. progressView.setProgress(Float(webView.estimatedProgress), animated: true)
  40. }
  41. }
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. self.attachmentBtn.isHidden = true
  45. if let subjectId = self.subject?.id {
  46. self.viewModel.getSubjectAttachmentList(subjectId: subjectId)
  47. .then { attachments in
  48. if attachments.count > 0 {
  49. self.attachmentList = attachments
  50. self.attachmentBtn.isHidden = false
  51. }
  52. }.catch { (error) in
  53. DDLogError(error.localizedDescription)
  54. }
  55. }
  56. self.theWebView()
  57. if O2AuthSDK.shared.isBBSMute() {
  58. self.replyBtn.isHidden = true
  59. }
  60. }
  61. @IBAction func clickAttachmentBtn(_ sender: UIButton) {
  62. DDLogDebug("点击附件列表")
  63. if self.attachmentList.count > 0 {
  64. self.performSegue(withIdentifier: "showSubAttachmentActionSegue", sender: nil)
  65. }else {
  66. self.showError(title: "没有附件!")
  67. }
  68. }
  69. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  70. if segue.identifier == "showReplyActionSegue" {
  71. let navVC = segue.destination as! ZLNavigationController
  72. let destVC = navVC.topViewController as! BBSReplySubjectViewController
  73. destVC.subject = self.subject
  74. if let parentId = sender {
  75. destVC.parentId = parentId as? String
  76. }
  77. }else if segue.identifier == "showSubAttachmentActionSegue" {
  78. let navVC = segue.destination as! ZLNavigationController
  79. let destVC = navVC.topViewController as! BBSSubjectAttachmentViewController
  80. destVC.attachmentList = self.attachmentList
  81. }
  82. }
  83. override func theWebView(){
  84. super.theWebView()
  85. self.webViewContainer.addSubview(self.webView)
  86. self.webView.translatesAutoresizingMaskIntoConstraints = false
  87. 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)
  88. 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)
  89. 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)
  90. 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)
  91. self.webViewContainer.addConstraints([top, bottom, trailing, leading])
  92. webView.navigationDelegate = self
  93. webView.uiDelegate = self
  94. webView.allowsBackForwardNavigationGestures = true
  95. loadDetailSubject()
  96. }
  97. func loadDetailSubject(){
  98. if let itemUrl = loadUrl, let urlR = URL(string: itemUrl) {
  99. let req = URLRequest(url: urlR)
  100. webView.load(req)
  101. }else {
  102. webView.loadHTMLString("<h2>没有获取到正确的URL!</h2>", baseURL: nil)
  103. }
  104. }
  105. @IBAction func unFromReplyBackSubject(_ segue:UIStoryboardSegue){
  106. loadDetailSubject()
  107. }
  108. //写评论
  109. @IBAction func replaySubject(_ sender: UIButton) {
  110. if O2AuthSDK.shared.isBBSMute() {
  111. DDLogInfo("当前用户被禁言")
  112. return
  113. }
  114. self.performSegue(withIdentifier:"showReplyActionSegue", sender: nil)
  115. }
  116. override func didReceiveMemoryWarning() {
  117. super.didReceiveMemoryWarning()
  118. // Dispose of any resources that can be recreated.
  119. }
  120. }
  121. extension BBSSubjectDetailViewController:WKNavigationDelegate,WKUIDelegate {
  122. func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
  123. DDLogDebug("didFailProvisionalNavigation \(String(describing: navigation)) error = \(error)")
  124. }
  125. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  126. DDLogDebug("didStartProvisionalNavigation \(String(describing: navigation))")
  127. }
  128. func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
  129. DDLogDebug("didCommit")
  130. }
  131. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  132. DDLogDebug("didFinish")
  133. //self.setupData()
  134. }
  135. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  136. DDLogDebug("didFail")
  137. DDLogError(error.localizedDescription)
  138. }
  139. func webViewDidClose(_ webView: WKWebView) {
  140. DDLogInfo("h5执行了window.close()")
  141. self.dismissVC(completion: nil)
  142. }
  143. }