Просмотр исходного кода

1.完成心跳包功能,在RootViewController新增方法DealWithUILogout用于本地登出时更新视图。

Pen Li 9 лет назад
Родитель
Сommit
6eb9978ed9

BIN
Ants Contract/AntsContract.xcworkspace/xcuserdata/macmini1.xcuserdatad/UserInterfaceState.xcuserstate


+ 8 - 8
Ants Contract/AntsContract/AntsContract.xcodeproj/project.pbxproj

@@ -61,8 +61,8 @@
 		713E83B61E037C2000C4F12A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
 		713E83B81E037C2000C4F12A /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
 		713E83B91E037C2000C4F12A /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
-		713E83BB1E037C2000C4F12A /* RootViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = "<group>"; };
-		713E83BC1E037C2000C4F12A /* RootViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
+		713E83BB1E037C2000C4F12A /* RootViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RootViewController.h; path = ../../common/RootViewController.h; sourceTree = "<group>"; };
+		713E83BC1E037C2000C4F12A /* RootViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RootViewController.m; path = ../../common/RootViewController.m; sourceTree = "<group>"; };
 		713E83BF1E037C2000C4F12A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
 		713E83C11E037C2000C4F12A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
 		713E83C41E037C2000C4F12A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@@ -72,8 +72,8 @@
 		7156712F1E0D0AAE006324A0 /* RectDrawable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RectDrawable.m; path = "../../../RedAnt ERP Mobile/common/pdfCreator/RectDrawable.m"; sourceTree = "<group>"; };
 		715671341E10A43D006324A0 /* NetworkUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NetworkUtils.h; path = "../../../RedAnt ERP Mobile/common/NetworkUtils.h"; sourceTree = "<group>"; };
 		715671351E10A43D006324A0 /* NetworkUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NetworkUtils.m; path = "../../../RedAnt ERP Mobile/common/NetworkUtils.m"; sourceTree = "<group>"; };
-		715671371E10A48E006324A0 /* ACNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ACNetwork.h; sourceTree = "<group>"; };
-		715671381E10A48E006324A0 /* ACNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ACNetwork.m; sourceTree = "<group>"; };
+		715671371E10A48E006324A0 /* ACNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ACNetwork.h; path = ../../common/ACNetwork.h; sourceTree = "<group>"; };
+		715671381E10A48E006324A0 /* ACNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ACNetwork.m; path = ../../common/ACNetwork.m; sourceTree = "<group>"; };
 		7156713A1E10AF67006324A0 /* RAUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RAUtils.h; path = "../../../RedAnt ERP Mobile/common/RAUtils.h"; sourceTree = "<group>"; };
 		7156713B1E10AF67006324A0 /* RAUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RAUtils.m; path = "../../../RedAnt ERP Mobile/common/RAUtils.m"; sourceTree = "<group>"; };
 		7156713E1E10B028006324A0 /* ZipArchive.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ZipArchive.h; path = "../../../RedAnt ERP Mobile/common/zip/ZipArchive.h"; sourceTree = "<group>"; };
@@ -90,8 +90,8 @@
 		7156714F1E10B1D8006324A0 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
 		715671541E10BB49006324A0 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Reachability.h; path = "../../../RedAnt ERP Mobile/common/Reachability.h"; sourceTree = "<group>"; };
 		715671551E10BB49006324A0 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Reachability.m; path = "../../../RedAnt ERP Mobile/common/Reachability.m"; sourceTree = "<group>"; };
-		715671571E10BDF6006324A0 /* LoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginViewController.h; sourceTree = "<group>"; };
-		715671581E10BDF6006324A0 /* LoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoginViewController.m; sourceTree = "<group>"; };
+		715671571E10BDF6006324A0 /* LoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LoginViewController.h; path = ../../common/LoginViewController.h; sourceTree = "<group>"; };
+		715671581E10BDF6006324A0 /* LoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = LoginViewController.m; path = ../../common/LoginViewController.m; sourceTree = "<group>"; };
 		7156715B1E10BE28006324A0 /* AESCrypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AESCrypt.h; path = "../../../RedAnt ERP Mobile/common/AES/AESCrypt.h"; sourceTree = "<group>"; };
 		7156715C1E10BE28006324A0 /* AESCrypt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AESCrypt.m; path = "../../../RedAnt ERP Mobile/common/AES/AESCrypt.m"; sourceTree = "<group>"; };
 		7156715D1E10BE28006324A0 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = LICENSE; path = "../../../RedAnt ERP Mobile/common/AES/LICENSE"; sourceTree = "<group>"; };
@@ -104,8 +104,8 @@
 		71D01ACA1E07AE34006620CE /* SUBCONTRACTORS REFERRAL LIST.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "SUBCONTRACTORS REFERRAL LIST.json"; sourceTree = "<group>"; };
 		71D01ACC1E07AF10006620CE /* SUBCONTRACTORS REFERRAL LIST.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "SUBCONTRACTORS REFERRAL LIST.pdf"; sourceTree = "<group>"; };
 		71D01ACE1E07B71B006620CE /* config.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = "<group>"; };
-		71D01AD21E07D699006620CE /* SignatureTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SignatureTableViewCell.h; sourceTree = "<group>"; };
-		71D01AD31E07D699006620CE /* SignatureTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SignatureTableViewCell.m; sourceTree = "<group>"; };
+		71D01AD21E07D699006620CE /* SignatureTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SignatureTableViewCell.h; path = ../../common/SignatureTableViewCell.h; sourceTree = "<group>"; };
+		71D01AD31E07D699006620CE /* SignatureTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SignatureTableViewCell.m; path = ../../common/SignatureTableViewCell.m; sourceTree = "<group>"; };
 		71D01AD61E08CA53006620CE /* SignatureView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SignatureView.h; path = "../../../RedAnt ERP Mobile/common/Functions/signature/SignatureView.h"; sourceTree = "<group>"; };
 		71D01AD71E08CA53006620CE /* SignatureView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SignatureView.m; path = "../../../RedAnt ERP Mobile/common/Functions/signature/SignatureView.m"; sourceTree = "<group>"; };
 		71D01AD81E08CA53006620CE /* SignatureViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SignatureViewController.h; path = "../../../RedAnt ERP Mobile/common/Functions/signature/SignatureViewController.h"; sourceTree = "<group>"; };

+ 9 - 9
Ants Contract/Granite Expo eSign/Granite Expo eSign.xcodeproj/xcuserdata/macmini1.xcuserdatad/xcschemes/eSign.xcscheme

@@ -15,8 +15,8 @@
             <BuildableReference
                BuildableIdentifier = "primary"
                BlueprintIdentifier = "7128C23A1E124F4500FF635B"
-               BuildableName = "eSign.app"
-               BlueprintName = "eSign"
+               BuildableName = "Granite Expo eSign.app"
+               BlueprintName = "Granite Expo eSign"
                ReferencedContainer = "container:Granite Expo eSign.xcodeproj">
             </BuildableReference>
          </BuildActionEntry>
@@ -33,8 +33,8 @@
          <BuildableReference
             BuildableIdentifier = "primary"
             BlueprintIdentifier = "7128C23A1E124F4500FF635B"
-            BuildableName = "eSign.app"
-            BlueprintName = "eSign"
+            BuildableName = "Granite Expo eSign.app"
+            BlueprintName = "Granite Expo eSign"
             ReferencedContainer = "container:Granite Expo eSign.xcodeproj">
          </BuildableReference>
       </MacroExpansion>
@@ -42,7 +42,7 @@
       </AdditionalOptions>
    </TestAction>
    <LaunchAction
-      buildConfiguration = "Release"
+      buildConfiguration = "Debug"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       launchStyle = "0"
@@ -56,8 +56,8 @@
          <BuildableReference
             BuildableIdentifier = "primary"
             BlueprintIdentifier = "7128C23A1E124F4500FF635B"
-            BuildableName = "eSign.app"
-            BlueprintName = "eSign"
+            BuildableName = "Granite Expo eSign.app"
+            BlueprintName = "Granite Expo eSign"
             ReferencedContainer = "container:Granite Expo eSign.xcodeproj">
          </BuildableReference>
       </BuildableProductRunnable>
@@ -82,8 +82,8 @@
          <BuildableReference
             BuildableIdentifier = "primary"
             BlueprintIdentifier = "7128C23A1E124F4500FF635B"
-            BuildableName = "eSign.app"
-            BlueprintName = "eSign"
+            BuildableName = "Granite Expo eSign.app"
+            BlueprintName = "Granite Expo eSign"
             ReferencedContainer = "container:Granite Expo eSign.xcodeproj">
          </BuildableReference>
       </BuildableProductRunnable>

+ 2 - 0
Ants Contract/Granite Expo eSign/Granite Expo eSign/config.h

@@ -45,6 +45,7 @@
     #define  URL_NEW_DOCUMENT  @"http://192.168.0.155/phpcms/admin.php"
     #define  URL_UPDATE_NOTES  @"http://192.168.0.155/phpcms/admin.php"
     #define  URL_REQUEST_DOC  @"http://192.168.0.155/phpcms/admin.php"
+    #define URL_HEART_BEAT @"http://192.168.0.155/phpcms/admin.php"
 #else
     #define  URL_ERR_LOG  @""
     #define  URL_UPDATE_AUTH  @"https://www.thegraniteexpo.com/admin.php"
@@ -53,6 +54,7 @@
     #define  URL_REQUEST_DOC  @"https://www.thegraniteexpo.com/admin.php"
     #define  URL_NEW_DOCUMENT  @"https://www.thegraniteexpo.com/admin.php"
     #define  URL_UPDATE_NOTES  @"https://www.thegraniteexpo.com/admin.php"
+    #define URL_HEART_BEAT @"https://www.thegraniteexpo.com/admin.php"
 #endif
 
 #endif /* config_h */

+ 3 - 0
Ants Contract/common/ACNetwork.h

@@ -27,4 +27,7 @@
 
 
 +(NSString *) offline_docnumber;
+
++ (int)heartBeat;
+
 @end

+ 26 - 1
Ants Contract/common/ACNetwork.m

@@ -14,7 +14,7 @@
 
 #import "config.h"
 #import "AESCrypt.h"
-
+#import <AdSupport/ASIdentifierManager.h>
 
 
 
@@ -754,4 +754,29 @@
     
     return [NSString stringWithFormat:@"%@%6ld",appDelegate.offPrefix,docint ];
 }
+
++ (int)heartBeat {
+    
+    NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
+    
+    [params setObject:@"getStatus" forKey:@"_operate"];
+    
+    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
+    if(appDelegate.user!=nil)
+        [params setValue:appDelegate.user forKey:@"user"];
+    if(appDelegate.password!=nil)
+        [params setValue:appDelegate.password forKey:@"password"];
+    NSString *DeviceToken = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
+    [params setValue:DeviceToken forKey:@"deviceid"];
+    
+    
+    NSData* json=[self get_json:URL_HEART_BEAT parameters:params file:nil];
+    if(json==nil)
+        return -1;
+    NSError *error=nil;
+    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
+
+    return [[resultDic objectForKey:@"result"] intValue];
+}
+
 @end

+ 137 - 1
Ants Contract/common/AppDelegate.m

@@ -19,6 +19,8 @@
 //#import "AFHTTPSessionManager.h"
 @interface AppDelegate ()
 
+@property (nonatomic,strong) NSTimer *heartBeat;
+
 @end
 
 @implementation AppDelegate
@@ -231,6 +233,8 @@
 - (void)applicationDidEnterBackground:(UIApplication *)application {
     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
+    
+    [self stopHeartBeat];
 }
 
 
@@ -241,6 +245,11 @@
 
 - (void)applicationDidBecomeActive:(UIApplication *)application {
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
+    
+    if (self.bLogin) {
+         [self startHeartBeat];
+    }
+    
 }
 
 
@@ -254,6 +263,13 @@
     self.user = user;
     self.password = pwd;
     self.bLogin = true;
+    
+    // 延迟1s启动心跳,以避免在登录框还在Dismiss动画过程中再次调用Dismiss
+    __weak typeof(self) weakself = self;
+    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
+    dispatch_after(time, dispatch_get_main_queue(), ^{
+        [weakself heartBeatRightNow:YES];
+    });
 }
 -(void) Logout
 {
@@ -269,7 +285,7 @@
     self.password=nil;
     self.bLogin = false;
     
-    
+    [self stopHeartBeat];
 //    self.user_icon=nil;
 //    self.user_type = USER_ROLE_UNKNOWN;
 //    self.bLogin = false;
@@ -282,4 +298,124 @@
 //    [self SetMode:nil];
     
 }
+
+#pragma mark - HeartBeat
+
+- (void)markHeartBeatTime {
+    
+    NSDate *date = [NSDate date];
+    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
+    [userDefaults setValue:date forKey:@"heartBeatTime"];
+    [userDefaults synchronize];
+}
+
+- (BOOL)checkHearBeatTimeOut {
+    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
+    // 前一次成功心跳的时间
+    NSDate *preTime = [userDefaults valueForKey:@"heartBeatTime"];
+    
+    if (preTime) {
+        NSDate *curTime = [NSDate date];
+        
+        NSTimeInterval timeInterval = [curTime timeIntervalSinceDate:preTime];
+        NSTimeInterval timeOutInterval = 1 * 60 * 60;
+        if (timeInterval > timeOutInterval) {
+            return YES;
+        }
+    }
+    
+    return NO;
+}
+
+- (void)heartBeatRightNow:(BOOL)rightNow {
+    if (rightNow) {
+        [self heartBeatAction:nil];
+    }
+    
+    NSTimeInterval heartBeatTimeInterval = 3 * 60;
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+        self.heartBeat = [NSTimer scheduledTimerWithTimeInterval:heartBeatTimeInterval target:self selector:@selector(heartBeatAction:) userInfo:nil repeats:YES];
+        [[NSRunLoop currentRunLoop] addTimer:self.heartBeat forMode:NSDefaultRunLoopMode];
+        [[NSRunLoop currentRunLoop] run];
+    });
+
+}
+
+- (void)startHeartBeat {
+    
+
+        [self heartBeatRightNow:YES];
+
+    
+}
+
+- (void)stopHeartBeat {
+    if (self.heartBeat) {
+        [self.heartBeat invalidate];
+        self.heartBeat = nil;
+    }
+}
+
+- (void)heartBeatTimeout {
+    __weak typeof(self) weakself = self;
+    dispatch_async(dispatch_get_main_queue(), ^{
+        
+        UIApplication *application = [UIApplication sharedApplication];
+        UINavigationController *rootVC = (UINavigationController *)application.keyWindow.rootViewController;
+        
+        // 超时登出
+        [weakself Logout];
+        // 提示登录
+        UIViewController* presentVC=rootVC.presentedViewController;
+        if (presentVC) { //如果当前已有模态对话框,则关闭当前的模态框
+            [presentVC dismissViewControllerAnimated:NO completion:^{
+                // 退回到初始视图
+                [rootVC popToRootViewControllerAnimated:NO];
+                RootViewController* topvc= (RootViewController*)rootVC.topViewController;
+                [topvc dealWithUILogout];
+                [topvc presentLogin:topvc.ibSignin];
+            }];
+        } else {
+            // 退回到初始视图
+            [rootVC popToRootViewControllerAnimated:NO];
+            RootViewController* topvc= (RootViewController*)rootVC.topViewController;
+            [topvc dealWithUILogout];
+            [topvc presentLogin:topvc.ibSignin];
+        }
+        
+        
+    });
+}
+
+- (void)heartBeatAction:(NSTimer *)timer {
+    
+    __weak typeof(self) weakself = self;
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+       
+        int result = [ACNetwork heartBeat];
+//        int result = 2;
+        
+        if (result == 2) {
+            // 心跳成功才处理计时
+            [self markHeartBeatTime];
+            
+        } else if (result == 4) { // 超时
+            
+            [weakself heartBeatTimeout];
+            
+            
+        } else if ([weakself checkHearBeatTimeOut]) {// 本地检查超时(上一次成功心跳距现在的间隔时间超出1h)
+            
+            [weakself heartBeatTimeout];
+        }
+        
+        DebugLog(@"heart beat result %d",result);
+        
+    });
+}
+
+- (void)dealloc {
+    [self stopHeartBeat];
+}
+
 @end

+ 2 - 0
Ants Contract/common/RootViewController.h

@@ -23,5 +23,7 @@
 @property (weak, nonatomic) IBOutlet UIImageView *imageBG;
 @property (weak, nonatomic) IBOutlet UIImageView *coverBG;
 
+- (void)dealWithUILogout;
+
 @end
 

+ 10 - 0
Ants Contract/common/RootViewController.m

@@ -375,6 +375,16 @@
     
     
     
+}
+
+- (void)dealWithUILogout {
+    self.welcomeLabel.text=@"Not sign in";
+    //                        [sender setTitle:@"Sign in" forState:UIControlStateNormal];
+    [self.ibSignin setTitle:@"Sign in"];
+    
+    self.buttonHIC.enabled = false;
+    self.buttonSRL.enabled = false;
+    self.coverView.hidden = false;
 }
 
 - (IBAction)OnSigninClick:(id)sender {