RYBaseVM.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // RYBaseVM.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2023/12/25.
  6. //
  7. #import "RYBaseVM.h"
  8. @implementation RYBaseVM
  9. + (instancetype)setupVMclass:(Class)VMclass {
  10. return [[VMclass alloc]init];
  11. }
  12. - (instancetype)initDelegate:(id<RY_baseVMprotocol>)delegte;
  13. {
  14. self = [super init];
  15. if (self) {
  16. _delegate = delegte;
  17. }
  18. return self;
  19. }
  20. -(void)ry_formDataRequestPostApi:(NSString *)mark param:(NSDictionary *)param{
  21. [ASNetTools.shared formData_postWithPath:mark param:param success:^(id _Nonnull json) {
  22. NSLog(@"mark--%@--param-%@\n-json--%@",mark,param,json);
  23. [self ry_respnsSucessWithPath:mark data:json reqNetType:FormDataPostReqNetType];
  24. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  25. NSLog(@"mark--%@--param-%@\n-Error--%@",mark,param,msg);
  26. [self ry_reponsFaildWithPath:mark code:code msg:msg reqNetType:FormDataPostReqNetType];
  27. }];
  28. }
  29. -(void)ry_requestPostApi:(NSString *)mark param:(NSDictionary *)param{
  30. [ASNetTools.shared postWithPath:mark param:param success:^(id _Nonnull json) {
  31. NSLog(@"mark--%@--param-%@\n-json--%@",mark,param,json);
  32. [self ry_respnsSucessWithPath:mark data:json reqNetType:PostReqNetType];
  33. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  34. NSLog(@"mark--%@--param-%@\n-Error--%@",mark,param,msg);
  35. [self ry_reponsFaildWithPath:mark code:code msg:msg reqNetType:PostReqNetType];
  36. }];
  37. }
  38. -(void)ry_requestGetApi:(NSString *)mark param:(NSDictionary *)param{
  39. K_WEAK_SELF;
  40. [ASNetTools.shared getWithPath:mark param:param success:^(id _Nonnull json) {
  41. K_STRONG_SELF;
  42. NSLog(@"mark--%@--param-%@\n-json--%@",mark,param,json);
  43. [self ry_respnsSucessWithPath:mark data:json reqNetType:GetReqNetType];
  44. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  45. K_STRONG_SELF;
  46. NSLog(@"mark--%@--param-%@\n-Error--%@",mark,param,msg);
  47. [self ry_reponsFaildWithPath:mark code:code msg:msg reqNetType:GetReqNetType];
  48. }];
  49. }
  50. -(void)ry_requestPutApi:(NSString *)mark param:(NSString *)paramStr{
  51. [ASNetTools.shared putWithPath:[NSString stringWithFormat:@"%@/%@",mark,paramStr] param:@{} success:^(id _Nonnull json) {
  52. NSLog(@"mark--%@--param-%@\n-json--%@",mark,paramStr,json);
  53. [self ry_respnsSucessWithPath:mark data:json reqNetType:PutReqNetType];
  54. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  55. NSLog(@"mark--%@--param-%@\n-Error--%@",mark,paramStr,msg);
  56. [self ry_reponsFaildWithPath:mark code:code msg:msg reqNetType:PutReqNetType];
  57. }];
  58. }
  59. -(void)ry_requestDeleteApi:(NSString *)mark paramStr:(NSString *)paramStr{
  60. [ASNetTools.shared delWithPath:[NSString stringWithFormat:@"%@/%@",mark,paramStr] param:@{} success:^(id _Nonnull json) {
  61. NSLog(@"mark--%@--param-%@\n-json--%@",mark,paramStr,json);
  62. [self ry_respnsSucessWithPath:mark data:json reqNetType:DeleteReqNetType];
  63. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  64. NSLog(@"mark--%@--param-%@\n-Error--%@",mark,paramStr,msg);
  65. [self ry_reponsFaildWithPath:mark code:code msg:msg reqNetType:DeleteReqNetType];
  66. }];
  67. }
  68. //重写的方法 解析之后再传给 VC
  69. -(void)ry_respnsSucessWithPath:(NSString *)path data:(id)data reqNetType:(ReqNetType)reqNetType{
  70. [self ry_VMconfigDelegateData:data parseAry:[NSMutableArray arrayWithArray:@[data]] success:YES mark:path reqNetType:reqNetType];
  71. }
  72. -(void)ry_reponsFaildWithPath:(NSString *)path code:(NSString *)code msg:(NSString *)msg reqNetType:(ReqNetType)reqNetType{
  73. [self ry_VMconfigDelegateData:msg parseAry:[NSMutableArray arrayWithArray:@[msg]] success:NO mark:path reqNetType:reqNetType];
  74. }
  75. #pragma mark - **************** VM调用绑定 Delgate ****************
  76. - (void)ry_VMconfigDelegateData:(id)data
  77. parseAry:(NSMutableArray *)ary
  78. success:(BOOL)success
  79. mark:(NSString *)mark
  80. reqNetType:(ReqNetType)reqNetType{
  81. if([self.delegate respondsToSelector:@selector(ry_respnsData:parseAry:sucess:mark:reqNetType:)]){
  82. [self.delegate ry_respnsData:data parseAry:ary sucess:success mark:mark reqNetType:reqNetType];
  83. }
  84. }
  85. @end