OOListUnitViewModel.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if let err = result.error {
  65. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  66. }else{
  67. let data = result.value?.data
  68. guard let image = UIImage(data: data!) else {
  69. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  70. return
  71. }
  72. compeletionBlock(image,nil)
  73. }
  74. }
  75. }
  76. }
  77. extension OOListUnitViewModel{
  78. func numberOfSections() -> Int {
  79. return unitDatas.count
  80. }
  81. func numberOfRowsInSection(_ section: Int) -> Int {
  82. return unitDatas[section]!.count
  83. }
  84. func nodeForIndexPath(_ indexPath:IndexPath) -> DataModel? {
  85. return unitDatas[indexPath.section]?[indexPath.row]
  86. }
  87. func headerHeightOfSection(_ section:Int) -> CGFloat {
  88. return 40.0
  89. }
  90. func footerHeightOfSection(_ section:Int) -> CGFloat {
  91. return 10.0
  92. }
  93. func headerTypeOfSection(_ section:Int) -> UIView {
  94. return UIView()
  95. }
  96. }