Selaa lähdekoodia

1.实现Login的登录逻辑。

Pen Li 9 vuotta sitten
vanhempi
commit
4dc63c8d26

+ 3 - 0
RA Image/BasicViewController.h

@@ -9,6 +9,9 @@
 #import <UIKit/UIKit.h>
 #import "const.h"
 #import "config.h"
+#import "RAINetwork.h"
+#import "AppDelegate.h"
+#import "RAUtils.h"
 
 @interface BasicViewController : UIViewController
 

BIN
RA Image/RA Image.xcodeproj/project.xcworkspace/xcuserdata/macmini1.xcuserdatad/UserInterfaceState.xcuserstate


+ 12 - 0
RA Image/RA Image.xcodeproj/xcuserdata/macmini1.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -2,4 +2,16 @@
 <Bucket
    type = "1"
    version = "2.0">
+   <Breakpoints>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "Yes"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            scope = "0"
+            stopOnStyle = "0">
+         </BreakpointContent>
+      </BreakpointProxy>
+   </Breakpoints>
 </Bucket>

+ 6 - 0
RA Image/RA Image/AppDelegate.h

@@ -16,5 +16,11 @@
 @property (strong,nonatomic) NSString * password;
 @property (strong,nonatomic) NSString* build;
 @property bool bLogin;
+@property (nonatomic,copy) NSString *address;
+
+- (void)showNormalRootVC;
+
+- (void)showLoginVC;
+
 @end
 

+ 18 - 0
RA Image/RA Image/AppDelegate.m

@@ -7,13 +7,27 @@
 //
 
 #import "AppDelegate.h"
+#import "LoginViewController.h"
 
 @interface AppDelegate ()
 
+@property (nonatomic,strong) UIViewController *rootVC;
+
 @end
 
 @implementation AppDelegate
 
+- (void)showNormalRootVC {
+    self.window.rootViewController = self.rootVC;
+}
+
+- (void)showLoginVC {
+    if (![self.window.rootViewController isKindOfClass:[LoginViewController class]]) {
+        LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
+        
+        [self.window setRootViewController:loginVC];
+    }
+}
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     // Override point for customization after application launch.
@@ -21,6 +35,7 @@
     
     NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
     self.build =[infoDict objectForKey:@"CFBundleVersion"];
+    self.rootVC = self.window.rootViewController;
     return YES;
 }
 
@@ -44,6 +59,9 @@
 
 - (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 showLoginVC];
+    }
 }
 
 

+ 4 - 2
RA Image/RA Image/Base.lproj/Main.storyboard

@@ -77,7 +77,7 @@
                                         <rect key="frame" x="0.0" y="28" width="375" height="40"/>
                                         <autoresizingMask key="autoresizingMask"/>
                                         <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="AzG-Bz-3Gu" id="DIU-Qe-sGe">
-                                            <rect key="frame" x="0.0" y="0.0" width="375" height="39"/>
+                                            <rect key="frame" x="0.0" y="0.0" width="375" height="40"/>
                                             <autoresizingMask key="autoresizingMask"/>
                                             <subviews>
                                                 <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="fill" placeholder="place order" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="YXP-if-YQ2">
@@ -102,7 +102,7 @@
                                         <rect key="frame" x="0.0" y="68" width="375" height="40"/>
                                         <autoresizingMask key="autoresizingMask"/>
                                         <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="LYr-Om-Dpu" id="DQB-yC-XbO">
-                                            <rect key="frame" x="0.0" y="0.0" width="375" height="39"/>
+                                            <rect key="frame" x="0.0" y="0.0" width="375" height="40"/>
                                             <autoresizingMask key="autoresizingMask"/>
                                             <subviews>
                                                 <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="记住我的登录状态" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l0F-dN-mYq">
@@ -209,6 +209,7 @@
                                 <state key="normal" title="O">
                                     <color key="titleColor" red="0.25435081980000002" green="0.75957768849999996" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                 </state>
+                                <state key="selected" title="Y"/>
                                 <connections>
                                     <action selector="internalCheckBtnClick:" destination="r7u-Ls-Lza" eventType="touchUpInside" id="GIU-oB-sNH"/>
                                 </connections>
@@ -243,6 +244,7 @@
                                 <state key="normal" title="O">
                                     <color key="titleColor" red="0.25435081980000002" green="0.75957768849999996" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                 </state>
+                                <state key="selected" title="Y"/>
                                 <connections>
                                     <action selector="externalCheckBtnClick:" destination="r7u-Ls-Lza" eventType="touchUpInside" id="BhV-eZ-ONE"/>
                                 </connections>

+ 2 - 0
RA Image/RA Image/LoginSettingViewController.h

@@ -10,4 +10,6 @@
 
 @interface LoginSettingViewController : BasicViewController
 
+@property (nonatomic,copy) void(^returnValue)(NSString *);
+
 @end

+ 36 - 3
RA Image/RA Image/LoginSettingViewController.m

@@ -8,6 +8,8 @@
 
 #import "LoginSettingViewController.h"
 
+static NSString *kScanAddress = @"ScanAddress";
+
 @interface LoginSettingViewController ()
 
 @property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@@ -15,6 +17,7 @@
 @property (strong, nonatomic) IBOutlet UITextField *internalTextField;
 @property (strong, nonatomic) IBOutlet UIButton *externalCheckBtn;
 @property (strong, nonatomic) IBOutlet UITextField *externalTextField;
+@property (nonatomic,copy) NSString *checkedAddress;
 
 @end
 
@@ -28,6 +31,8 @@
     
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneItemClick:)];
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Scan" style:UIBarButtonItemStylePlain target:self action:@selector(scanItemClick:)];
+    
+    [self loadAddress];
 }
 
 - (void)didReceiveMemoryWarning {
@@ -35,23 +40,51 @@
     // Dispose of any resources that can be recreated.
 }
 
+- (void)loadAddress {
+    NSDictionary *addressDic = [self userDefaultsValue:kScanAddress];
+    if (addressDic) {
+        self.internalTextField.text = [addressDic objectForKey:@"internalAddress"];
+        self.externalTextField.text = [addressDic objectForKey:@"externalAddress"];
+    }
+}
+
 
 #pragma mark - Button Click
 
 - (void)doneItemClick:(UIBarButtonItem *)sender {
+    if (self.externalCheckBtn.selected) self.checkedAddress = self.externalTextField.text;
+    if (self.internalCheckBtn.selected) self.checkedAddress = self.internalTextField.text;
+    if (self.checkedAddress && self.returnValue) {
+        self.returnValue(self.checkedAddress);
+    }
     [self.navigationController dismissViewControllerAnimated:YES completion:nil];
 }
 
 - (void)scanItemClick:(UIBarButtonItem *)sender {
-    
+    // 扫描成功保存扫描值
+    if (true) {
+        NSString *internalAddr = @"http://192.168.0.155/wm_postgresql/handset_login.php";
+        NSString *externalAddr = @"127.0.0.1";
+        NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
+        [addressDic setObject:internalAddr forKey:@"internalAddress"];
+        [addressDic setObject:externalAddr forKey:@"externalAddress"];
+        
+        self.internalTextField.text = internalAddr;
+        self.externalTextField.text = externalAddr;
+        
+        [self setUserDefaultsValue:addressDic forKey:kScanAddress];
+        
+    }
 }
 
 - (IBAction)internalCheckBtnClick:(UIButton *)sender {
-    
+    sender.selected = !sender.selected;
+    if (sender.selected) self.externalCheckBtn.selected = NO;
 }
 
 - (IBAction)externalCheckBtnClick:(UIButton *)sender {
-    
+    sender.selected = !sender.selected;
+    if (sender.selected) self.internalCheckBtn.selected = NO;
 }
 
 @end

+ 46 - 2
RA Image/RA Image/LoginViewController.m

@@ -32,6 +32,7 @@ static NSString *kLoginUserInfo = @"UserInfo";
     [super viewDidLoad];
     // Do any additional setup after loading the view.
     
+    [self initUserData];
     [self loadData];
     [self.loginTable reloadData];
 }
@@ -116,6 +117,7 @@ static NSString *kLoginUserInfo = @"UserInfo";
         } else if ([placeOrder isEqualToString:@"password"]) {
             cell.textBox.text = self.pwd;
         }
+        cell.textBox.secureTextEntry = [[itemDic objectForKey:@"security"] boolValue];
         return cell;
     
     } else if ([type isEqualToString:@"switch"]) {
@@ -145,14 +147,56 @@ static NSString *kLoginUserInfo = @"UserInfo";
 
 - (IBAction)loginBtnClick:(UIButton *)sender {
     [self prepareUserData];
-    if ([self userDefaultsValue:kRememberLogin]) {
-        [self setUserDefaultsValue:[NSString stringWithFormat:@"%@&%@&%@",self.address,self.user,self.pwd] forKey:kLoginUserInfo];
+    if (!self.address.length) {
+        [RAUtils message_alert:@"Please choose an address in setting" title:@"Warning" controller:self];
+        return;
     }
+    
+    if (!self.user.length) {
+        [RAUtils message_alert:@"User name cann't be blank" title:@"Warning" controller:self];
+        return;
+    }
+    
+    if (!self.pwd.length) {
+        [RAUtils message_alert:@"Password cann't be blank" title:@"Warning" controller:self];
+        return;
+    }
+    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+    appDelegate.address = self.address;
+    __weak typeof(self) weakself = self;
+    UIAlertView *alert = [RAUtils waiting_alert:@"Please wait" title:@"Login"];
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+        if (weakself) {
+            __strong typeof(weakself) strongself = weakself;
+            NSDictionary *loginDic = [RAINetwork Login:strongself.user password:strongself.pwd];
+            dispatch_async(dispatch_get_main_queue(), ^{
+                [alert dismissWithClickedButtonIndex:0 animated:YES];
+                int result = [[loginDic objectForKey:@"result"] intValue];
+                if (result == 1 && [strongself userDefaultsValue:kRememberLogin]) {
+                    [strongself setUserDefaultsValue:[NSString stringWithFormat:@"%@&%@&%@",strongself.address,strongself.user,strongself.pwd] forKey:kLoginUserInfo];
+                    appDelegate.bLogin = YES;
+                    [appDelegate showNormalRootVC];
+                } else {
+                    NSString *msg = [loginDic objectForKey:@"err_msg"];
+                    [RAUtils message_alert:msg title:@"Warning" controller:strongself];
+                }
+            });
+
+        }
+    });
 }
 
 - (IBAction)settingBtnClick:(UIButton *)sender {
     
     LoginSettingViewController *loginSettingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginSettingViewController"];
+    __weak typeof(self) weakself = self;
+    loginSettingVC.returnValue = ^(NSString *address) {
+        if (weakself) {
+            __strong typeof(weakself) strongself = weakself;
+            strongself.address = address;
+            [strongself.loginTable reloadData];
+        }
+    };
     UINavigationController *settingRootNav = [[UINavigationController alloc] initWithRootViewController:loginSettingVC];
     [self presentViewController:settingRootNav animated:YES completion:nil];
     

+ 21 - 0
RA Image/RA Image/RAINetwork.m

@@ -104,4 +104,25 @@
      */
     return nil;
 }
+
++ (NSDictionary *)Login:(NSString *)user password:(NSString *)password {
+    NSMutableDictionary *params = @{@"user":user,
+                                    @"password":password
+                                    }.mutableCopy;
+    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+    
+    NSData *data = [self get_json:appDelegate.address parameters:params file:nil];
+    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
+    return resultDic;
+}
+
++ (NSDictionary *)logout {
+    NSMutableDictionary *params = @{
+                                    }.mutableCopy;
+    
+    NSData *data = [self get_json:nil parameters:params file:nil];
+    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
+    return resultDic;
+}
+
 @end

+ 2 - 2
RA Image/RA Image/RootViewController.m

@@ -20,14 +20,14 @@
     // Do any additional setup after loading the view, typically from a nib.
     
     self.view.backgroundColor = [UIColor redColor];
+    
+
 }
 
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     
-    LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
     
-    [self.navigationController presentViewController:loginVC animated:YES completion:nil];
 }
 
 

+ 8 - 3
RA Image/RA Image/login.json

@@ -4,17 +4,22 @@
   "item_0" : {
     "edit" : false,
     "type" : "text",
-    "place_order" : "Address"
+    "place_order" : "Address",
+    "security" : false
   },
   "item_1" : {
     "edit" : true,
     "type" : "text",
-    "place_order" : "user name"
+      "place_order" : "user name",
+      "security" : false
+
   },
   "item_2" : {
     "edit" : true,
     "type" : "text",
-    "place_order" : "password"
+      "place_order" : "password",
+      "security" : true
+
   },
   "item_3" : {
     "type" : "switch",