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