From b7fa25faef4decf509a998ae861f059896ea6093 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 19 四月 2021 16:35:22 +0800
Subject: [PATCH] 2021-04-19 1.配置说明截图,记录下来方便下次其他同事集成时候参考

---
 Demo/EZOpensdk_iOS_4.15.1_build20201104/Demo/EZOpenSDKDemo/Global/EZHttpUtil.m |  251 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 244 insertions(+), 7 deletions(-)

diff --git a/Demo/EZOpensdk_iOS_4.15.1_build20201104/Demo/EZOpenSDKDemo/Global/EZHttpUtil.m b/Demo/EZOpensdk_iOS_4.15.1_build20201104/Demo/EZOpenSDKDemo/Global/EZHttpUtil.m
index d969a2e..29b7426 100644
--- a/Demo/EZOpensdk_iOS_4.15.1_build20201104/Demo/EZOpenSDKDemo/Global/EZHttpUtil.m
+++ b/Demo/EZOpensdk_iOS_4.15.1_build20201104/Demo/EZOpenSDKDemo/Global/EZHttpUtil.m
@@ -9,19 +9,239 @@
 #import "EZHttpUtil.h"
 #import <Foundation/Foundation.h>
 #import <CommonCrypto/CommonDigest.h>
+#import "AFNetworking.h"
 
+#define TestRequestHttpsHost @"https://test-gz.hdlcontrol.com"
+#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"
 
+
+#define TIME_OUT 15.0f
+
 @implementation EZHttpUtil
 
+#pragma mark -鎺ュ彛璇锋眰閮ㄥ垎
 
-+(NSMutableDictionary *)GetSignRequestDictionary:(NSMutableDictionary *)params{
+
+/// sharedManager
++ (id)sharedManager {
+    static dispatch_once_t once;
+    static id instance;
+    dispatch_once(&once, ^{
+        instance = [self new];
+    });
+    return instance;
+}
+
+/**
+ *  @since 娌充笢鑾峰彇瀛愯处鍙穞oken鐨勬帴鍙�
+ *
+ *  @param block 鍥炶皟block
+ */
+- (void)getChildToken:(void (^)(NSString *accessToken))block{
+    
+    //2.璁剧疆璇锋眰鍙傛暟
+    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
+    [parameters setValue: [NSString stringWithFormat:@"%d", [GlobalKit shareKit].hdlPlatform] forKey:@"platform"];
+    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锛宔rror涓虹┖鏃惰〃绀烘坊鍔犳垚鍔�
+ *
+ */
+- (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 setValue: [NSString stringWithFormat:@"%d",[GlobalKit shareKit].hdlPlatform] forKey:@"platform"];
+    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锛宔rror涓虹┖鏃惰〃绀烘坊鍔犳垚鍔�
+ *
+ */
+- (void)deleteDeviceByHDL:(NSString *)deviceSerial completion:(void (^)(ResponseData *responseData))completion{
+    //1.璁剧疆璇锋眰鍙傛暟
+    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
+    [parameters setValue:deviceSerial forKey:@"deviceSerial"];
+    [parameters setValue: [NSString stringWithFormat:@"%d",[GlobalKit shareKit].hdlPlatform] forKey:@"platform"];
+    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 = [GlobalKit shareKit].GlobalRequestHttpsHost;
+    if([self stringIsNullOrEmpty:URL]){
+        URL = TestRequestHttpsHost;
+    }
+    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 = TIME_OUT;
+    [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.瑙f瀽鎷垮埌鐨勫搷搴旀暟鎹�
+                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;
+}
+
+/**
+ *  鍩虹鏈嶅姟鐨勬帴鍙i兘瑕佹牎楠宻ign
+ */
+-(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.瀵筀EY鍗囧簭
     NSArray *keyArray = [params allKeys];
     NSArray *sortKeyArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
@@ -40,9 +260,9 @@
     }
     //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){
+    if([self stringIsNullOrEmpty:newString]){
         newString = @"";
     }
     NSString* signstr = [self signMD5Encrypt:newString];
@@ -51,7 +271,23 @@
     return params;
 }
 
-+ (NSString*)signMD5Encrypt:(NSString *)str
+
+/// 瀛楃涓插垽绌�
+/// @param valueStr 浼犲叆鍒ゆ柇瀛楃
+- (BOOL)stringIsNullOrEmpty:(NSString *)valueStr
+{
+    if((valueStr == nil || [valueStr isKindOfClass:[NSNull class]] || valueStr.length == 0)){
+        return YES;
+    }else{
+        return NO;
+    }
+    
+}
+
+/**
+ MD5杞崲+杞皬鍐�
+ */
+- (NSString*)signMD5Encrypt:(NSString *)str
 {
     const char *cStr = [str UTF8String];
     unsigned char digest[CC_MD5_DIGEST_LENGTH];
@@ -71,8 +307,8 @@
 /// </summary>
 /// <param name="valueStr"></param>
 /// <returns></returns>
-+(bool)IfValueNeedSign:(NSString *)valueStr{
-    if (( (valueStr == nil || [valueStr isKindOfClass:[NSNull class]] || valueStr.length == 0))//鍒ょ┖瀛楃
+-(bool)IfValueNeedSign:(NSString *)valueStr{
+    if (( [self stringIsNullOrEmpty:valueStr])//鍒ょ┖瀛楃
         || ([[valueStr substringToIndex:1] isEqual:@"{"])//鍒ゆ柇鏄惁涓哄璞�
         || ([[valueStr substringToIndex:1] isEqual:@"["])//鍒ゆ柇鏄惁涓烘暟缁�
         ) {
@@ -81,4 +317,5 @@
     return true;
 }
 
+
 @end

--
Gitblit v1.8.0