SPSBBaseNetworkModel.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // SPSBBaseNetworkModel.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/20.
  6. //
  7. #import "SPSBBaseNetworkModel.h"
  8. #import <objc/runtime.h>
  9. #import <JXHGeneralTools.h>
  10. @implementation SPSBBaseNetworkModel
  11. #define spsb_keyPrefix 5
  12. #pragma mark - 字典转Model
  13. + (NSArray *)getModelListWithArray:(nullable NSArray *)array {
  14. if (!array || ![array isKindOfClass:[NSArray class]]) {
  15. return @[];
  16. }
  17. NSMutableArray *dataList = NSMutableArray.new;
  18. for (id obj in array) {
  19. if (![obj isKindOfClass:[NSDictionary class]]) {
  20. continue;
  21. }
  22. id data = self.new;
  23. bool flag = [data handleDataWithDictionary:obj];
  24. if (flag) {
  25. [dataList addObject:data];
  26. }
  27. }
  28. return dataList;
  29. }
  30. + (id)getModelWithDictionary:(nullable NSDictionary *)dict {
  31. id data = self.new;
  32. if (!dict || ![dict isKindOfClass:[NSDictionary class]]) {
  33. dict = @{};
  34. }
  35. if ([data handleDataWithDictionary:dict]) {
  36. return data;
  37. }
  38. [data handleDataWithDictionary:@{}];
  39. return data;
  40. }
  41. - (bool)handleDataWithDictionary:(nullable NSDictionary *)dict {
  42. Class class = [self class];
  43. while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
  44. unsigned int count;
  45. objc_property_t *properties = class_copyPropertyList(class, &count);
  46. for(int i = 0; i < count; i++) {
  47. objc_property_t property = properties[i];
  48. NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  49. if ([key hasPrefix:@"spsb_"]) {
  50. SEL action = NSSelectorFromString([NSString stringWithFormat:@"GET%@WithDictionary:key:", key]);
  51. IMP imp = [self methodForSelector:action];
  52. id (*func)(id, SEL, NSDictionary *, NSString *) = (void *)imp;
  53. id value = func(self, action, dict, [key substringFromIndex:spsb_keyPrefix]);
  54. [self setValue:value forKey:key];
  55. }
  56. }
  57. free(properties);
  58. class = class_getSuperclass(class);
  59. }
  60. return [self handleAdditionalProperty];
  61. }
  62. - (bool)handleAdditionalProperty {
  63. return true;
  64. }
  65. - (id)getStringDataWithDictionary:(nullable NSDictionary *)dic variable:(NSString *)variable {
  66. if (!dic || !dic[variable] || jxh_isNullClass(dic[variable])) {
  67. return @"";
  68. }
  69. return [NSString stringWithFormat:@"%@", dic[variable]];
  70. }
  71. #pragma mark - Model转字典
  72. + (NSArray *)getArray:(NSArray *)array {
  73. if (!array || ![array isKindOfClass:[NSArray class]]) {
  74. return @[];
  75. }
  76. NSMutableArray *result = NSMutableArray.new;
  77. for (id object in array) {
  78. if (![object isKindOfClass:[self class]]) continue;
  79. id r = [object performSelector:@selector(getDictionary)];
  80. [result addObject:r];
  81. }
  82. return result;
  83. }
  84. - (NSDictionary *)getDictionary {
  85. NSMutableDictionary *dic = NSMutableDictionary.new;
  86. Class class = [self class];
  87. while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
  88. unsigned int count;
  89. objc_property_t *properties = class_copyPropertyList(class, &count);
  90. for(int i = 0; i < count; i++) {
  91. objc_property_t property = properties[i];
  92. NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  93. if ([key hasPrefix:@"spsb_"]) {
  94. SEL action = NSSelectorFromString([NSString stringWithFormat:@"GETStringWith%@:", key]);
  95. IMP imp = [self methodForSelector:action];
  96. id (*func)(id, SEL, NSString *) = (void *)imp;
  97. id value = func(self, action, key);
  98. [dic setValue:value forKey:[key substringFromIndex:spsb_keyPrefix]];
  99. }
  100. }
  101. free(properties);
  102. class = class_getSuperclass(class);
  103. }
  104. return [self handleAdditionalWithDic:dic];
  105. }
  106. - (NSMutableDictionary *)handleAdditionalWithDic:(NSMutableDictionary *)dic {
  107. return dic;
  108. }
  109. - (id)getStringWithKey:(NSString *)key {
  110. return [self valueForKey:key];
  111. }
  112. #pragma mark - 消息转发
  113. + (BOOL)resolveInstanceMethod:(SEL)sel {
  114. NSString *method = NSStringFromSelector(sel);
  115. if ([method hasPrefix:@"GETspsb_"]) {
  116. Method originalMethod = class_getInstanceMethod(self, @selector(getStringDataWithDictionary:variable:));
  117. class_addMethod(self, sel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  118. return true;
  119. }
  120. if ([method hasPrefix:@"GETStringWithspsb_"]) {
  121. Method originalMethod = class_getInstanceMethod(self, @selector(getStringWithKey:));
  122. class_addMethod(self, sel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  123. return true;
  124. }
  125. return [super resolveInstanceMethod:sel];
  126. }
  127. #pragma mark - 归档
  128. - (id)copyWithZone:(NSZone *)zone {
  129. Class class = [self class];
  130. id new = class.new;
  131. while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
  132. unsigned int count;
  133. objc_property_t *properties = class_copyPropertyList(class, &count);
  134. for(int i = 0; i < count; i++) {
  135. objc_property_t property = properties[i];
  136. NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  137. if ([key hasPrefix:@"spsb"]) {
  138. [new setValue:[self valueForKey:key] forKey:key];
  139. }
  140. }
  141. free(properties);
  142. class = class_getSuperclass(class);
  143. }
  144. return new;
  145. }
  146. - (void)encodeWithCoder:(NSCoder *)aCoder {
  147. Class class = [self class];
  148. while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
  149. unsigned int count;
  150. objc_property_t *properties = class_copyPropertyList(class, &count);
  151. for(int i = 0; i < count; i++) {
  152. objc_property_t property = properties[i];
  153. NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  154. if ([key hasPrefix:@"spsb"]) {
  155. [aCoder encodeObject:[self valueForKey:key] forKey:[NSString stringWithFormat:@"k%@", key]];
  156. }
  157. }
  158. free(properties);
  159. class = class_getSuperclass(class);
  160. }
  161. }
  162. - (id)initWithCoder:(NSCoder *)aDecoder {
  163. self = [super init];
  164. if (self) {
  165. Class class = [self class];
  166. while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
  167. unsigned int count;
  168. objc_property_t *properties = class_copyPropertyList(class, &count);
  169. for(int i = 0; i < count; i++) {
  170. objc_property_t property = properties[i];
  171. NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  172. if ([key hasPrefix:@"spsb"]) {
  173. id value = [aDecoder decodeObjectForKey:[NSString stringWithFormat:@"k%@", key]];
  174. if (!value) {
  175. value = @"";
  176. }
  177. [self setValue:value forKey:key];
  178. }
  179. }
  180. free(properties);
  181. class = class_getSuperclass(class);
  182. }
  183. }
  184. return self;
  185. }
  186. - (NSString *)description {
  187. NSMutableString *desc = [NSMutableString stringWithString:@""];
  188. Class class = [self class];
  189. while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
  190. unsigned int count;
  191. objc_property_t *properties = class_copyPropertyList(class, &count);
  192. for(int i = 0; i < count; i++) {
  193. objc_property_t property = properties[i];
  194. NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  195. if ([key hasPrefix:@"spsb"]) {
  196. [desc appendFormat:@"%@ : %@\n", key, [self valueForKey:key]];
  197. }
  198. }
  199. free(properties);
  200. class = class_getSuperclass(class);
  201. }
  202. return desc;
  203. }
  204. @end