BaseModel.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. public class O2UPloadImageIdsDataModel: DataModel {
  77. var id: String?
  78. var origId: String?
  79. required public init() {}
  80. public var description: String {
  81. return toJSONString(prettyPrint: true) ?? ""
  82. }
  83. }
  84. class BaseErrorResponse: HandyJSON {
  85. var count:Int?
  86. @objc var date:String?
  87. @objc var message:String?
  88. @objc var size:String?
  89. var spent:Int?
  90. @objc var type:String?
  91. required public init() {}
  92. }
  93. // MARK:- 返回的基本数据格式协议
  94. public protocol BaseModel:HandyJSON,CustomStringConvertible {
  95. //数据头一致,不同的data的数据类型
  96. associatedtype T
  97. var count:Int { get set }
  98. var date:String? {get set}
  99. var message:String? {get set}
  100. var position:Int {get set}
  101. var size:String? {get set}
  102. var spent:Int {get set}
  103. var type:ResultType? {get set}
  104. var userMessage:String? {get set}
  105. var data:T? {get set}
  106. func isSuccess() -> Bool
  107. }
  108. // MARK:- 数据格式模型基本实现
  109. open class BaseModelClass<U:CustomStringConvertible>:BaseModel,CustomStringConvertible {
  110. public typealias T = U
  111. open var position: Int = 0
  112. open var type: ResultType?
  113. open var count: Int = 0
  114. open var date: String?
  115. open var message: String?
  116. open var size: String?
  117. open var spent: Int = -1
  118. open var userMessage: String?
  119. open var data:T?
  120. required public init() {
  121. }
  122. public func isSuccess() -> Bool {
  123. guard let t = type else {
  124. return false
  125. }
  126. if t == .FAIL {
  127. return false
  128. }
  129. return true
  130. }
  131. public var description: String {
  132. 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))"
  133. }
  134. }
  135. public class OOCommonValueModel:DataModel {
  136. var value:String?
  137. required public init() {
  138. }
  139. public var description: String {
  140. return "OOCommonValueModel"
  141. }
  142. }
  143. public class OOCommonValueBoolModel:DataModel {
  144. var value:Bool?
  145. required public init() {
  146. }
  147. public var description: String {
  148. return "OOCommonValueBoolModel"
  149. }
  150. }
  151. public class OOCommonValueIntModel:DataModel {
  152. var value:Int?
  153. required public init() {
  154. }
  155. public var description: String {
  156. return "OOCommonValueIntModel"
  157. }
  158. }
  159. public class OOCommonIdModel:DataModel {
  160. public var description: String {
  161. return "OOCommonIdModel"
  162. }
  163. var id:String?
  164. required public init() {
  165. }
  166. }
  167. public class OOCommonModel:DataModel {
  168. required public init() {
  169. }
  170. public var description: String {
  171. return "CommonModel"
  172. }
  173. }
  174. public class OOEchoModel: DataModel {
  175. public var description: String {
  176. return "OOEchoModel"
  177. }
  178. var servletContextName:String?
  179. var serverTime:String?
  180. required public init() {
  181. }
  182. }