JLChen
2021-05-17 a722e767f98042d5ef6259d2dde7854c925e4167
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
//
//  LCApiKit.m
//  LCOnDemo
//
//  Created by 陈嘉乐 on 2021/4/26.
//
 
#import "LCApiKit.h"
#import <LCOpenSDKDynamic/LCOpenSDKDynamic.h>
 
#define DEFAULTHOSTAPICHN @"https://openapi.lechange.cn:443" //中国大陆(正式)
#define DEFAULTHOSTAPIOVS @"https://openapi.easy4ip.com:443" //海外(正式)
 
 
@interface LCApiKit ()
 
 
@end
 
 
 
@implementation LCApiKit
 
/**
 如果为单例,取得对应的单例
 
 @return 单例对象
 */
+(instancetype)sharedInstance{
    static LCApiKit * sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[LCApiKit alloc] init];
        sharedInstance.isChinaMainland = NO;
    });
    return sharedInstance;
}
 
/// SDK连接时的HostApi
- (NSString *)LCHostApi{
    // 若没有自定义地址直接返回默认地址
    self.sdkHostApi = ((self.sdkHostApi == nil || [self.sdkHostApi isEqualToString:@""]) ? (self.isChinaMainland ? DEFAULTHOSTAPICHN : DEFAULTHOSTAPIOVS) : self.sdkHostApi);
    return self.sdkHostApi;
}
 
/// SDK连接时的Host
- (NSString *)LCSDKHost {
    
    NSString *host = [NSString stringWithFormat:@"%@/openapi", [self LCHostApi]];
    NSString *textStr = [[[[[[host componentsSeparatedByString:@"//"] objectAtIndex:1] componentsSeparatedByString:@"/"] objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:0];
    return textStr;
}
 
/// SDK连接时的Port
- (NSInteger)LCSDKPort {
    NSString *host = [self LCHostApi];
    NSString *regex = @"(:[0-9]{1,4})";
    NSRegularExpression *regular = [[NSRegularExpression alloc]initWithPattern:regex options:NSRegularExpressionDotMatchesLineSeparators error:nil];
    NSArray *resultArray = [regular matchesInString:host options:0 range:NSMakeRange(0, host.length)];
    if (resultArray.count == 0) {
        return [host containsString:@"https"]?443:80;
    }
    NSTextCheckingResult *result = resultArray[0];
    NSString *textStr = [[host substringWithRange:result.range] substringFromIndex:1];
    return [textStr integerValue];
}
 
/// 初始化LCOpenSDK_Api
- (void)initSDKOpenApi:(NSString *)lcToken
{
    self.lcSdkToken = lcToken;
    LCOpenSDK_ApiParam *param = [LCOpenSDK_ApiParam new];
    param.procotol = [[self LCHostApi] containsString:@"https"] ? 1 : 0;
    param.addr = [self LCSDKHost];
    param.port = [self LCSDKPort];
    param.token = self.lcSdkToken;
    [[LCOpenSDK_Api shareMyInstance] initOpenApi:param];
    NSLog(@"initSDKOpenApi: %@,@%ld", [self LCSDKHost], (long)[self LCSDKPort]);
}
 
///// 拼接错误信息
//- (NSString *) getErrorMesWithCode:(NSString *)message code:(int)code
//{
//    return [NSString stringWithFormat:@"%@(%d)",message, code];
//}
@end