OOContactSearchViewModel.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. switch result {
  94. case .success(let res):
  95. guard let image = UIImage(data: res.data) else {
  96. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  97. return
  98. }
  99. compeletionBlock(image,nil)
  100. break
  101. case .failure(let err):
  102. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  103. break
  104. }
  105. // if let err = result.error {
  106. // compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  107. // }else{
  108. // let data = result.value?.data
  109. // guard let image = UIImage(data: data!) else {
  110. // compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  111. // return
  112. // }
  113. // compeletionBlock(image,nil)
  114. // }
  115. }
  116. }
  117. }
  118. extension OOContactSearchViewModel {
  119. func numberOfSectionsForSearch() -> Int {
  120. return searchContactDatas.count
  121. }
  122. func numberOfRowsInSectionForSearch(_ section: Int) -> Int {
  123. return searchContactDatas[OOContacSearchSectionHeaderType(rawValue:section)!]!.count
  124. }
  125. func nodeForIndexPathForSearch(_ indexPath:IndexPath) -> DataModel? {
  126. let type = OOContacSearchSectionHeaderType(rawValue: indexPath.section)
  127. let item = searchContactDatas[type!]![indexPath.row]
  128. return item
  129. }
  130. func headerHeightOfSection(_ section:Int) -> CGFloat {
  131. return 40.0
  132. }
  133. func footerHeightOfSection(_ section:Int) -> CGFloat {
  134. return 10.0
  135. }
  136. func headerTypeOfSection(_ section:Int) -> OOContacSearchSectionHeaderType {
  137. return OOContacSearchSectionHeaderType(rawValue: section)!
  138. }
  139. }