OOListUnitViewModel.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // OOListUnitViewModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/21.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import Moya
  10. class OOListUnitViewModel:NSObject {
  11. private var unitDatas:[Int:[DataModel]] = [:]
  12. private let contactAPI = OOMoyaProvider<OOContactAPI>()
  13. typealias AddBlock = (_ msg:String?) -> Void
  14. var updateBlock:AddBlock?
  15. override init() {
  16. super.init()
  17. initSetup()
  18. }
  19. func initSetup() {
  20. unitDatas[0] = []
  21. }
  22. // MARK: - 读取数据
  23. func refreshData(_ unitFlag:String){
  24. initSetup()
  25. //读取下一级
  26. contactAPI.request(.listSubDirect(unitFlag)) { (result) in
  27. let myResult = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  28. if myResult.isResultSuccess() {
  29. myResult.model?.data?.forEach({ (uModel) in
  30. self.unitDatas[0]?.append(uModel)
  31. })
  32. }
  33. guard let block = self.updateBlock else {
  34. return
  35. }
  36. if myResult.isResultSuccess() {
  37. block(nil)
  38. }else{
  39. block(myResult.error.debugDescription)
  40. }
  41. }
  42. //读取本级下的所有人员
  43. contactAPI.request(.getUnit(unitFlag)) { (result) in
  44. let myResult = OOResult<BaseModelClass<OOUnitModel>>(result)
  45. if myResult.isResultSuccess() {
  46. let model = myResult.model?.data
  47. model?.woSubDirectIdentityList?.forEach({ (iModel) in
  48. self.unitDatas[0]?.append(iModel.woPerson!)
  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. // MARK: - 获取icon
  62. func getIconOfPerson(_ person:OOPersonModel,compeletionBlock:@escaping (_ image:UIImage?,_ errMsg:String?) -> Void) {
  63. contactAPI.request(.iconByPerson(person.id!)) { (result) in
  64. switch result {
  65. case .success(let res):
  66. guard let image = UIImage(data: res.data) else {
  67. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  68. return
  69. }
  70. compeletionBlock(image,nil)
  71. break
  72. case .failure(let err):
  73. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  74. break
  75. }
  76. }
  77. }
  78. }
  79. extension OOListUnitViewModel{
  80. func numberOfSections() -> Int {
  81. return unitDatas.count
  82. }
  83. func numberOfRowsInSection(_ section: Int) -> Int {
  84. return unitDatas[section]!.count
  85. }
  86. func nodeForIndexPath(_ indexPath:IndexPath) -> DataModel? {
  87. return unitDatas[indexPath.section]?[indexPath.row]
  88. }
  89. func headerHeightOfSection(_ section:Int) -> CGFloat {
  90. return 40.0
  91. }
  92. func footerHeightOfSection(_ section:Int) -> CGFloat {
  93. return 10.0
  94. }
  95. func headerTypeOfSection(_ section:Int) -> UIView {
  96. return UIView()
  97. }
  98. }