| | |
| | | #import "EZHttpUtil.h" |
| | | #import <Foundation/Foundation.h> |
| | | #import <CommonCrypto/CommonDigest.h> |
| | | #import "AFNetworking.h" |
| | | |
| | | #pragma mark API |
| | | #define API_POST_EZ_AddDevice @"/home-wisdom/platform/childAddDevice" |
| | | #define API_POST_EZ_GetChildToken @"/home-wisdom/platform/childToken" |
| | | #define API_POST_EZ_ChildDelDevice @"/home-wisdom/platform/childDelDevice" |
| | | #define API_POST_EZ_RefreshToken @"/smart-footstone/member/oauth/login" |
| | | |
| | | |
| | | #pragma mark APP_KEY |
| | | #define APP_KEY @"HDL-HOME-APP-TEST" |
| | | #define SECRET_KEY @"WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss" |
| | | |
| | | |
| | | @implementation EZHttpUtil |
| | | |
| | | #pragma mark -接口请求部分 |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * @since 河东获取子账号token的接口 |
| | | * |
| | | * @param block 回调block |
| | | */ |
| | | + (void)getChildToken:(void (^)(NSString *accessToken))block{ |
| | | |
| | | //2.设置请求参数 |
| | | NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
| | | parameters = [self GetSignRequestDictionary:parameters]; |
| | | |
| | | [self requestHttpsPost:API_POST_EZ_GetChildToken parameters:parameters completion:^(ResponseData *responseData) { |
| | | if (block) { |
| | | NSString * token = @""; |
| | | if(responseData.success){ |
| | | token = responseData.data[@"accessToken"]; |
| | | } |
| | | block(token); |
| | | } |
| | | |
| | | }]; |
| | | |
| | | // return task; |
| | | } |
| | | /** |
| | | * @since 河东添加设备的接口 |
| | | * 根据设备序列号和设备验证码添加设备接口 |
| | | * |
| | | * @param deviceSerial 设备序列号 |
| | | * @param verifyCode 设备验证码 |
| | | * @param completion 回调block,error为空时表示添加成功 |
| | | * |
| | | * @return operation |
| | | */ |
| | | + (void)addDeviceByHDL:(NSString *)deviceSerial |
| | | verifyCode:(NSString *)verifyCode |
| | | completion:(void (^)(ResponseData *responseData))completion{ |
| | | |
| | | //2.设置请求参数 |
| | | NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
| | | [parameters setValue:deviceSerial forKey:@"deviceSerial"]; |
| | | [parameters setValue:verifyCode forKey:@"validateCode"]; |
| | | parameters = [self GetSignRequestDictionary:parameters]; |
| | | |
| | | [self requestHttpsPost:API_POST_EZ_AddDevice parameters:parameters completion:^(ResponseData *responseData) { |
| | | if (completion) { |
| | | completion (responseData); |
| | | } |
| | | }]; |
| | | |
| | | // return task; |
| | | } |
| | | |
| | | /** |
| | | * @since 河东删除设备的接口 |
| | | * 根据设备序列号删除设备接口 |
| | | * |
| | | * @param deviceSerial 设备序列号 |
| | | * @param completion 回调block,error为空时表示添加成功 |
| | | * |
| | | * @return operation |
| | | */ |
| | | + (void)deleteDeviceByHDL:(NSString *)deviceSerial completion:(void (^)(ResponseData *responseData))completion{ |
| | | //1.设置请求参数 |
| | | NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
| | | [parameters setValue:deviceSerial forKey:@"deviceSerial"]; |
| | | parameters = [self GetSignRequestDictionary:parameters]; |
| | | |
| | | [self requestHttpsPost:API_POST_EZ_ChildDelDevice parameters:parameters completion:^(ResponseData *responseData) { |
| | | if (completion) { |
| | | completion (responseData); |
| | | } |
| | | }]; |
| | | |
| | | // return task; |
| | | } |
| | | |
| | | |
| | | |
| | | /// 刷新token |
| | | /// @param block 结果 |
| | | + (void)refreshHDLToken:(void (^)(BOOL isSuccess))block{ |
| | | |
| | | //1.设置请求参数 |
| | | NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
| | | [parameters setValue:GlobalKit.shareKit.hdlRefreshToken forKey:@"refreshToken"]; |
| | | [parameters setValue:@"refresh_token" forKey:@"grantType"]; |
| | | [parameters setValue:@"APP" forKey:@"platform"]; |
| | | |
| | | parameters = [self GetSignRequestDictionary:parameters]; |
| | | |
| | | [self requestHttpsBase:API_POST_EZ_RefreshToken parameters:parameters completion:^(ResponseData *responseData) { |
| | | if (block) { |
| | | if (responseData) { |
| | | if(responseData.success){ |
| | | [EZSDK setHDlAccessToken: |
| | | [NSString stringWithFormat:@"%@%@",responseData.data[@"headerPrefix"], responseData.data[@"accessToken"]] |
| | | refreshToken:responseData.data[@"refreshToken"]]; |
| | | |
| | | block(YES); |
| | | return; |
| | | } |
| | | } |
| | | block(NO); |
| | | } |
| | | }]; |
| | | |
| | | |
| | | // return task; |
| | | } |
| | | |
| | | |
| | | #pragma mark HttpUtil |
| | | |
| | | /// 通用 请求服务器方法 |
| | | /// @param apiPath 接口地址 |
| | | /// @param parameters 请求参数 |
| | | /// @param completion 请求响应参数 |
| | | + (void)requestHttpsPost:(NSString *)apiPath |
| | | parameters:(NSMutableDictionary *)parameters |
| | | completion:(void (^)(ResponseData *responseData))completion{ |
| | | |
| | | [self requestHttpsBase:apiPath parameters:parameters completion:^(ResponseData *responseData) { |
| | | if (completion) { |
| | | if([responseData.code isEqual:@"10001"]){ |
| | | //刷新token |
| | | [self refreshHDLToken:^(BOOL isSuccess) { |
| | | if (isSuccess) { |
| | | //刷新token成功,重新请求一次 |
| | | [self requestHttpsBase:apiPath parameters:parameters completion:^(ResponseData *responseData) { |
| | | completion(responseData); |
| | | return; |
| | | }]; |
| | | } |
| | | }]; |
| | | } |
| | | |
| | | completion(responseData); |
| | | } |
| | | }]; |
| | | } |
| | | |
| | | /// 通用 请求服务器方法 |
| | | /// @param apiPath 接口地址 |
| | | /// @param parameters 请求参数 |
| | | /// @param completion 请求响应参数 |
| | | + (void)requestHttpsBase:(NSString *)apiPath |
| | | parameters:(NSMutableDictionary *)parameters |
| | | completion:(void (^)(ResponseData *responseData))completion{ |
| | | |
| | | //1,创建你得请求url |
| | | NSString *URL = @""; |
| | | if([GlobalKit shareKit].GlobalRequestHttpsHost == NULL){ |
| | | URL = @"https://test-gz.hdlcontrol.com"; |
| | | } |
| | | URL = [NSString stringWithFormat:@"%@%@", URL, apiPath]; |
| | | |
| | | // NSURLSessionDataTask * task=nil; |
| | | AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; |
| | | AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer]; |
| | | manager.responseSerializer = responseSerializer; |
| | | |
| | | //3.request |
| | | NSMutableURLRequest * request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"post" URLString:URL parameters:parameters error:nil]; |
| | | request.timeoutInterval = 10.0f; |
| | | [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; |
| | | [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; |
| | | [request setValue:GlobalKit.shareKit.hdlAccessToken forHTTPHeaderField:@"Authorization"]; |
| | | //4.dataTaskWithRequest |
| | | [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { |
| | | if (completion) { |
| | | ResponseData *responseData = [[ResponseData alloc] init]; |
| | | responseData.code = @"-1"; |
| | | |
| | | if(responseObject != NULL){ |
| | | //4.解析拿到的响应数据 |
| | | NSLog(@"%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]); |
| | | NSDictionary * outDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; |
| | | responseData = [[ResponseData alloc] initWithDictionary:outDic]; |
| | | } |
| | | |
| | | if (!responseData) { |
| | | responseData = [[ResponseData alloc] init]; |
| | | } |
| | | completion(responseData); |
| | | } |
| | | }]resume]; |
| | | // return task; |
| | | } |
| | | |
| | | /** |
| | | * 基础服务的接口都要校验sign |
| | | */ |
| | | +(NSMutableDictionary *)GetSignRequestDictionary:(NSMutableDictionary *)params{ |
| | | if(params == NULL){ |
| | | params =[NSMutableDictionary dictionary]; |
| | | } |
| | | UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000; |
| | | [params setValue:APP_KEY forKey:@"appKey"]; |
| | | [params setValue:@"" forKey:@"timestamp"]; |
| | | [params setValue:[NSString stringWithFormat:@"%llu",recordTime] forKey:@"timestamp"]; |
| | | //1.对KEY升序 |
| | | NSArray *keyArray = [params allKeys]; |
| | | NSArray *sortKeyArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { |
| | |
| | | } |
| | | //2.3 拼接SECRET_KEY |
| | | newString = [newString substringToIndex:[newString length] - 1]; |
| | | newString = [ newString stringByAppendingString: SECRET_KEY]; |
| | | newString = [newString stringByAppendingString: SECRET_KEY]; |
| | | //2.4 MD5转换+转小写 |
| | | if(newString == nil || newString == NULL){ |
| | | newString = @""; |
| | |
| | | return params; |
| | | } |
| | | |
| | | /** |
| | | MD5转换+转小写 |
| | | */ |
| | | + (NSString*)signMD5Encrypt:(NSString *)str |
| | | { |
| | | const char *cStr = [str UTF8String]; |
| | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | @end |