ZYLService.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // ZYLService.m
  3. // HeiHuaBaiHua
  4. //
  5. // Created by HeiHuaBaiHua on 16/6/2.
  6. // Copyright © 2016年 HeiHuaBaiHua. All rights reserved.
  7. //
  8. #import "ZYLService.h"
  9. @interface ZYLService ()
  10. @property (assign, nonatomic) ZYLServiceEnvironment environment;
  11. @end
  12. @implementation ZYLService
  13. #pragma mark - Interface
  14. static ZYLService *currentService;
  15. static dispatch_semaphore_t lock;
  16. + (ZYLService *)currentService {
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. lock = dispatch_semaphore_create(1);
  20. currentService = [ZYLService environmentWithType:BuildServiceEnvironment];
  21. });
  22. return currentService;
  23. }
  24. + (void)switchToEnvironment:(ZYLServiceEnvironment)environment {
  25. dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
  26. NSString *environmentString = @"";
  27. switch (environment) {
  28. case ZYLServiceEnvironmentTest:
  29. environmentString = @"ZYLServiceEnvironmentTest";
  30. break;
  31. case ZYLServiceEnvironmentDevelop:
  32. environmentString = @"ZYLServiceEnvironmentDevelop";
  33. break;
  34. case ZYLServiceEnvironmentRelease:
  35. environmentString = @"ZYLServiceEnvironmentRelease";
  36. break;
  37. case ZYLServiceEnvironmentTestGy:
  38. environmentString = @"ZYLServiceEnvironmentTestGy";
  39. break;
  40. }
  41. [[NSUserDefaults standardUserDefaults] setObject:environmentString forKey:@"ZYLServiceEnvironment"];
  42. [[NSUserDefaults standardUserDefaults] synchronize];
  43. currentService = [ZYLService environmentWithType:environment];
  44. dispatch_semaphore_signal(lock);
  45. }
  46. + (ZYLService *)environmentWithType:(ZYLServiceEnvironment)environment {
  47. ZYLService *service = [ZYLService new];
  48. NSString *environmentString = [[NSUserDefaults standardUserDefaults] objectForKey:@"ZYLServiceEnvironment"];
  49. if ([BaseMethod isNOTNull:environmentString]) {
  50. if ([environmentString isEqualToString:@"ZYLServiceEnvironmentTest"]) {
  51. service.environment = ZYLServiceEnvironmentTest;
  52. }else if ([environmentString isEqualToString:@"ZYLServiceEnvironmentDevelop"]) {
  53. service.environment = ZYLServiceEnvironmentDevelop;
  54. }else if ([environmentString isEqualToString:@"ZYLServiceEnvironmentRelease"]) {
  55. service.environment = ZYLServiceEnvironmentRelease;
  56. }else if ([environmentString isEqualToString:@"ZYLServiceEnvironmentTestGy"]) {
  57. service.environment = ZYLServiceEnvironmentTestGy;
  58. }
  59. }else {
  60. service.environment = environment;
  61. }
  62. return service;
  63. }
  64. - (NSString *)baseUrl {
  65. switch (self.environment) {
  66. //case ZYLServiceEnvironmentTest: return @"http://api.timichat.net/";
  67. case ZYLServiceEnvironmentTest: return @"http:/47.98.212.223:9201/";
  68. case ZYLServiceEnvironmentDevelop: return @"http://api.rc.timichat.cn/";
  69. case ZYLServiceEnvironmentRelease: return @"https://api.timichat.cn/";
  70. case ZYLServiceEnvironmentTestGy: return @"http:/127.0.0.1:9201/";//http:/127.0.0.1:9201/、http:/192.168.3.13:9201/
  71. }
  72. }
  73. - (NSString *)mallBaseUrl {
  74. switch (self.environment) {
  75. //case ZYLServiceEnvironmentTest: return @"http://xkapi.laylib.com/";
  76. case ZYLServiceEnvironmentTest: return @"http:/47.98.212.223:9201/";
  77. case ZYLServiceEnvironmentDevelop: return @"http://xkapi.laylib.com/";
  78. case ZYLServiceEnvironmentRelease: return @"https://api.mfxike.com/";
  79. case ZYLServiceEnvironmentTestGy: return @"http:/127.0.0.1:9201/";//http:/127.0.0.1:9201/、http:/192.168.3.13:9201/
  80. }
  81. }
  82. @end