From dddf7cb014e543811f2c2447ef8cbea94b74c0d5 Mon Sep 17 00:00:00 2001 From: fengshuo <963787902@qq.com> Date: Thu, 9 Sep 2021 11:50:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E4=BA=86BaseObject=20?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=9A=84=E5=9F=BA=E7=B1=BB=20=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E6=A8=A1=E5=9E=8B=E7=9B=B8=E4=BA=92=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Podfile | 3 +- Podfile.lock | 12 ++-- xplan-ios.xcodeproj/project.pbxproj | 6 ++ xplan-ios/Base/Base.pch | 1 - xplan-ios/Base/MVP/Model/AccountModel.h | 4 +- xplan-ios/Base/MVP/Model/BaseModel.h | 4 +- xplan-ios/Base/MVP/Model/BaseObject.h | 35 ++++++++++++ xplan-ios/Base/MVP/Model/BaseObject.m | 55 +++++++++++++++++++ xplan-ios/Base/Net/HttpRequestHelper.m | 8 +-- .../Main/Login/Presenter/LoginPresenter.m | 2 +- 10 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 xplan-ios/Base/MVP/Model/BaseObject.h create mode 100644 xplan-ios/Base/MVP/Model/BaseObject.m diff --git a/Podfile b/Podfile index 6f9540bf..b67456d2 100644 --- a/Podfile +++ b/Podfile @@ -8,11 +8,12 @@ target 'xplan-ios' do # Pods for xplan-ios pod 'AFNetworking' pod 'YYText' - pod 'YYModel' pod 'Masonry' pod 'ReactiveObjC' pod 'MBProgressHUD' #易盾 本机一键登录 pod 'NTESQuickPass', '~> 2.1.6' + #模型转化 + pod 'MJExtension' end diff --git a/Podfile.lock b/Podfile.lock index 4cff4bb8..49f5cb3c 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -16,18 +16,18 @@ PODS: - AFNetworking/NSURLSession - Masonry (1.1.0) - MBProgressHUD (1.2.0) + - MJExtension (3.3.0) - NTESQuickPass (2.1.9) - ReactiveObjC (3.1.1) - - YYModel (1.0.4) - YYText (1.0.7) DEPENDENCIES: - AFNetworking - Masonry - MBProgressHUD + - MJExtension - NTESQuickPass (~> 2.1.6) - ReactiveObjC - - YYModel - YYText SPEC REPOS: @@ -35,20 +35,20 @@ SPEC REPOS: - AFNetworking - Masonry - MBProgressHUD + - MJExtension - NTESQuickPass - ReactiveObjC - - YYModel - YYText SPEC CHECKSUMS: AFNetworking: 7864c38297c79aaca1500c33288e429c3451fdce Masonry: 678fab65091a9290e40e2832a55e7ab731aad201 MBProgressHUD: 3ee5efcc380f6a79a7cc9b363dd669c5e1ae7406 + MJExtension: 01704cca2b60a214c10761b6491eab74069d68a9 NTESQuickPass: 8431dc52737c95883cd382c2ee75664d58f39377 ReactiveObjC: 011caa393aa0383245f2dcf9bf02e86b80b36040 - YYModel: 2a7fdd96aaa4b86a824e26d0c517de8928c04b30 YYText: 5c461d709e24d55a182d1441c41dc639a18a4849 -PODFILE CHECKSUM: 34d6fc0f46b543fbcabfb886e7a531f43d5dd7f5 +PODFILE CHECKSUM: 6662a976a7be0eed298430e68b706dbb92d1ad1e -COCOAPODS: 1.10.1 +COCOAPODS: 1.11.0 diff --git a/xplan-ios.xcodeproj/project.pbxproj b/xplan-ios.xcodeproj/project.pbxproj index 8e1acdc5..4c5a3b36 100644 --- a/xplan-ios.xcodeproj/project.pbxproj +++ b/xplan-ios.xcodeproj/project.pbxproj @@ -56,6 +56,7 @@ 18E7B26926E8D5D60064BC9B /* XCCurrentVCStackManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B26726E8D5D60064BC9B /* XCCurrentVCStackManager.m */; }; 73FFADDC93E195344047A2EC /* Pods_xplan_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF623970097D653132D69A /* Pods_xplan_ios.framework */; }; 9B0E1C5926E77022005D4442 /* BaseNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B0E1C5826E77022005D4442 /* BaseNavigationController.m */; }; + E8B825BA26E9A9D5009E8E9F /* BaseObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E8B825B926E9A9D5009E8E9F /* BaseObject.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -157,6 +158,8 @@ 9B0E1C5826E77022005D4442 /* BaseNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BaseNavigationController.m; sourceTree = ""; }; B66633E061B1B34177CD011C /* Pods-xplan-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-xplan-ios.release.xcconfig"; path = "Target Support Files/Pods-xplan-ios/Pods-xplan-ios.release.xcconfig"; sourceTree = ""; }; CACF623970097D653132D69A /* Pods_xplan_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_xplan_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + E8B825B826E9A9D5009E8E9F /* BaseObject.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BaseObject.h; sourceTree = ""; }; + E8B825B926E9A9D5009E8E9F /* BaseObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BaseObject.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -271,6 +274,8 @@ 187EEEEE26E89FE8002833B2 /* AccountInfoStorage.m */, 187EEEFD26E8A82C002833B2 /* NSObject+AutoCoding.h */, 187EEEFC26E8A82C002833B2 /* NSObject+AutoCoding.m */, + E8B825B826E9A9D5009E8E9F /* BaseObject.h */, + E8B825B926E9A9D5009E8E9F /* BaseObject.m */, ); path = Model; sourceTree = ""; @@ -619,6 +624,7 @@ buildActionMask = 2147483647; files = ( 189DD76226E60DDC00AB55B1 /* Api+Login.m in Sources */, + E8B825BA26E9A9D5009E8E9F /* BaseObject.m in Sources */, 18E7B22626E8CDCF0064BC9B /* XplanFlutterBoostDelegate.m in Sources */, 189DD73E26E21C3F00AB55B1 /* YYUtility.m in Sources */, 189DD53426DE255300AB55B1 /* TabbarViewController.m in Sources */, diff --git a/xplan-ios/Base/Base.pch b/xplan-ios/Base/Base.pch index c2eef4fd..3a818c2b 100644 --- a/xplan-ios/Base/Base.pch +++ b/xplan-ios/Base/Base.pch @@ -11,7 +11,6 @@ // Include any system framework and library headers here that should be included in all compilation units. // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. #import -#import #define AppName ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]) diff --git a/xplan-ios/Base/MVP/Model/AccountModel.h b/xplan-ios/Base/MVP/Model/AccountModel.h index fd72c714..29e1e147 100644 --- a/xplan-ios/Base/MVP/Model/AccountModel.h +++ b/xplan-ios/Base/MVP/Model/AccountModel.h @@ -5,11 +5,11 @@ // Created by zu on 2021/9/8. // -#import +#import "BaseObject.h" NS_ASSUME_NONNULL_BEGIN -@interface AccountModel : NSObject +@interface AccountModel : BaseObject @property (nonatomic , assign) NSString *uid; @property (nonatomic , copy) NSString *jti; diff --git a/xplan-ios/Base/MVP/Model/BaseModel.h b/xplan-ios/Base/MVP/Model/BaseModel.h index 48f1e813..9f0d400f 100644 --- a/xplan-ios/Base/MVP/Model/BaseModel.h +++ b/xplan-ios/Base/MVP/Model/BaseModel.h @@ -5,11 +5,11 @@ // Created by zu on 2021/9/8. // -#import +#import "BaseObject.h" NS_ASSUME_NONNULL_BEGIN -@interface BaseModel : NSObject +@interface BaseModel : BaseObject @property (nonatomic , strong) id data; @property (nonatomic , assign) NSInteger code; diff --git a/xplan-ios/Base/MVP/Model/BaseObject.h b/xplan-ios/Base/MVP/Model/BaseObject.h new file mode 100644 index 00000000..337de37b --- /dev/null +++ b/xplan-ios/Base/MVP/Model/BaseObject.h @@ -0,0 +1,35 @@ +// +// BaseObject.h +// xplan-ios +// +// Created by 冯硕 on 2021/9/9. +// + +#import + +NS_ASSUME_NONNULL_BEGIN +@interface BaseObject : NSObject +/// 依据数组初始化一个实例数组 +/// @param json json 数据 ++ (NSArray *)modelsWithArray:(id)json; + +/// 依据字典初始化一个实例 +/// @param dictionary 字典 ++ (instancetype)modelWithDictionary:(NSDictionary *)dictionary; + +/// 依据JSON对象初始化一个实例 +/// @param json json 数据 ++ (instancetype)modelWithJSON:(id)json; + +///model 转字典 +- (NSDictionary *)model2dictionary; + +///扩展方法 按需索取 重写即可 +///如果一个模型中 包含一个数组 数组中是另一个模型 ++ (NSDictionary *)objectClassInArray; +///如果一个模型中需要字段映射的话 比如id -> ID name -> other.name ++ (NSDictionary *)replacedKeyFromPropertyName; + +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Base/MVP/Model/BaseObject.m b/xplan-ios/Base/MVP/Model/BaseObject.m new file mode 100644 index 00000000..94559f8b --- /dev/null +++ b/xplan-ios/Base/MVP/Model/BaseObject.m @@ -0,0 +1,55 @@ +// +// BaseObject.m +// xplan-ios +// +// Created by 冯硕 on 2021/9/9. +// + +#import "BaseObject.h" +#import + +@implementation BaseObject + +/// 依据数组初始化一个实例数组 +/// @param json json 数据 ++ (NSArray *)modelsWithArray:(id)json { + return [self mj_objectArrayWithKeyValuesArray:json]; +} + +/// 依据字典初始化一个实例 +/// @param dictionary 字典 ++ (instancetype)modelWithDictionary:(NSDictionary *)dictionary { + return [self mj_objectWithKeyValues:dictionary]; +} + +/// 依据JSON对象初始化一个实例 +/// @param json json 数据 ++ (instancetype)modelWithJSON:(id)json { + return [self mj_objectWithKeyValues:json]; +} + +///model 转字典 +- (NSDictionary *)model2dictionary { + return [[self mj_keyValues] copy]; +} + +///如果模型中包含数组的话 ++ (NSDictionary *)mj_objectClassInArray { + return [self objectClassInArray]; +} + +///模型中需要映射 重写整个方法 ++ (NSDictionary *)mj_replacedKeyFromPropertyName { + return [self replacedKeyFromPropertyName]; +} + +///扩展方法 按需索取 重写即可 +///如果一个模型中 包含一个数组 数组中是另一个模型 ++ (NSDictionary *)objectClassInArray { + return @{}; +} +///如果一个模型中需要字段映射的话 比如id -> ID name -> other.name ++ (NSDictionary *)replacedKeyFromPropertyName { + return @{}; +} +@end diff --git a/xplan-ios/Base/Net/HttpRequestHelper.m b/xplan-ios/Base/Net/HttpRequestHelper.m index 63e7ecec..35bae883 100644 --- a/xplan-ios/Base/Net/HttpRequestHelper.m +++ b/xplan-ios/Base/Net/HttpRequestHelper.m @@ -55,9 +55,9 @@ AFHTTPSessionManager *manager = [HttpRequestHelper requestManager]; [manager GET:method parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { #ifdef DEBUG - NSLog(@"\n%@", [responseObject yy_modelToJSONString]); + NSLog(@"\n%@", responseObject); #endif - BaseModel *baseModel = [BaseModel yy_modelWithDictionary:responseObject]; + BaseModel *baseModel = [BaseModel modelWithDictionary:responseObject]; if (baseModel.code == 200) { success(baseModel); } else { @@ -96,9 +96,9 @@ AFHTTPSessionManager *manager = [HttpRequestHelper requestManager]; [manager POST:method parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { #ifdef DEBUG - NSLog(@"\n%@", [responseObject yy_modelToJSONString]); + NSLog(@"\n%@", responseObject); #endif - BaseModel *baseModel = [BaseModel yy_modelWithDictionary:responseObject]; + BaseModel *baseModel = [BaseModel modelWithDictionary:responseObject]; if (baseModel.code == 200) { success(baseModel); } else { diff --git a/xplan-ios/Main/Login/Presenter/LoginPresenter.m b/xplan-ios/Main/Login/Presenter/LoginPresenter.m index 314a8865..31a11c4d 100644 --- a/xplan-ios/Main/Login/Presenter/LoginPresenter.m +++ b/xplan-ios/Main/Login/Presenter/LoginPresenter.m @@ -18,7 +18,7 @@ - (void)phoneQuickLogin:(NSString *)accessToken token:(NSString *)token { [Api phoneQuickLogin:[self createHttpCompletion:^(BaseModel *data) { - [[AccountInfoStorage instance] saveAccountInfo:[AccountModel yy_modelWithDictionary:data.data]]; + [[AccountInfoStorage instance] saveAccountInfo:[AccountModel modelWithDictionary:data.data]]; [[self getView] phoneQuickLoginSuccess]; } fail:^(NSInteger code, NSString * _Nullable msg) { // todo fail