BaseModel.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // BaseModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/21.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import HandyJSON
  10. import Moya
  11. // MARK:- 通用过程处理回调闭包定义
  12. public typealias ProgressBlock = (_ progressResponse:ProgressResponse) -> Void
  13. // MARK:- 通用结果回调闭包定义
  14. public typealias ResultBlock<T:BaseModel> = (_ result:OOResult<T>) -> Void
  15. // MARK:- 结果类型定义
  16. public enum ResultType:String,HandyJSONEnum {
  17. case SUCCESS = "success"
  18. case FAIL = "error"
  19. }
  20. public protocol DataModel: HandyJSON,CustomStringConvertible {
  21. }
  22. public protocol IBaseO2ResponseData: HandyJSON, CustomStringConvertible {
  23. //数据头一致,不同的data的数据类型
  24. associatedtype T
  25. var count:Int { get set }
  26. var date:String? {get set}
  27. var message:String? {get set}
  28. var position:Int {get set}
  29. var size:String? {get set}
  30. var spent:Int {get set}
  31. var type:ResultType? {get set}
  32. var userMessage:String? {get set}
  33. var data:T? {get set}
  34. func isSuccess() -> Bool
  35. }
  36. open class BaseO2ResponseData<U: CustomStringConvertible>: IBaseO2ResponseData {
  37. public typealias T = U
  38. open var type: ResultType?
  39. open var count: Int = 0
  40. open var date: String?
  41. open var message: String?
  42. open var position: Int = 0
  43. open var size: String?
  44. open var spent: Int = -1
  45. open var userMessage: String?
  46. open var data:T?
  47. required public init() {}
  48. public func isSuccess() -> Bool {
  49. guard let t = type else {
  50. return false
  51. }
  52. if t == .FAIL {
  53. return false
  54. }
  55. return true
  56. }
  57. open var description: String {
  58. return toJSONString(prettyPrint: true) ?? ""
  59. }
  60. }
  61. /// value String类型
  62. public class O2StringValueModel: DataModel {
  63. var value:String?
  64. required public init() {}
  65. public var description: String {
  66. return toJSONString(prettyPrint: true) ?? ""
  67. }
  68. }
  69. public class O2IdDataModel: DataModel {
  70. var id: String?
  71. required public init() {}
  72. public var description: String {
  73. return toJSONString(prettyPrint: true) ?? ""
  74. }
  75. }
  76. // MARK:- 返回的基本数据格式协议
  77. public protocol BaseModel:HandyJSON,CustomStringConvertible {
  78. //数据头一致,不同的data的数据类型
  79. associatedtype T
  80. var count:Int { get set }
  81. var date:String? {get set}
  82. var message:String? {get set}
  83. var position:Int {get set}
  84. var size:String? {get set}
  85. var spent:Int {get set}
  86. var type:ResultType? {get set}
  87. var userMessage:String? {get set}
  88. var data:T? {get set}
  89. func isSuccess() -> Bool
  90. }
  91. // MARK:- 数据格式模型基本实现
  92. open class BaseModelClass<U:CustomStringConvertible>:BaseModel,CustomStringConvertible {
  93. public typealias T = U
  94. open var position: Int = 0
  95. open var type: ResultType?
  96. open var count: Int = 0
  97. open var date: String?
  98. open var message: String?
  99. open var size: String?
  100. open var spent: Int = -1
  101. open var userMessage: String?
  102. open var data:T?
  103. required public init() {
  104. }
  105. public func isSuccess() -> Bool {
  106. guard let t = type else {
  107. return false
  108. }
  109. if t == .FAIL {
  110. return false
  111. }
  112. return true
  113. }
  114. public var description: String {
  115. return "type=\(String(describing: type)),position=\(position),count=\(count),date=\(String(describing: date)),message=\(String(describing: message)),size=\(String.init(describing: size) ),spent=\(spent),userMessage=\(userMessage!),data=\(String(describing: data))"
  116. }
  117. }
  118. public class OOCommonValueModel:DataModel {
  119. var value:String?
  120. required public init() {
  121. }
  122. public var description: String {
  123. return "OOCommonValueModel"
  124. }
  125. }
  126. public class OOCommonValueBoolModel:DataModel {
  127. var value:Bool?
  128. required public init() {
  129. }
  130. public var description: String {
  131. return "OOCommonValueBoolModel"
  132. }
  133. }
  134. public class OOCommonValueIntModel:DataModel {
  135. var value:Int?
  136. required public init() {
  137. }
  138. public var description: String {
  139. return "OOCommonValueIntModel"
  140. }
  141. }
  142. public class OOCommonIdModel:DataModel {
  143. public var description: String {
  144. return "OOCommonIdModel"
  145. }
  146. var id:String?
  147. required public init() {
  148. }
  149. }
  150. public class OOCommonModel:DataModel {
  151. required public init() {
  152. }
  153. public var description: String {
  154. return "CommonModel"
  155. }
  156. }