Commit 856c99606b19b2fe7f41c2a890c7466bc8def18f
1 parent
eeaa0622
?
Showing
2 changed files
with
95 additions
and
0 deletions
LiveVideoCloud/Classes/Sections/homeSections/Controller/LiveListService.h
0 → 100644
| 1 | +// | ||
| 2 | +// LiveLIstService.h | ||
| 3 | +// LiveVideoCloud | ||
| 4 | +// | ||
| 5 | +// Created by cnliveJunBo on 17/3/28. | ||
| 6 | +// Copyright © 2017年 cnlive. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import "HttpService.h" | ||
| 10 | + | ||
| 11 | +@interface LiveListService : HttpService | ||
| 12 | + | ||
| 13 | ++ (NSURLSessionDataTask *)getLiveListWithHandler:(completionHandler)handler; | ||
| 14 | + | ||
| 15 | ++ (NSString*)sha1:(NSString *)string; | ||
| 16 | + | ||
| 17 | ++ (NSString *)sortParameters:(NSDictionary*)parameters; | ||
| 18 | + | ||
| 19 | +@end |
LiveVideoCloud/Classes/Sections/homeSections/Controller/LiveListService.m
0 → 100644
| 1 | +// | ||
| 2 | +// LiveLIstService.m | ||
| 3 | +// LiveVideoCloud | ||
| 4 | +// | ||
| 5 | +// Created by cnliveJunBo on 17/3/28. | ||
| 6 | +// Copyright © 2017年 cnlive. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import "LiveListService.h" | ||
| 10 | +#import "CommonCrypto/CommonDigest.h" | ||
| 11 | + | ||
| 12 | +@implementation LiveListService | ||
| 13 | + | ||
| 14 | ++ (NSURLSessionDataTask *)getLiveListWithHandler:(completionHandler)handler | ||
| 15 | +{ | ||
| 16 | + NSInteger time = [[NSDate date] timeIntervalSince1970]; | ||
| 17 | + NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time]; | ||
| 18 | + NSMutableDictionary *parameter=[@{@"platform_id": [NSBundle mainBundle].bundleIdentifier, | ||
| 19 | + @"timestamp": timeString, | ||
| 20 | + @"appId": UserAppId} mutableCopy]; | ||
| 21 | + NSString *string = [NSString stringWithFormat:@"%@&key=%@", [self sortParameters:parameter], UserAppKey]; | ||
| 22 | + NSString *signString = [[self sha1:string] uppercaseString]; | ||
| 23 | + [parameter setObject:signString forKey:@"sign"]; | ||
| 24 | + | ||
| 25 | + NSMutableDictionary *params = parameter.mutableCopy; | ||
| 26 | + completionHandler compleionHandler = ^(id data,NSError *error) { | ||
| 27 | + handler(data,error); | ||
| 28 | + }; | ||
| 29 | + return [[HttpService service] sendRequestWithHttpMethod:E_HTTP_REQUEST_METHOD_GET URLPath:[ZXServerUrl getLiveListURL] parameters:params completionHandler:compleionHandler]; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +//sha1加密 | ||
| 33 | ++ (NSString*)sha1:(NSString *)string | ||
| 34 | +{ | ||
| 35 | + NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; | ||
| 36 | + | ||
| 37 | + uint8_t digest[CC_SHA1_DIGEST_LENGTH]; | ||
| 38 | + | ||
| 39 | + CC_SHA1(data.bytes, data.length, digest); | ||
| 40 | + | ||
| 41 | + NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; | ||
| 42 | + | ||
| 43 | + for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) | ||
| 44 | + [output appendFormat:@"%02x", digest[i]]; | ||
| 45 | + | ||
| 46 | + while ([[output substringToIndex:1] isEqualToString:@"0"]) { | ||
| 47 | + output = [[NSMutableString alloc] initWithString:[output substringFromIndex:1]]; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + return output; | ||
| 51 | +} | ||
| 52 | +//参数排序 | ||
| 53 | ++ (NSString *)sortParameters:(NSDictionary*)parameters | ||
| 54 | +{ | ||
| 55 | + //对所有传入参数按照字段名的 ASCII 码从小到大排序 | ||
| 56 | + NSArray *keyArr=[parameters allKeys]; | ||
| 57 | + NSArray *arr = [keyArr sortedArrayUsingSelector:@selector(compare:)]; | ||
| 58 | + | ||
| 59 | + NSMutableString *string1 = [[NSMutableString alloc]init]; | ||
| 60 | + for (int i=0; i<arr.count; i++) { | ||
| 61 | + | ||
| 62 | + NSString *parametersString = parameters[[arr objectAtIndex:i]]; | ||
| 63 | + if (parametersString.length > 0) { | ||
| 64 | + [string1 appendString:[NSString stringWithFormat:@"%@=%@&",[arr objectAtIndex:i],parameters[[arr objectAtIndex:i]]]]; | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + if (string1.length > 0) { | ||
| 69 | + [string1 deleteCharactersInRange:NSMakeRange(string1.length-1, 1)]; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + return string1; | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +@end |