ASCurrencyManager.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // ASCurrencyManager.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/12/9.
  6. //
  7. #import "ASCurrencyManager.h"
  8. @interface ASCurrencyManager ()
  9. @end
  10. @implementation ASCurrencyManager
  11. + (instancetype)shared {
  12. static id sharedInstance = nil;
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. sharedInstance = [[self alloc] init];
  16. });
  17. return sharedInstance;
  18. }
  19. - (instancetype)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. self.avaiCurencys = [NSArray array];
  24. }
  25. return self;
  26. }
  27. - (void)setSelectCurrency:(NSString *)sel {
  28. [NSUserDefaults.standardUserDefaults setValue:sel forKey:UserLocalCur];
  29. [NSNotificationCenter.defaultCenter postNotificationName:UserLocalCurUpdate object:nil userInfo:@{@"currency":sel}];
  30. }
  31. - (NSString *)currentCur {
  32. NSString *localCur = [NSUserDefaults.standardUserDefaults stringForKey:UserLocalCur];
  33. if (localCur == nil || localCur.length == 0) {
  34. localCur = @"USD";
  35. }
  36. return localCur;
  37. }
  38. //获取当前汇率 (保留两位小数的字符串)
  39. - (NSString *)getCurrentCurrencyRate {
  40. NSString *rateStr = @"1";
  41. for (int i = 0; i < self.exchange_rates.count; i++) {
  42. NSDictionary *rateDic = [self.exchange_rates objectAtIndex:i];
  43. NSString *tempCurrency = rateDic[@"currency_to"];
  44. if ([self.currentCur isEqualToString:tempCurrency]) {
  45. rateStr = rateDic[@"rate"];
  46. break;
  47. }
  48. }
  49. return rateStr;
  50. }
  51. - (void)getAllCurrencyData {
  52. __weak typeof(self) weakSelf = self;
  53. [ASNetTools.shared getWithPath:getAllCurrencyUrl param:@{} success:^(id _Nonnull json) {
  54. NSDictionary *dic = (NSDictionary *)json;
  55. weakSelf.avaiCurencys = dic[@"available_currency_codes"];
  56. weakSelf.exchange_rates = dic[@"exchange_rates"];
  57. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  58. weakSelf.avaiCurencys = [NSMutableArray array];
  59. }];
  60. }
  61. @end