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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//
//  Copyright © 2015 dahua. All rights reserved.
//
 
#import <LCBaseModule/DHUserManager.h>
#import <GTMBase64/GTMBase64.h>
#import <SDWebImage/SDWebImageDownloader.h>
#import <SDWebImage/SDImageCache.h>
#import <LCBaseModule/DHFileManager.h>
#import <LCBaseModule/DHImageLoaderManager.h>
#import <LCBaseModule/DHModuleConfig.h>
 
@implementation DHUserManager
 
@synthesize language = _language, sound = _sound, timeFormat = _timeFormat, timezoneOffset = _timezoneOffset, bAlertBindEmail = _bAlertBindEmail;
 
static DHUserManager *userManager = nil;
+ (DHUserManager *)shareInstance
{
    if(userManager == nil)
    {
        userManager = [[DHUserManager alloc]init];
    }
 
    return userManager;
}
 
- (id)init
{
    if (self = [super init])
    {
        _isAllowCellularPlay = NO;
        _showExperiencePlanGuide = YES;
        _messageState = YES;
        _messageTime = @"00:00-23:59";
        _bAlertBindEmail = YES;
        _showSearchLightGuide = YES;
        _isShowUserExperiencePlan = NO;
        _isSupportAd = NO;
        _openUserExperience = NO;
        
        // 默认未开启调试模式
        _openDebugModeTime = 0;
    }
    
    return self;
}
 
//获取配置
- (void)getUserConfigFile
{
    // config
    NSString *configfilepath = [DHFileManager userConfigFilePath];
    NSString *configStr = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:configfilepath]
                                                   encoding:NSUTF8StringEncoding
                                                      error:NULL];
    
    NSData *desData = [GTMBase64 decodeString:configStr];
    NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:desData];
    
    id val = [dic objectForKey:@"showExperiencePlanGuide"];
    if (val != NULL) {
        _showExperiencePlanGuide = [val boolValue];
    }
    
    val = [dic objectForKey:@"isAllowCellularPlay"];
    if (val != NULL) {
        _isAllowCellularPlay = [val boolValue];
    }
    
    val = [dic objectForKey:@"messageState"];
    if (val != NULL) {
        _messageState = [val boolValue];
    }
    
    val = [dic objectForKey:@"messageTime"];
    if (val != NULL) {
        _messageTime = val;
    }
    
    val = [dic objectForKey:@"showSearchLightGuide"];
    if (val != NULL) {
        _showSearchLightGuide = [val boolValue];
    }
    
    val = [dic objectForKey:@"openUserExperience"];
    if (val != NULL) {
        _openUserExperience = [val boolValue];
    }
 
    val = [dic objectForKey:@"isShowUserExperiencePlan"];
    if (val != NULL) {
        _isShowUserExperiencePlan = [val boolValue];
    }
    
    
    val = [dic objectForKey:@"messageTime"];
    if (val != NULL) {
        _messageTime = val;
    }
    
    val = [dic objectForKey:@"isSupportAd"];
    if (val != NULL) {
        _isSupportAd = [val boolValue];
    }
    
    _countryCode = dic[@"countryCode"];
    _avatarUrl = dic[@"avatarUrl"];
    _avatarMd5 = dic[@"avatarMd5"];
    _nickname = dic[@"nickname"];
    _wxNickname = dic[@"wxNickname"];
    _fbNickname = dic[@"fbNickname"];
    _appleNickname = dic[@"appleNickname"];
    _phone = dic[@"phone"];
    _email = dic[@"email"];
    
    //时区数据
    _beginSummer = dic[@"beginSummer"];
    _endSummer = dic[@"endSummer"];
    _areaIndex = [dic[@"areaIndex"] intValue];
    _timeZoneIndex = [dic[@"timeZoneIndex"] intValue];
    _setDefault = [dic[@"setDefault"] boolValue];
    
    _mSSIDMutableArray = dic[@"mSSIDMutableArray"];
    _deviceVersionDictionary = dic[@"deviceVersionDictionary"];
    _timeFormat = dic[@"timeFormat"];
    _sound = dic[@"sound"];
    _timezoneOffset = [dic[@"timezoneOffset"] intValue];
    _bAlertBindEmail = [dic[@"bAlertBindEmail"] boolValue];
    _msgId = [dic[@"msgId"] longLongValue];
    _openDebugModeTime = [[dic objectForKey:@"openDebugModeTime"] doubleValue];
}
 
- (void)saveUserConfigFile
{
    // config
    NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    
    NSNumber *showExperiencePlanGuide = [NSNumber numberWithBool:_showExperiencePlanGuide];
    [dic setObject:showExperiencePlanGuide forKey:@"showExperiencePlanGuide"];
    
    NSNumber *isAllowCellularPlayNumber = [NSNumber numberWithBool:_isAllowCellularPlay];
    [dic setObject:isAllowCellularPlayNumber forKey:@"isAllowCellularPlay"];
    
    NSNumber *showSearchLightGuide = [NSNumber numberWithBool:_showSearchLightGuide];
    [dic setObject:showSearchLightGuide forKey:@"showSearchLightGuide"];
    
    NSNumber *openUserExperienceNumber = [NSNumber numberWithBool:_openUserExperience];
    [dic setObject:openUserExperienceNumber forKey:@"openUserExperience"];
    
    NSNumber *messageState = [NSNumber numberWithBool:_messageState];
    [dic setObject:messageState forKey:@"messageState"];
    
    if (_openDebugModeTime > 0) {
        [dic setObject:[NSNumber numberWithDouble:_openDebugModeTime] forKey:@"openDebugModeTime"];
    }
 
    NSNumber *isShowUserExperiencePlanNumber = [NSNumber numberWithBool:_isShowUserExperiencePlan];
    [dic setObject:isShowUserExperiencePlanNumber forKey:@"isShowUserExperiencePlan"];
 
    NSNumber *isSupportAdNumber = [NSNumber numberWithBool:_isSupportAd];
    [dic setObject:isSupportAdNumber forKey:@"isSupportAd"];
    
    if (_openDebugModeTime > 0) {
        [dic setObject:[NSNumber numberWithDouble:_openDebugModeTime] forKey:@"openDebugModeTime"];
    }
 
    if(_messageTime.length > 0) {
        NSString * messageTime = [NSString stringWithFormat:@"%@",_messageTime];
        [dic setObject:messageTime forKey:@"messageTime"];
    }
    
    dic[@"countryCode"] = _countryCode;
    dic[@"nickname"] = _nickname;
    dic[@"avatarUrl"] = _avatarUrl;
    dic[@"avatarMd5"] = _avatarMd5;
    dic[@"wxNickname"] = _wxNickname;
    dic[@"fbNickname"] = _fbNickname;
    dic[@"appleNickname"] = _appleNickname;
    dic[@"phone"] = _phone;
    dic[@"email"] = _email;
    dic[@"language"] = _language;
    dic[@"timeFormat"] = _timeFormat;
    dic[@"sound"] = _sound;
    dic[@"timezoneOffset"] = [NSString stringWithFormat:@"%d",_timezoneOffset];
    dic[@"bAlertBindEmail"] = [NSString stringWithFormat:@"%d",_bAlertBindEmail];
    
    //时区设置;
    [dic setValue:_beginSummer forKey:@"beginSummer"];
    [dic setValue:_endSummer forKey:@"endSummer"];
    [dic setValue:@(_areaIndex) forKey:@"areaIndex"];
    [dic setValue:@(_timeZoneIndex) forKey:@"timeZoneIndex"];
    [dic setValue:@(_setDefault) forKey:@"setDefault"];
    
    [dic setValue:_mSSIDMutableArray forKey:@"mSSIDMutableArray"];
    [dic setValue:_deviceVersionDictionary forKey:@"deviceVersionDictionary"];
    [dic setValue:@(_msgId) forKey:@"msgId"];
    
    NSData *config2Data = [NSKeyedArchiver archivedDataWithRootObject:dic];
    NSString *config2Str = [GTMBase64 stringByEncodingData:config2Data];
    [config2Str writeToFile:[DHFileManager userConfigFilePath]
                 atomically:YES
                   encoding:NSUTF8StringEncoding
                      error:NULL];
}
 
- (void)syncUserCountryInfo
{
    if(![DHUserManager shareInstance].countryCode || [DHUserManager shareInstance].countryCode.length == 0)
    {
        [[NSUserDefaults standardUserDefaults] setValue:@(1) forKey:@"PopCountryInfo"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setValue:@(0) forKey:@"PopCountryInfo"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
 
- (void)clearCacheAvatar:(NSString *)avatarUrl completion:(void(^)(NSString *acheKey))completion
{
    NSString *cacheKey = [DHImageLoaderManager getImageCacheKey:_avatarUrl];
    //旧的头像地址对应的缓存key值
    [[SDImageCache sharedImageCache] removeImageForKey:cacheKey withCompletion:^{
        NSLog(@"DHUserManager::Delete cache avatar image %@", cacheKey);
        
        //为解决新旧头像地址不一致时【未设置时、已设置时,头像地址不同】,缓存key值不同的问题
        NSString *newCacheKey = [DHImageLoaderManager getImageCacheKey:avatarUrl];
        if (completion) {
            completion(newCacheKey);
        }
    }];
}
 
- (void)clearDiskAndCacheAvatar:(NSString *)avatarUrl completion:(void(^)(NSString *acheKey))completion {
    NSString *cacheKey = [DHImageLoaderManager getImageCacheKey:_avatarUrl];
    [[SDImageCache sharedImageCache] removeImageForKey:cacheKey fromDisk:YES withCompletion:^{
        NSLog(@"DHUserManager::Delete cache avatar image %@", cacheKey);
        
        //为解决新旧头像地址不一致时【未设置时、已设置时,头像地址不同】,缓存key值不同的问题
        NSString *newCacheKey = [DHImageLoaderManager getImageCacheKey:avatarUrl];
        if (completion) {
            completion(newCacheKey);
        }
    }];
}
 
- (void)downloadAvatar:(NSString *)avatarUrl
{
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:avatarUrl] options:0  progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        if (image) {
            //先清空旧的图片,再进行覆盖写入
            NSString *cacheKey = [DHImageLoaderManager getImageCacheKey:avatarUrl];
            [[SDImageCache sharedImageCache] removeImageForKey:cacheKey withCompletion:^{
                [[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:cacheKey];
            }];
        }
    }];
}
 
-(void)clearData
{
    NSString *userFilePath = [DHFileManager userFolder];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:userFilePath])
    {
        [fm removeItemAtPath:userFilePath error:nil];
    }
    userManager = nil;
}
 
#pragma mark - Set Properties
- (void)setIsSupportAd:(BOOL)isSupportAd {
    _isSupportAd = isSupportAd;
    [self saveUserConfigFile];
}
 
- (void)setEmail:(NSString *)email
{
    NSLog(@"🍎🍎🍎 %@:: Set email %@", NSStringFromClass([self class]), email);
    _email = email;
    [self saveUserConfigFile];
}
 
- (void)setPhone:(NSString *)phone
{
    NSLog(@"🍎🍎🍎 %@:: Set phone %@", NSStringFromClass([self class]), phone);
    _phone = phone;
    [self saveUserConfigFile];
}
 
- (void)setNickname:(NSString *)nickname
{
    _nickname = nickname;
    [self saveUserConfigFile];
}
 
- (void)setCountryCode:(NSString *)countryCode
{
    _countryCode = countryCode;
    [self saveUserConfigFile];
}
 
- (void)setAvatarUrl:(NSString *)avatarUrl
{
    _avatarUrl = avatarUrl;
    [self saveUserConfigFile];
}
 
- (void)setAvatarMd5:(NSString *)avatarMd5
{
    _avatarMd5 = avatarMd5;
    [self saveUserConfigFile];
}
 
- (void)setWxNickname:(NSString *)wxNickname
{
    _wxNickname = wxNickname;
    [self saveUserConfigFile];
}
 
- (void)setFbNickname:(NSString *)fbNickname
{
    _fbNickname = fbNickname;
    [self saveUserConfigFile];
}
 
- (void)setAppleNickname:(NSString *)appleNickname
{
    _appleNickname = appleNickname;
    [self saveUserConfigFile];
}
 
- (void)setLanguage:(NSString *)language
{
    _language = language;
    [self saveUserConfigFile];
}
 
- (void)setTimeFormat:(NSString *)timeFormat
{
    _timeFormat = timeFormat;
    [self saveUserConfigFile];
}
 
- (void)setSound:(NSString *)sound
{
    _sound = sound;
    [self saveUserConfigFile];
}
 
- (void)setTimezoneOffset:(int)timezoneOffset
{
    _timezoneOffset = timezoneOffset;
    [self saveUserConfigFile];
}
 
- (void)setBAlertBindEmail:(BOOL)bAlertBindEmail
{
    _bAlertBindEmail = bAlertBindEmail;
    [self saveUserConfigFile];
}
 
- (void)setMsgId:(int64_t)msgId{
    _msgId = msgId;
    [self saveUserConfigFile];
}
 
-(void)setIsAllowCellularPlay:(BOOL)isAllowCellularPlay
{
    _isAllowCellularPlay = isAllowCellularPlay;
    [self saveUserConfigFile];
}
 
- (void)setIsShowUserExperiencePlan:(BOOL)isShowUserExperiencePlan {
    _isShowUserExperiencePlan = isShowUserExperiencePlan;
    [self saveUserConfigFile];
}
 
- (void)setOpenDebugModeTime:(NSTimeInterval)openDebugModeTime {
    _openDebugModeTime = openDebugModeTime;
    [self saveUserConfigFile];
}
 
- (NSString *)language
{
    NSString *currentLanguage = [NSLocale preferredLanguages].firstObject;
    if ([currentLanguage hasPrefix:@"zh"]) {
        currentLanguage = [currentLanguage containsString:@"zh-Hant"] ? @"zh-TW" : @"zh-CN";
    }
    
    _language = currentLanguage;
    return _language;
}
 
- (NSString *)sound
{
    if (!_sound || [_timeFormat isEqualToString:@""])
    {
        _sound = @"";
    }
    return _sound;
}
 
- (NSString *)timeFormat
{
    if (!_timeFormat || [_timeFormat isEqualToString:@""])
    {
        _timeFormat = @"yyyy-MM-dd HH:mm:ss";
    }
    return _timeFormat;
}
 
- (int)timezoneOffset
{
    if (_timezoneOffset == 0)
    {
        NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone] ;///获取当前时区信息
        NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMT];
        _timezoneOffset = (int)sourceGMTOffset;
    }
    return _timezoneOffset;
}
 
- (BOOL)bAlertBindEmail
{
    if (_bAlertBindEmail)
    {
        return !(_email.length > 0);
    }
    else
    {
        return _bAlertBindEmail;
    }
}
 
- (void)setOpenUserExperience:(BOOL)openUserExperience {
    _openUserExperience = openUserExperience;
    [self saveUserConfigFile];
}
 
- (BOOL)openDebugMode {
    if (_openDebugModeTime <= 0) {
        // 没有开启时间,直接返回false
        return false;
    }
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval oneDay = 24*60*60;
    if (currentTime - _openDebugModeTime > oneDay) {
        // 超过一天,开关关闭
        _openDebugModeTime = 0;
        return false;
    }else {
        // 开关为开启状态
        return true;
    }
}
 
#pragma mark - Device
 
- (BOOL)deviceCanUpdateWithDeviceId:(NSString *)deviceId currentVersion:(NSString *)currentVersion newVersion:(NSString *)newVersion {
    BOOL canUpdate = YES;
    if(_deviceVersionDictionary == nil) {
        _deviceVersionDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    
    NSString *oldVersion = [_deviceVersionDictionary objectForKey:deviceId];
    
    if (oldVersion == NULL) {
        oldVersion = currentVersion;
        [_deviceVersionDictionary setValue:currentVersion forKey:deviceId];
        [self saveUserConfigFile];
    }
    
    if ([oldVersion isEqualToString:newVersion]) {
        canUpdate =  NO;
    }
    
    return canUpdate;
}
 
- (void)deviceAddIgnoreVersionWithDeviceId:(NSString *)deviceId version:(NSString *)version {
    [_deviceVersionDictionary setValue:version forKey:deviceId];
    [self saveUserConfigFile];
}
 
#pragma mark - SSID
- (BOOL)ssidIsSaved:(NSString *)ssid
{
    if (_mSSIDMutableArray.count == 0)
    {
        return NO;
    }
    
    BOOL isSaved = NO;
    for (NSDictionary *dict in _mSSIDMutableArray)
    {
        NSString *mSSID = dict[@"mSSID"];
        if ([mSSID isEqualToString:ssid])
        {
            isSaved = YES;
            break;
        }
    }
    
    return isSaved;
}
 
- (NSString *)ssidPwdBy:(NSString *)ssid {
    if (_mSSIDMutableArray.count == 0) {
        return nil;
    }
    
    NSString *ssidPwd = nil;
    for (NSDictionary *dict in _mSSIDMutableArray)
    {
        NSString *mSSID = dict[@"mSSID"];
        if ([mSSID isEqualToString:ssid])
        {
            ssidPwd = dict[@"mSSIDPWD"];
            break;
        }
    }
    
    return ssidPwd;
}
 
- (void)addSSID:(NSString *)ssid ssidPwd:(NSString *)ssidPwd
{
    if (ssid == nil) {
        ssid = @"";
    }
    
    if (ssidPwd == nil) {
        ssidPwd = @"";
    }
    
    NSDictionary *dict = @{@"mSSID":ssid, @"mSSIDPWD":ssidPwd};
    
    if (_mSSIDMutableArray == nil)
    {
        _mSSIDMutableArray = [NSMutableArray arrayWithCapacity:3];
    }
    
    //如果有相同的,先删除旧的
    if ([self ssidPwdBy:ssid]) {
        [self removeSSID:ssid];
    }
    
    [_mSSIDMutableArray addObject:dict];
    [self saveUserConfigFile];
}
 
- (void)removeSSID:(NSString *)ssid
{
    if (_mSSIDMutableArray.count == 0)
    {
        return;
    }
    
    NSDictionary *ssidDict = [NSDictionary dictionary];
    for (NSDictionary *dict in _mSSIDMutableArray)
    {
        NSString *mSSID = dict[@"mSSID"];
        if ([mSSID isEqualToString:ssid])
        {
            ssidDict = dict;
            break;
        }
    }
    
    [_mSSIDMutableArray removeObject:ssidDict];
    [self saveUserConfigFile];
}
 
- (void)clearCachedSSID {
    [_mSSIDMutableArray removeAllObjects];
    [self saveUserConfigFile];
}
 
- (void)resetMemoryCache {
    _email = @"";
    _phone = @"";
    _nickname = @"";
    _avatarMd5 = @"";
    _avatarUrl = @"";
    _countryCode = @"";
    _fbNickname = @"";
    _openUserExperience = NO;
    _isSupportAd = NO;
    _showExperiencePlanGuide = YES;
    [_mSSIDMutableArray removeAllObjects];
}
 
@end