MessageViewController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // MessageViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/23.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import NVActivityIndicatorView
  10. import WHToast
  11. import GTMRefresh
  12. import IBAnimatable
  13. /// 消息视图容器控制类
  14. class MessageViewController: BaseViewController {
  15. // MARK: - 属性列表
  16. /// 通知
  17. @IBOutlet weak var message: UIButton!
  18. /// 公告
  19. @IBOutlet weak var notice: UIButton!
  20. /// 消息数量
  21. @IBOutlet weak var msgCount: AnimatableLabel!
  22. /// 公告数量
  23. @IBOutlet weak var noticeCount: AnimatableLabel!
  24. /// 分割线
  25. @IBOutlet weak var line: UIImageView!
  26. /// 属性列表
  27. @IBOutlet weak var tableView: UITableView!
  28. /// CellIdentifier
  29. let CellIdentifier = "Message_Cell"
  30. /// 公告消息
  31. var notices: [MoNotice] = [] {
  32. didSet {
  33. /// 刷新列表数据
  34. tableView.reloadData()
  35. /// 是否隐藏无数据按钮
  36. noDataButton.isHidden = notices.count != 0
  37. }
  38. }
  39. /// 消息类型
  40. var msgType: Int = 2
  41. // MARK: - 生命周期相关
  42. required init?(coder aDecoder: NSCoder) {
  43. super.init(coder: aDecoder)
  44. /// 设置TabBar图片
  45. self.tabBarItem.image = UIImage(named: "message_normal")?.withRenderingMode(.alwaysOriginal)
  46. self.tabBarItem.selectedImage = UIImage(named: "message_selected")?.withRenderingMode(.alwaysOriginal)
  47. }
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. // Do any additional setup after loading the view.
  51. /// 不需要返回按钮
  52. addBackBarButtonItem(true)
  53. /// UI界面初始化
  54. buildView()
  55. /// 数据初始化
  56. initData()
  57. }
  58. override func viewWillAppear(_ animated: Bool) {
  59. super.viewWillAppear(animated)
  60. /// 查询数据
  61. self.tableView.triggerRefreshing()
  62. }
  63. // MARK: - 初始化
  64. /// UI界面初始化
  65. fileprivate func buildView() {
  66. /// Loding.....
  67. addLoadingView()
  68. /// 添加下拉刷新控件
  69. self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  70. /// 重置请求条件
  71. self.initData()
  72. }
  73. }
  74. /// 数据初始化
  75. fileprivate func initData() {
  76. /// 查询公告消息
  77. self.requestMsgCount()
  78. /// 查询公告消息
  79. self.requestNotices(msgType)
  80. }
  81. // MARK: - 交互相关
  82. @IBAction fileprivate func onButtonPressed(_ sender: UIControl) {
  83. switch sender {
  84. case message: /// 查询数据
  85. msgType = 2
  86. self.requestNotices(msgType)
  87. self.message.setTitleColor(UIColorFromHex(rgbValue: 0x3384F3), for: .normal)
  88. self.notice.setTitleColor(UIColorFromHex(rgbValue: 0x333333), for: .normal)
  89. default: /// 查询数据
  90. msgType = 1
  91. self.requestNotices(msgType)
  92. self.notice.setTitleColor(UIColorFromHex(rgbValue: 0x3384F3), for: .normal)
  93. self.message.setTitleColor(UIColorFromHex(rgbValue: 0x333333), for: .normal)
  94. }
  95. UIView.animate(withDuration: 0.3) {
  96. self.line.centerX = sender.centerX+10.0
  97. }
  98. }
  99. // MARK: - 接口请求
  100. /// 查询公告消息
  101. fileprivate func requestNotices(_ msgType: Int = 1) {
  102. /// 当前未登录
  103. guard let _ = MTP2BusinessCore.shared.accountManager?.loginRsp,
  104. let noticeManager = MTP2BusinessCore.shared.noticeManager else {
  105. /// 您还未登录,请登录后查看!
  106. self.noDataButton.set(image: nil, title: "您还未登录,请登录后查看!", titlePosition: .bottom, additionalSpacing: 0.0, state: .normal)
  107. self.noDataButton.isHidden = false
  108. return
  109. }
  110. /// 开启Loading
  111. self._anim?.startAnimating()
  112. /// 数据查询
  113. noticeManager.requestNotices(nil, nil, msgType, false) { (isComplete, error, notices) in
  114. DispatchQueue.main.async {
  115. /// stop
  116. self._anim?.stopAnimating()
  117. /// endRefreshing
  118. self.tableView.endRefreshing(isSuccess: true)
  119. if isComplete {
  120. self.notices = notices ?? []
  121. } else {
  122. self.notices = []
  123. WHToast.showError(withMessage: "查询失败,原因:\(error?.retMsg ?? "")", duration: 1.5, finishHandler: {})
  124. }
  125. }
  126. }
  127. }
  128. /// 查询公告通知未读数量
  129. fileprivate func requestMsgCount() {
  130. /// 当前未登录
  131. guard let _ = MTP2BusinessCore.shared.accountManager?.loginRsp,
  132. let noticeManager = MTP2BusinessCore.shared.noticeManager else {
  133. return
  134. }
  135. /// 数据查询
  136. noticeManager.requestNotices(nil, nil, nil, false) { (isComplete, error, notices) in
  137. DispatchQueue.main.async {
  138. if isComplete,
  139. let msgs = notices,
  140. msgs.count != 0 {
  141. var msgCount = 0, noticeCount = 0
  142. for (_, obj) in msgs.enumerated() {
  143. if !obj.readed { if obj.msgtype == 2 { msgCount += 1 } else { noticeCount += 1 } }
  144. }
  145. if msgCount != 0 { self.msgCount.text = "\(msgCount)" }
  146. if noticeCount != 0 { self.noticeCount.text = "\(noticeCount)" }
  147. self.msgCount.isHidden = msgCount == 0
  148. self.noticeCount.isHidden = noticeCount == 0
  149. }
  150. }
  151. }
  152. }
  153. // MARK: - Navigation
  154. // In a storyboard-based application, you will often want to do a little preparation before navigation
  155. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  156. // Get the new view controller using segue.destination.
  157. // Pass the selected object to the new view controller.
  158. if segue.identifier == "ShowNoticeDetail" { /// 详情
  159. (segue.destination as! MessageDetailViewController).model = sender as? MoNotice
  160. }
  161. }
  162. }
  163. // MARK: - UITableViewDelegate, UITableViewDataSource
  164. extension MessageViewController: UITableViewDelegate, UITableViewDataSource {
  165. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  166. return notices.count
  167. }
  168. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  169. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! MessageCell
  170. cell.model = notices[indexPath.row]
  171. return cell
  172. }
  173. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  174. self.performSegue(withIdentifier: "ShowNoticeDetail", sender: notices[indexPath.row])
  175. }
  176. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 90.0 }
  177. }
  178. // MARK: - PurchaseCell
  179. class MessageCell: BaseTableViewCell<MoNotice> {
  180. /// 未读标识
  181. @IBOutlet weak var read: AnimatableView!
  182. /// 发布时间
  183. @IBOutlet weak var date: UILabel!
  184. /// 公告标题
  185. @IBOutlet weak var title: UILabel!
  186. /// 公告详情
  187. @IBOutlet weak var detail: UILabel!
  188. /// 公告图标
  189. @IBOutlet weak var iconImage: UIImageView!
  190. override var model: MoNotice? {
  191. didSet {
  192. guard let obj = model else { return }
  193. /// 发布时间
  194. date.text = DateUtils.getTDateString(obj.createtime, "yyyy-MM-dd HH:mm")
  195. /// 公告标题
  196. title.text = obj.title
  197. /// 公告详情
  198. detail.text = obj.content
  199. /// 未读标识
  200. read.isHidden = obj.readed
  201. /// 公告图标
  202. guard let url = StringUtils.getImageUrl(obj.msgiconurl) else { return }
  203. iconImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  204. }
  205. }
  206. }