CloudServerViewController.mm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // CloudServerViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by XM on 2019/1/3.
  6. // Copyright © 2019年 XM. All rights reserved.
  7. //
  8. #import "CloudServerViewController.h"
  9. #import <JavaScriptCore/JavaScriptCore.h>
  10. #import "DeviceManager.h"
  11. #import "SystemInfoConfig.h"
  12. @protocol OCAndJSInteractionProtocol <JSExport>
  13. -(void)openCloudStorageList:(NSString *)param;
  14. @end
  15. @interface OCAndJSInteraction : NSObject <OCAndJSInteractionProtocol>
  16. @property (nonatomic,weak) id <OCAndJSInteractionProtocol>delegate;
  17. @end
  18. @implementation OCAndJSInteraction
  19. -(void)openCloudStorageList:(NSString *)param
  20. {
  21. if (self.delegate) {
  22. [self.delegate openCloudStorageList:param];
  23. }
  24. }
  25. @end
  26. @interface CloudServerViewController () <UIWebViewDelegate,OCAndJSInteractionProtocol,SystemInfoConfigDelegate>
  27. @property (nonatomic, strong) NSString *hardWare; //硬件版本
  28. @property (nonatomic, strong) NSString *softWare; //软件版本
  29. @property (nonatomic, strong) UIWebView *webView;
  30. @property (nonatomic, strong) NSURLRequest *webURLRequest;
  31. @property (nonatomic,strong) JSContext *context;
  32. @property (nonatomic,strong) UIWebView *wakeUpWeb;
  33. @property (nonatomic, strong) UIProgressView *progressView;
  34. @property (nonatomic,strong) NSTimer *countTimer;
  35. @property (nonatomic,assign) int curLoadNum; // 范围 1-10
  36. @property (nonatomic,assign) BOOL ifFinished; // 是否加载完毕
  37. @property (nonatomic,strong) SystemInfoConfig *config;
  38. @end
  39. @implementation CloudServerViewController
  40. - (void)viewWillAppear:(BOOL)animated{
  41. [super viewWillAppear:animated];
  42. [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated {
  45. [super viewWillDisappear:animated];
  46. [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
  47. }
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. self.automaticallyAdjustsScrollViewInsets = NO;
  51. self.navigationItem.title = TS("Cloud_storage");
  52. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,NavHeight, ScreenWidth, ScreenHeight-NavHeight)];
  53. self.webView.delegate = self;
  54. self.webView.scrollView.bounces = NO;
  55. [self.view addSubview:self.webView];
  56. self.progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
  57. self.progressView.frame = CGRectMake(0, 0, ScreenWidth, 10);
  58. self.progressView.progress = 0;
  59. self.progressView.progressTintColor = [UIColor blueColor];
  60. self.progressView.trackTintColor = [UIColor lightGrayColor];
  61. //设置进度条的高度,下面这句代码表示进度条的宽度变为原来的1倍,高度变为原来的1.5倍.
  62. self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
  63. [self.webView addSubview:self.progressView];
  64. // 先判断是否是主账号,然后根据结果继续操作 (这里回调后没有判断,没有联系人系统的话,这个也不用判断)
  65. [[DeviceManager getInstance] checkMasterAccount:nil];
  66. //判断当前硬件和软件信息,云服务需要用到这两个信息
  67. [self checkDeviceAbility];
  68. }
  69. #pragma mark 检查设备硬件软件信息
  70. - (void)checkDeviceAbility {
  71. ChannelObject *channel = [[DeviceControl getInstance] getSelectChannel];
  72. DeviceObject *device = [[DeviceControl getInstance] GetDeviceObjectBySN:channel.deviceMac];
  73. if (device.info.hardWare && device.info.hardWare.length >0 && device.info.softWareVersion && device.info.softWareVersion.length >0) {
  74. [self requestURL];
  75. //刷新加载进度
  76. self.countTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(countTimerAction) userInfo:nil repeats:YES];
  77. self.curLoadNum = 0;
  78. self.ifFinished = NO;
  79. }else{
  80. _config = [[SystemInfoConfig alloc] init];
  81. _config.delegate = self;
  82. [_config getSystemInfo];
  83. }
  84. }
  85. #pragma mark webView请求
  86. -(void)requestURL {
  87. char szUserID[128] = {0};
  88. FUN_GetFunStrAttr(EFUN_ATTR_LOGIN_USER_ID, szUserID, 128);
  89. NSString *URLString;
  90. NSArray *languages = [NSLocale preferredLanguages];
  91. NSString *currentLanguage = [languages objectAtIndex:0];
  92. ChannelObject *channel = [[DeviceControl getInstance] getSelectChannel];
  93. DeviceObject *device = [[DeviceControl getInstance] GetDeviceObjectBySN:channel.deviceMac];
  94. //支付云存储功能网页支持中英文两种语言
  95. if ([currentLanguage containsString:@"zh-Hans"] || [currentLanguage containsString:@"zh-Hant"]) {
  96. URLString = [NSString stringWithFormat:@"https://boss.xmcsrv.net/index.do?user_id=%s&uuid=%@&hardware=%@&software=%@&lan=zh-CN&appKey=%s",szUserID,device.deviceMac,device.info.hardWare,device.info.softWareVersion,APPKEY];
  97. }else{
  98. URLString = [NSString stringWithFormat:@"https://boss.xmcsrv.net/index.do?user_id=%s&uuid=%@&hardware=%@&software=%@&lan=en&appKey=%s",szUserID,device.deviceMac,device.info.hardWare,device.info.softWareVersion,APPKEY];
  99. }
  100. self.webURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
  101. [self.webView loadRequest:self.webURLRequest];
  102. }
  103. #pragma mark UIWebDelegate
  104. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  105. NSString *url = request.URL.absoluteString;
  106. if ([url hasPrefix:@"alipays://"] || [url hasPrefix:@"alipay://"]) {
  107. [[UIApplication sharedApplication] openURL:request.URL];
  108. [self requestURL];
  109. return NO;
  110. }
  111. else if ([url hasPrefix:@"weixin://wap/pay"]){
  112. [[UIApplication sharedApplication] openURL:request.URL];
  113. return NO;
  114. }
  115. NSDictionary *header = [request allHTTPHeaderFields];
  116. BOOL hasReferer = [header objectForKey:@"Referer"] != nil;
  117. if (hasReferer && [url hasPrefix:@"https://wx.tenpay.com"] && ![[header objectForKey:@"Referer"] isEqualToString:@"boss.xmcsrv.net://"]) {
  118. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. NSURL *urls = [request URL];
  121. NSString *originUrlStr = urls.absoluteString;
  122. NSArray *array = [originUrlStr componentsSeparatedByString:@"redirect_url"];
  123. NSString *nowUrlStr = [NSString stringWithFormat:@"%@redirect_url%@",[array objectAtIndex:0],@"boss.xmcsrv.net://"];
  124. NSURL *newUrl = [NSURL URLWithString:nowUrlStr];
  125. NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
  126. [req setHTTPMethod:@"GET"];
  127. [req setValue:@"boss.xmcsrv.net://" forHTTPHeaderField:@"Referer"];
  128. [self.wakeUpWeb loadRequest:req];
  129. });
  130. });
  131. [self requestURL];
  132. return NO;
  133. }
  134. return YES;
  135. }
  136. - (void)webViewDidStartLoad:(UIWebView *)webView {
  137. }
  138. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  139. self.ifFinished = YES;
  140. // 还可以为js端提供完整的原生方法供其调用(记得导入#import <JavaScriptCore/JavaScriptCore.h>)
  141. self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  142. OCAndJSInteraction *ocjsObj = [[OCAndJSInteraction alloc] init];
  143. ocjsObj.delegate = self;
  144. self.context[@"XmAppJsSDK"] = ocjsObj;
  145. }
  146. #pragma mark webView点击回调
  147. -(void)openCloudStorageList:(NSString *)param {
  148. dispatch_async(dispatch_get_main_queue(), ^{
  149. if ([param isEqualToString:@"0"]) {
  150. [self gotoCloudPhotoVC];
  151. }
  152. else if ([param isEqualToString:@"1"]) {
  153. [self gotoCloudVideoPlaybackVC];
  154. }
  155. });
  156. }
  157. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  158. }
  159. #pragma mark - 模拟加载动画
  160. -(void)countTimerAction{
  161. if (self.curLoadNum < 24 && self.ifFinished == NO) {
  162. self.curLoadNum++;
  163. [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
  164. self.progressView.progress = self.curLoadNum / 30.0;
  165. } completion:nil];
  166. }
  167. else{
  168. if (self.ifFinished) {
  169. [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
  170. self.progressView.progress = 1;
  171. } completion:^(BOOL complete){
  172. self.progressView.hidden = YES;
  173. [self.countTimer invalidate];
  174. self.countTimer = nil;
  175. }];
  176. }
  177. }
  178. }
  179. #pragma mark 判断主账号信息结果回调
  180. - (void)checkMaster:(NSString *)sId Result:(int)result {
  181. if (result >=1) { //主账号
  182. }else if (result == 0) { //不是主账号
  183. }else{//获取失败
  184. [MessageUI ShowErrorInt:result];
  185. }
  186. }
  187. #pragma mark 获取能力级回调信息
  188. - (void)SystemInfoConfigGetResult:(NSInteger)result {
  189. if (result <=0) {
  190. [MessageUI ShowErrorInt:(int)result];
  191. }else{
  192. [self checkDeviceAbility];
  193. }
  194. }
  195. #pragma mark 界面跳转
  196. - (void)gotoCloudPhotoVC {
  197. CloudPhotoViewController *cloudPhotoVC = [[CloudPhotoViewController alloc] init];
  198. [self.navigationController pushViewController:cloudPhotoVC animated:YES];
  199. }
  200. - (void)gotoCloudVideoPlaybackVC {
  201. CloudVideoViewController *cloudVideoVC = [[CloudVideoViewController alloc] init];
  202. [self.navigationController pushViewController:cloudVideoVC animated:YES];
  203. }
  204. @end