OOContactViewModel.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // OOContactViewModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/20.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import Promises
  10. class OOContactViewModel: NSObject {
  11. //分组
  12. private let sectionNums = 4
  13. //数据
  14. private var contactDatas:[OOContactGroupHeaderType:[DataModel]] = [:]
  15. private let contactAPI = OOMoyaProvider<OOContactAPI>()
  16. typealias AddBlock = (_ msg:String?) -> Void
  17. var updateBlock:AddBlock?
  18. override init() {
  19. super.init()
  20. initData()
  21. }
  22. func initData(){
  23. contactDatas[.department] = []
  24. contactDatas[.company] = []
  25. contactDatas[.linkman] = []
  26. contactDatas[.group] = []
  27. }
  28. }
  29. // MARK: - 读取公司、部门及群组和常用联系人
  30. extension OOContactViewModel {
  31. func refreshData() {
  32. let currentAccount = O2AuthSDK.shared.myInfo()!
  33. self.initData()
  34. //请求当前帐号的所有个人信息
  35. contactAPI.request(.getPerson(currentAccount.id!)) { (result) in
  36. let myResult = OOResult<BaseModelClass<OOPersonModel>>(result)
  37. if myResult.isResultSuccess() {
  38. //数据处理 增加我的部门 从我的身份中取到部门信息
  39. if let model = myResult.model?.data {
  40. model.woIdentityList?.forEach({ (iModel) in
  41. if let unit = iModel.woUnit {
  42. self.contactDatas[.department]?.append(unit)
  43. }
  44. })
  45. //我的群组
  46. model.woGroupList?.forEach({ (gModel) in
  47. self.contactDatas[.group]?.append(gModel)
  48. })
  49. }
  50. }
  51. guard let block = self.updateBlock else {
  52. return
  53. }
  54. if myResult.isResultSuccess() {
  55. block(nil)
  56. }else{
  57. block(myResult.error.debugDescription)
  58. }
  59. }
  60. //请求顶层组织
  61. contactAPI.request(.listTop) { (result) in
  62. let myResult = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  63. if myResult.isResultSuccess() {
  64. if let model = myResult.model?.data {
  65. model.forEach({ (uModel) in
  66. self.contactDatas[.company]?.append(uModel)
  67. })
  68. }
  69. }
  70. guard let block = self.updateBlock else {
  71. return
  72. }
  73. if myResult.isResultSuccess() {
  74. block(nil)
  75. }else{
  76. block(myResult.error.debugDescription)
  77. }
  78. }
  79. }
  80. }
  81. extension OOContactViewModel {
  82. func numberOfSections() -> Int {
  83. return contactDatas.count
  84. }
  85. func numberOfRowsInSection(_ section: Int) -> Int {
  86. return contactDatas[OOContactGroupHeaderType(rawValue:section)!]!.count
  87. }
  88. func nodeForIndexPath(_ indexPath:IndexPath) -> DataModel? {
  89. let type = OOContactGroupHeaderType(rawValue: indexPath.section)
  90. let item = contactDatas[type!]![indexPath.row]
  91. return item
  92. }
  93. func headerHeightOfSection(_ section:Int) -> CGFloat {
  94. return 40.0
  95. }
  96. func footerHeightOfSection(_ section:Int) -> CGFloat {
  97. return 10.0
  98. }
  99. func headerTypeOfSection(_ section:Int) -> OOContactGroupHeaderType {
  100. return OOContactGroupHeaderType(rawValue: section)!
  101. }
  102. }