网络请求
- 是否异步
- 参数字典 (本身参数 系统环境参数, 默认参数)
- 遮罩层
- 报错提示
- url
网络参数类(RequestParam)
userid: NSString *
entid: NSString *
appid: NSString *
secret: NSString *
paramDict: NSDictionary *
+ (instancetype) DefaultRequestParam;
+ (void)setSharedRequestParam;
网络请求类 (HbbNetworkRequest)
url: NSString *
method: NSString *
keyName: NSString *
sync: BOOL
autoEscape: BOOL
logTip: BOOL
requestParam: RequestParam *
- (instancetype)initWith url method keyName requestParam
普通网络请求类
CommonHbbNetworkRequest : HbbNetworkRequest
url = [从环境组件中拿取common网络url]
PinanBannkHbbNetworkRequest : HbbNetworkRequest
url = [从环境组件中拿取平安银行网络url]
二. 以前使用到的业务层封装(适用于需求比较单一的项目)
#import <Foundation/Foundation.h> #import "NetworkReturnResult.h" #import <Hbb_NetworkFramework/Hbb_NetworkFramework.h> static const NSString *HbbNetworkRequestOptionShowHUB = @"ShowHUB";//是否显示遮罩层 static const NSString *HbbNetworkRequestOptionHubMessage = @"HubMessage"; //遮罩层信息 static const NSString *HbbNetworkRequestOptionHubShowType = @"HubShowType"; //遮罩层显示类型 static const NSString *HbbNetworkRequestOptionShowToast = @"ShowToast"; //是否显示toast static const NSString *HbbNetworkRequestOptionAutoUnEscapeKey = @"AutoUnEscapeKey"; //是否自动解码Escape static const NSString *kHbbNetworkRequestOptionCachePolicy = @"CachePolicy"; //缓存策略 /** * 网络请求成功 * * @param networkReturnResult 网络返回结果 */ typedef void (^HbbNetworkRequestSuccess)(NetworkReturnResult *networkReturnResult); /** * 网络请求失败 * * @param error 错误对象 */ typedef void (^HbbNetworkRequestFail)(NSError *error); /** * 货宝宝专用网络请求 * * @author apem */ @interface HbbNetworkRequest : NSObject /** * 公共请求 webService * * @param paramDict 参数字典 * @param cachePolicy 缓存策略 * @param successBlock 成功回调 * @param failBlock 失败回调 */ + (AFHTTPRequestOperation *)requestCommonWebService:(NSDictionary *)paramDict methodName:(NSString *)methodName cachePolicy:(NSURLRequestCachePolicy)cachePolicy successBlock:(void (^)(NetworkReturnResult *networkReturnResult))successBlock failBlock:(void (^)(NSError *error))failBlock; /** * 公共请求 webService, 附带默认处理 * * @param cachePolicy 缓存策略 * @param paramDict 参数字典 * @param successBlock 成功回调 * @param failBlock 失败回调 */ + (AFHTTPRequestOperation *)requestCommonWebServiceWithDefaultDeal:(UIView *)view paramDict:(NSDictionary *)paramDict methodName:(NSString *)methodName cachePolicy:(NSURLRequestCachePolicy)cachePolicy successBlock:(void (^)(NetworkReturnResult *networkReturnResult))successBlock failBlock:(void (^)(NSError *error))failBlock; /** * 平安银行接口 */ + (AFHTTPRequestOperation *)requestPinganBankWebServiceWithDefaultDeal:(UIView *)view paramDict:(NSDictionary *)paramDict methodName:(NSString *)methodName opts:(NSDictionary *)opts successBlock:(void (^)(NetworkReturnResult *networkReturnResult))successBlock failBlock:(void (^)(NSError *error))failBlock; /** * OSS专用接口 */ + (AFHTTPRequestOperation *)requestOSSWebServiceWithDefaultDeal:(UIView *)view paramDict:(NSDictionary *)paramDict methodName:(NSString *)methodName opts:(NSDictionary *)opts successBlock:(void (^)(NetworkReturnResult *networkReturnResult))successBlock failBlock:(void (^)(NSError *error))failBlock; /** * 公共请求 webService, 附带默认处理 * * @param options 其他参数 (HbbNetworkRequestOptionShowHUB,HbbNetworkRequestOptionHubMessage,HbbNetworkRequestOptionHubShowType) * @param cachePolicy 缓存策略 * @param paramDict 参数字典 * @param successBlock 成功回调 * @param failBlock 失败回调 */ + (AFHTTPRequestOperation *)requestCommonWebServiceWithDefaultDealWithOptions:(NSDictionary *)options view:(UIView *)view paramDict:(NSDictionary *)paramDict methodName:(NSString *)methodName cachePolicy:(NSURLRequestCachePolicy)cachePolicy successBlock:(void (^)(NetworkReturnResult *networkReturnResult))successBlock failBlock:(void (^)(NSError *error))failBlock; /** * 任何请求 webService, 附带默认处理 * * @param url 链接 * @param cachePolicy 缓存策略 * @param paramDict 参数字典 * @param options 其他信息 (HbbNetworkRequestOptionShowHUB,HbbNetworkRequestOptionHubMessage,HbbNetworkRequestOptionHubShowType) * @param successBlock 成功回调 * @param failBlock 失败回调 */ + (AFHTTPRequestOperation *)requestAnyWebServiceWithDefaultDealWithURL:(NSString *)url view:(UIView *)view paramDict:(NSDictionary *)paramDict methodName:(NSString *)methodName cachePolicy:(NSURLRequestCachePolicy)cachePolicy options:(NSDictionary *)options successBlock:(void (^)(NetworkReturnResult *networkReturnResult))successBlock failBlock:(void (^)(NSError *error))failBlock; @end
这种以面向过程的工厂式的方法
优点:
1. 调用方便
2. 结构简单
缺点:
1. 方法必传参数过多
2. 当新增一个参数, 往往需要添加一个方法, 使方法膨胀