BBSViewModel.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // BBSViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/28.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import Promises
  9. class BBSViewModel : NSObject {
  10. override init() {
  11. super.init()
  12. }
  13. private let bbsAPI = OOMoyaProvider<O2BBSAPI>()
  14. }
  15. extension BBSViewModel {
  16. //获取附件列表
  17. func getSubjectAttachmentList(subjectId: String) -> Promise<[O2BBSSubjectAttachmentInfo]> {
  18. return Promise{ fulfill, reject in
  19. self.bbsAPI.request(.getSubjectAttachmentList(subjectId), completion: { result in
  20. let response = OOResult<BaseModelClass<[O2BBSSubjectAttachmentInfo]>>(result)
  21. if response.isResultSuccess() {
  22. if let data = response.model?.data {
  23. fulfill(data)
  24. }else {
  25. reject(OOAppError.apiEmptyResultError)
  26. }
  27. }else {
  28. reject(response.error!)
  29. }
  30. })
  31. }
  32. }
  33. //下载附件
  34. func downloadAttachment(att: O2BBSSubjectAttachmentInfo) -> Promise<URL> {
  35. return Promise { fulfill, reject in
  36. self.bbsAPI.request(.downloadAttachForSubject(att), completion: {result in
  37. switch result {
  38. case .success(_):
  39. //下载成功
  40. let fileURL = O2.cloudFileLocalFolder().appendingPathComponent(att.fileName!)
  41. fulfill(fileURL)
  42. case .failure(let err):
  43. reject(err)
  44. }
  45. })
  46. }
  47. }
  48. }