developer
2023-05-20 c1ffd99c4b60066774eb2c97b31e4aaa014e7f51
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
//  JSAPI.m
//  BuWanVideo2.0
//
//  Created by Aeline on 2021/5/15.
//  Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "JSAPI.h"
#import "dsbridge.h"
#import "NSString+YTH.h"
 
#define Version [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]  //build号
#define Package @"com.yeshi.buwanshequ.ios"  //相当于包名
 
@implementation JSAPI
 
- (NSString *)getVersion:(NSString *)msg {
    return Version;
}
 
- (NSString *)login:(NSString *)msg {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"h5Login" object:nil];
    return msg;
}
 
- (NSString *)logout:(NSString *)msg {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"buUserInfo"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"userOnLine"];
    [[NSUserDefaults standardUserDefaults] synchronize];
 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA" object:nil];
    return msg;
}
 
 
- (NSString *)getUserInfo:(NSString *)msg {
    NSMutableDictionary *parms = [[NSMutableDictionary alloc] init];
    NSDictionary *dictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"buUserInfo"];
    
    if (dictionary) {
        NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
        NSInteger nowDate = [self nowTimeSwitchTimestamp];
        if ([dictionary[@"vipExpireTime"] integerValue] > nowDate) {// vip时间大于
            data[@"isSVip"] = @(YES);
            
        } else {
            data[@"isSVip"] = @(NO);
        }
        data[@"code"] = dictionary[@"pptvCode"];
        data[@"nickname"] = dictionary[@"nickName"];
        data[@"isCoupon"] = @(NO);
        
        parms[@"code"] = @(1);
        parms[@"data"] = data;
        
    } else {
        parms[@"code"] = @(0);
    }
    NSString *json = [self jsonStringToJsonData:parms];
    return json;
}
 
 
- (NSString *)tryPlayFinish:(NSString *)msg {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"tryPlayFinish" object:nil];
    return msg;
}
 
- (NSString *)getUid:(NSString *)msg {
    NSString *ids = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"];
    return ids?:@"";
}
 
- (NSString *)getRequestBaseParams:(id)msg {
    
    NSMutableDictionary *parms = [NSMutableDictionary new];
    [parms addEntriesFromDictionary:msg];
    
    if (!msg[@"Method"]) {
        [parms setObject:@"1" forKey:@"Method"];
    }
    
    [parms setObject:@"ios" forKey:@"Platform"];
    NSString *sign;
    if ([YTHsharedManger startManger].Uid) {
        [parms setObject:[YTHsharedManger startManger].Uid forKey:@"Uid"];
        sign = [NSString stringWithFormat:@"%@%@%@", parms[@"Method"], [YTHsharedManger startManger].Uid,@"1"];
        
    }else{
        sign = [NSString stringWithFormat:@"%@%@%@", parms[@"Method"],[[UIDevice currentDevice].identifierForVendor UUIDString],@"1"];
    }
    
    [parms setObject:Version forKey:@"Version"];
    [parms setObject:Package forKey:@"Package"];
    [parms setObject:@"1" forKey:@"System"];
    [parms setObject:[NSString md5:sign] forKey:@"Sign"];
    [parms setObject:[[UIDevice currentDevice].identifierForVendor UUIDString] forKey:@"Device"];
    
    NSString *json = [self jsonStringToJsonData:parms];
    return json;
}
 
- (NSString *)showLoading:(id)msg {
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD show];
    });
    
    return @"";
}
 
- (NSString *)hideLoading:(id)msg {
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
    return @"";
}
 
- (NSString *)toast:(NSString *)msg {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
        [[self fetchCurrentViewController] presentViewController:alert animated:YES completion:^{
            //延时执行
            dispatch_queue_t q = dispatch_get_main_queue();
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.2 * NSEC_PER_SEC), q, ^{
                [alert dismissViewControllerAnimated:YES completion:nil];
            });
        }];
    });
    return @"";
}
 
- (NSString *)playFinish:(NSString *)msg {
    return msg;
}
 
- (void)buyVip:(NSString *)msg completionHandler:(JSCallback)completionHandler {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"buyVip" object:nil userInfo:@{@"code":msg?:@"",@"data" :completionHandler}];
    });
}
 
- (NSString *)vipPayFinish:(NSString *)msg {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"vipPayFinish" object:nil userInfo:@{@"code":msg}];
    });
    return msg;
}
 
//decrypt
- (NSString *)apiDecode:(NSString *)msg {
    NSString *result = [NSString decrypt:msg];
    return result;
}
 
- (NSString *)jsonStringToJsonData:(id)dic {
    if (!dic || [dic isEqual:[NSNull null]]) {
        return @"";
    }
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString = @"";
    
    if (!jsonData) {
        NSLog(@"%@",error);
        
    } else {
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0, jsonString.length};
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0, mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    //NSRange range3 = {0,mutStr.length};
    //去掉字符串中的换行符
    //[mutStr replaceOccurrencesOfString:@"\\" withString:@"" options:NSLiteralSearch range:range3];
    return mutStr;
}
 
- (NSInteger)nowTimeSwitchTimestamp {
    NSDate *datenow = [NSDate date]; // 现在时间
    NSInteger timeSp = [datenow timeIntervalSince1970]*1000;
    return timeSp;
}
 
- (UIViewController *)fetchCurrentViewController {
    UIViewController* currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else if ([currentViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
        } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
        } else {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            if (childViewControllerCount > 0) {
                currentViewController = currentViewController.childViewControllers.lastObject;
                return currentViewController;
            } else {
                return currentViewController;
            }
        }
    }
    return currentViewController;
}
@end