OOContactSearchViewModel.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // OOContactSearchViewModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/28.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. class OOContactSearchViewModel: NSObject {
  10. //搜索结果数据
  11. private var searchContactDatas:[OOContacSearchSectionHeaderType:[DataModel]] = [:]
  12. private let contactAPI = OOMoyaProvider<OOContactAPI>()
  13. typealias AddBlock = (_ msg:String?) -> Void
  14. var updateBlock:AddBlock?
  15. override init() {
  16. super.init()
  17. initData()
  18. }
  19. func initData(){
  20. searchContactDatas[.unit] = []
  21. searchContactDatas[.person] = []
  22. searchContactDatas[.group] = []
  23. }
  24. }
  25. // MARK:- 搜索
  26. extension OOContactSearchViewModel{
  27. func searchRefreshData(_ searchText:String){
  28. //搜索组织
  29. //搜索人员
  30. //组织群组
  31. //self.initData()
  32. contactAPI.request(.unitLike(searchText)) { (result) in
  33. let myResult = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  34. self.searchContactDatas[.unit] = []
  35. if myResult.isResultSuccess() {
  36. if let model = myResult.model?.data {
  37. model.forEach({ (uModel) in
  38. self.searchContactDatas[.unit]?.append(uModel)
  39. })
  40. }
  41. }
  42. guard let block = self.updateBlock else {
  43. return
  44. }
  45. if myResult.isResultSuccess() {
  46. block(nil)
  47. }else{
  48. block(myResult.error.debugDescription)
  49. }
  50. }
  51. contactAPI.request(.personLike(searchText)) { (result) in
  52. let myResult = OOResult<BaseModelClass<[OOPersonModel]>>(result)
  53. self.searchContactDatas[.person] = []
  54. if myResult.isResultSuccess() {
  55. if let model = myResult.model?.data {
  56. model.forEach({ (pModel) in
  57. self.searchContactDatas[.person]?.append(pModel)
  58. })
  59. }
  60. }
  61. guard let block = self.updateBlock else {
  62. return
  63. }
  64. if myResult.isResultSuccess() {
  65. block(nil)
  66. }else{
  67. block(myResult.error.debugDescription)
  68. }
  69. }
  70. contactAPI.request(.groupLike(searchText)) { (result) in
  71. let myResult = OOResult<BaseModelClass<[OOGroupModel]>>(result)
  72. self.searchContactDatas[.group] = []
  73. if myResult.isResultSuccess() {
  74. if let model = myResult.model?.data {
  75. model.forEach({ (gModel) in
  76. self.searchContactDatas[.group]?.append(gModel)
  77. })
  78. }
  79. }
  80. guard let block = self.updateBlock else {
  81. return
  82. }
  83. if myResult.isResultSuccess() {
  84. block(nil)
  85. }else{
  86. block(myResult.error.debugDescription)
  87. }
  88. }
  89. }
  90. // MARK: - 获取icon
  91. func getIconOfPerson(_ person:OOPersonModel,compeletionBlock:@escaping (_ image:UIImage?,_ errMsg:String?) -> Void) {
  92. contactAPI.request(.iconByPerson(person.id!)) { (result) in
  93. if let err = result.error {
  94. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  95. }else{
  96. let data = result.value?.data
  97. guard let image = UIImage(data: data!) else {
  98. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  99. return
  100. }
  101. compeletionBlock(image,nil)
  102. }
  103. }
  104. }
  105. }
  106. extension OOContactSearchViewModel {
  107. func numberOfSectionsForSearch() -> Int {
  108. return searchContactDatas.count
  109. }
  110. func numberOfRowsInSectionForSearch(_ section: Int) -> Int {
  111. return searchContactDatas[OOContacSearchSectionHeaderType(rawValue:section)!]!.count
  112. }
  113. func nodeForIndexPathForSearch(_ indexPath:IndexPath) -> DataModel? {
  114. let type = OOContacSearchSectionHeaderType(rawValue: indexPath.section)
  115. let item = searchContactDatas[type!]![indexPath.row]
  116. return item
  117. }
  118. func headerHeightOfSection(_ section:Int) -> CGFloat {
  119. return 40.0
  120. }
  121. func footerHeightOfSection(_ section:Int) -> CGFloat {
  122. return 10.0
  123. }
  124. func headerTypeOfSection(_ section:Int) -> OOContacSearchSectionHeaderType {
  125. return OOContacSearchSectionHeaderType(rawValue: section)!
  126. }
  127. }