schtonn 1 year ago
parent
commit
9d45d1ebc3
2 changed files with 55 additions and 30 deletions
  1. 10 7
      sdsz74/ContentView.swift
  2. 45 23
      sdsz74/account.swift

+ 10 - 7
sdsz74/ContentView.swift

@@ -11,6 +11,8 @@ struct ContentView: View {
     @State private var examCount = 0
     @State private var messageCount = 0
     @State private var isLoggedIn = 0
+    @State private var userInfo: UserInfo = .init(name: "...", o: "")
+
     var body: some View {
         TabView {
             NavigationView {
@@ -36,10 +38,10 @@ struct ContentView: View {
             }
             NavigationView {
                 if isLoggedIn != 0 {
-                    accountView(isLoggedIn: $isLoggedIn)
+                    accountView(isLoggedIn: $isLoggedIn, userInfo: $userInfo)
                         .navigationBarTitle("账号")
                 } else {
-                    loginView(isLoggedIn: $isLoggedIn)
+                    loginView(isLoggedIn: $isLoggedIn, userInfo: $userInfo)
                         .navigationBarTitle("登录")
                 }
             }
@@ -61,8 +63,9 @@ extension View {
     }
 }
 
-struct ContentView_Previews: PreviewProvider {
-    static var previews: some View {
-        ContentView()
-    }
-}
+//
+// struct ContentView_Previews: PreviewProvider {
+//    static var previews: some View {
+//        ContentView()
+//    }
+// }

+ 45 - 23
sdsz74/account.swift

@@ -54,6 +54,11 @@ struct Login: Encodable {
     let cook: String
 }
 
+struct UserInfo: Encodable {
+    var name: String
+    var o: String
+}
+
 class LoginHandler {
     static let sharedInstance = LoginHandler()
 
@@ -88,7 +93,6 @@ func doLogin(user: String, password: String, action: @escaping (_: Int) -> Void)
         AF.request("https://debug.sdsz.icu/andlogin", method: .post, parameters: login, encoder: JSONParameterEncoder.default, headers: nil).responseString { res in
             print("\(res)")
             if res.value == "success" {
-                print("SUCCESS!\(LoginHandler().getCookie(name: "CASTGC"))")
                 action(1)
             } else {
                 action(0)
@@ -97,13 +101,19 @@ func doLogin(user: String, password: String, action: @escaping (_: Int) -> Void)
     }
 }
 
+func getUserInfo(userInfo: inout UserInfo) {
+    userInfo.name = "向量"
+}
+
 struct loginView: View {
     @State var username: String = ""
     @State var password: String = ""
     @State var btnText: String = "登录"
     @State var btnColor: Color = .blue
+    @State var inProgress: Bool = false
     @FocusState private var isFocused: Bool
     @Binding var isLoggedIn: Int
+    @Binding var userInfo: UserInfo
     var body: some View {
         VStack {
             Form {
@@ -124,27 +134,30 @@ struct loginView: View {
                 }.onTapGesture {
                     isFocused = false
                 }
-                Section {
-                    HStack {
-                        Spacer()
-                        Button(action: {
-                            doLogin(user: username, password: password) { (ret: Int) in
-                                isLoggedIn = ret
-                                if ret == 0 {
-                                    btnText = "登录失败"
-                                    btnColor = .red
-                                }
+                HStack {
+                    Spacer()
+                    Button(action: {
+                        inProgress = true
+                        doLogin(user: username, password: password) { (ret: Int) in
+                            isLoggedIn = ret
+                            inProgress = false
+                            if ret == 0 {
+                                btnText = "登录失败"
+                                btnColor = .red
+                            } else {
+                                getUserInfo(userInfo: &userInfo)
                             }
-                        }) {
-                            VStack {
-                                Text(btnText)
+                        }
+                    }) {
+                        HStack {
+                            Text(btnText)
+                            if inProgress {
+                                ProgressView()
                             }
-                        }.font(.title3).foregroundColor(btnColor)
-                        Spacer()
-                    }
+                        }
+                    }.foregroundColor(btnColor).buttonStyle(.bordered)
                 }
             }
-            Spacer()
         }
     }
 }
@@ -154,13 +167,21 @@ struct accountView: View {
     @State var password: String = ""
     @FocusState private var isFocused: Bool
     @Binding var isLoggedIn: Int
+    @Binding var userInfo: UserInfo
     var body: some View {
         VStack {
             Form {
-                Section(header: Text("hi")) {
-                    Text("一些")
-                    Text("一些")
-                    Text("东西")
+                Section(header: Text("基本信息")) {
+                    HStack {
+                        Text("姓名")
+                        Spacer()
+                        Text(userInfo.name)
+                    }
+                    HStack {
+                        Text("所属班级")
+                        Spacer()
+                        Text(userInfo.name)
+                    }
                 }
                 Section {
                     Button(action: {
@@ -183,7 +204,8 @@ struct accountView: View {
 
 struct accountView_Previews: PreviewProvider {
     @State static var isLoggedIn = 0
+    @State static var userInfo: UserInfo = .init(name: "", o: "")
     static var previews: some View {
-        loginView(isLoggedIn: $isLoggedIn)
+        loginView(isLoggedIn: $isLoggedIn, userInfo: $userInfo)
     }
 }