1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- //
- // ASCurrencyManager.m
- // Asteria
- //
- // Created by iOS on 2023/12/9.
- //
- #import "ASCurrencyManager.h"
- @interface ASCurrencyManager ()
- @end
- @implementation ASCurrencyManager
- + (instancetype)shared {
- static id sharedInstance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedInstance = [[self alloc] init];
- });
- return sharedInstance;
- }
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- self.avaiCurencys = [NSArray array];
- }
- return self;
- }
- - (void)setSelectCurrency:(NSString *)sel {
- [NSUserDefaults.standardUserDefaults setValue:sel forKey:UserLocalCur];
- [NSNotificationCenter.defaultCenter postNotificationName:UserLocalCurUpdate object:nil userInfo:@{@"currency":sel}];
- }
- - (NSString *)currentCur {
- NSString *localCur = [NSUserDefaults.standardUserDefaults stringForKey:UserLocalCur];
- if (localCur == nil || localCur.length == 0) {
- localCur = @"USD";
- }
- return localCur;
- }
- //获取当前汇率 (保留两位小数的字符串)
- - (NSString *)getCurrentCurrencyRate {
- NSString *rateStr = @"1";
- for (int i = 0; i < self.exchange_rates.count; i++) {
- NSDictionary *rateDic = [self.exchange_rates objectAtIndex:i];
- NSString *tempCurrency = rateDic[@"currency_to"];
-
- if ([self.currentCur isEqualToString:tempCurrency]) {
- rateStr = rateDic[@"rate"];
- break;
- }
- }
- return rateStr;
- }
- - (void)getAllCurrencyData {
- __weak typeof(self) weakSelf = self;
- [ASNetTools.shared getWithPath:getAllCurrencyUrl param:@{} success:^(id _Nonnull json) {
- NSDictionary *dic = (NSDictionary *)json;
- weakSelf.avaiCurencys = dic[@"available_currency_codes"];
-
- weakSelf.exchange_rates = dic[@"exchange_rates"];
-
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
-
- weakSelf.avaiCurencys = [NSMutableArray array];
- }];
- }
- @end
|