MessageViewController.swift 8.3 KB

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