O2SearchViewModel.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // O2SearchViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/5/25.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import Promises
  9. class O2SearchViewModel: NSObject {
  10. override init() {
  11. super.init()
  12. }
  13. private let api = OOMoyaProvider<O2QuerySurfaceAPI>()
  14. private var searchResultIds:[String] = []
  15. private var idsTotalNumber = 0
  16. private var page = 1
  17. private var totalPage = 1
  18. func search(key: String) -> Promise<O2SearchPageModel> {
  19. return Promise {fulfill, reject in
  20. self.api.request(.segmentSearch(key), completion: { result in
  21. let response = OOResult<BaseModelClass<O2SearchIdsEntry>>(result)
  22. if response.isResultSuccess() {
  23. if let data = response.model?.data {
  24. self.searchResultIds = data.valueList
  25. self.idsTotalNumber = data.count
  26. if self.searchResultIds.count > O2.defaultPageSize {
  27. let m = self.searchResultIds.count % O2.defaultPageSize
  28. if m > 0 {
  29. self.totalPage = (self.searchResultIds.count / O2.defaultPageSize) + 1
  30. } else {
  31. self.totalPage = (self.searchResultIds.count / O2.defaultPageSize)
  32. }
  33. } else {
  34. self.totalPage = 1
  35. }
  36. self.page = 1
  37. self.loadListEntry().then { (list) in
  38. var model = O2SearchPageModel()
  39. model.page = self.page
  40. model.totalPage = self.totalPage
  41. model.list = list
  42. fulfill(model)
  43. }.catch { (err) in
  44. reject(err)
  45. }
  46. } else {
  47. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  48. }
  49. } else {
  50. reject(response.error!)
  51. }
  52. })
  53. }
  54. }
  55. /// 下一页
  56. func nextPage() -> Promise<O2SearchPageModel> {
  57. return Promise { fulfill, reject in
  58. if self.page < self.totalPage {
  59. self.page += 1
  60. self.loadListEntry().then { (list) in
  61. var model = O2SearchPageModel()
  62. model.page = self.page
  63. model.totalPage = self.totalPage
  64. model.list = list
  65. fulfill(model)
  66. }.catch { (err) in
  67. reject(err)
  68. }
  69. } else {
  70. reject(OOAppError.apiEmptyResultError)
  71. }
  72. }
  73. }
  74. /// 根据ids 查询结果集
  75. private func loadListEntry() -> Promise<[O2SearchEntry]> {
  76. return Promise{ fulfill, reject in
  77. if self.searchResultIds.count == 0 {
  78. fulfill([])
  79. } else {
  80. let start = (self.page - 1) * O2.defaultPageSize
  81. var end = start + O2.defaultPageSize
  82. if end > self.searchResultIds.count {
  83. end = self.searchResultIds.count
  84. }
  85. let subList = self.searchResultIds[start..<end]
  86. let a = subList.map { (s) -> String in
  87. return s
  88. }
  89. self.api.request(.segmentListEntry(a), completion: {result in
  90. let response = OOResult<BaseModelClass<[O2SearchEntry]>>(result)
  91. if response.isResultSuccess() {
  92. if let list = response.model?.data {
  93. fulfill(list)
  94. }else {
  95. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  96. }
  97. } else {
  98. reject(response.error!)
  99. }
  100. })
  101. }
  102. }
  103. }
  104. }