JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
//  OpenApiClient.m
//  LCOpenSDKDemo
//
//  Created by bzy on 17/3/21.
//  Copyright © 2017年 lechange. All rights reserved.
//
 
#import "OpenApiClient.h"
#import "OpenApiNetworking.h"
#import "NSDictionary+LCOpenSDK.h"
#import "NSString+LCOpenSDK.h"
 
@implementation OpenApiClient
 
+(NSString *)getRandomNumber
{
    NSString *str = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    NSString *randomStr = @"";
    for (int i=0; i<32; i++) {
        int rand = arc4random()%str.length;
        randomStr = [randomStr stringByAppendingString:[str substringWithRange:NSMakeRange(rand, 1)]];
    }
    return randomStr;
}
 
+(NSString *)getTime
{
    NSString * tm = [NSString stringWithFormat:@"%ld",time(NULL)];
    return tm;
}
 
static OpenApiClient* _instance = nil;
+ (instancetype)shareMyInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
 
- (void)setParams:(NSString*)host port:(NSInteger)port appId:(NSString*)appId appSecret:(NSString*)appSecret method:(NSString *)method
{
    _host = host;
    _port = port;
    _appId = appId;
    _appSecret = appSecret;
    _method = method;
    if (_port == 80) {
          _url = [NSString stringWithFormat:@"http://%@:%ld%@", host, (long)port,_method];
    }
    else {
          _url = [NSString stringWithFormat:@"https://%@:%ld%@", host, (long)port,_method];
    } 
}
 
- (void)setMethod:(NSString *)method {
    _method = [method copy];
    if (_port == 80) {
          _url = [NSString stringWithFormat:@"http://%@:%ld%@", _host, (long)_port,_method];
    }
    else {
          _url = [NSString stringWithFormat:@"https://%@:%ld%@", _host, (long)_port,_method];
    }
}
 
- (NSDictionary*)request:(NSDictionary*)req;
{
    [self sign:req];
    NSDictionary *dic = @{@"id": [NSString stringWithFormat:@"%@",@(g_id++)], @"system" : _system, @"params" : req};
    NSString *request = [ self dictionaryToJSONString:dic];
    NSString *resp = [[OpenApiNetworking shareMyInstance] requestByPost:_url params:request];
    return [resp toDictionary];
}
 
static NSInteger g_id = 0;
- (void)sign:(NSDictionary*)req
{
    if (!_appId || !_appSecret) {
        NSLog(@"appId or appSecret is nil");
        return;
    }
    NSString * signOrg;
//    NSArray *keys = [req allKeys];
//    for (NSString* key in keys) {
//        NSString* value = [req objectForKey:key];
//        if (!signOrg) {
//            signOrg = [NSString stringWithFormat:@"%@:%@",key,value];
//        }else{
//            signOrg = [signOrg stringByAppendingFormat:@",%@:%@",key,value];
//        }
//    }
    NSString *time = [OpenApiClient getTime];
    NSString *nonce = [OpenApiClient getRandomNumber];
    signOrg = [NSString stringWithFormat:@"time:%@,nonce:%@,appSecret:%@", time, nonce, _appSecret];
    NSString *sign = [signOrg stringToMD5];
    _system = @{ @"appId": _appId,@"nonce": nonce, @"sign" :sign, @"time":time, @"ver" : @"1.0"};
}
 
- (NSString *)dictionaryToJSONString:(NSDictionary *)dictionary
 
{
    NSError *error = nil;
    NSData *jsonData;
    if (dictionary) {
        jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
    }
    NSString *jsonString;
    if (jsonData) {
        jsonString  = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}
 
- (void)cancelRequest {
    [[OpenApiNetworking shareMyInstance] cancelRequest];
}
 
@end