schtonn hai 1 ano
pai
achega
ca5d93bfba
Modificáronse 3 ficheiros con 81 adicións e 83 borrados
  1. 19 19
      sdsz74/ContentView.swift
  2. 59 61
      sdsz74/account.swift
  3. 3 3
      sdsz74/exam.swift

+ 19 - 19
sdsz74/ContentView.swift

@@ -12,39 +12,40 @@ struct ContentView: View {
     @State private var messageCount = 0
     @State private var isLoggedIn = 0
     var body: some View {
-        TabView{
+        TabView {
             NavigationView {
-                NavigationLink(destination: examListView(count:$examCount)) {
+                NavigationLink(destination: examListView(count: $examCount)) {
                     Text("共 \(examCount) 场考试")
                 }
                 .navigationBarTitle("74 桌")
             }
-                .tabItem({Image(systemName: "chart.bar.fill")
-                    Text("成绩分析")})
+            .tabItem { Image(systemName: "chart.bar.fill")
+                Text("成绩分析")
+            }
             NavigationView {
-                List{
-                    NavigationLink(destination: messageView(count:$messageCount)) {
+                List {
+                    NavigationLink(destination: messageView(count: $messageCount)) {
                         Text("消息")
                             .badge(messageCount)
                     }
                 }
                 .navigationBarTitle("数字校园")
             }
-                .tabItem({Image(systemName: "graduationcap")
-                    Text("数字校园")})
-            NavigationView{
-                if isLoggedIn != 0{
+            .tabItem { Image(systemName: "graduationcap")
+                Text("数字校园")
+            }
+            NavigationView {
+                if isLoggedIn != 0 {
                     accountView(isLoggedIn: $isLoggedIn)
-                    .navigationBarTitle("账号")
-                }
-                else{
-                    loginView(isLoggedIn:$isLoggedIn)
-                    .navigationBarTitle("登录")
+                        .navigationBarTitle("账号")
+                } else {
+                    loginView(isLoggedIn: $isLoggedIn)
+                        .navigationBarTitle("登录")
                 }
             }
-                .tabItem({Image(systemName: "person")
-                    Text("账号")})
-            
+            .tabItem { Image(systemName: "person")
+                Text("账号")
+            }
         }
     }
 }
@@ -60,7 +61,6 @@ extension View {
     }
 }
 
-
 struct ContentView_Previews: PreviewProvider {
     static var previews: some View {
         ContentView()

+ 59 - 61
sdsz74/account.swift

@@ -5,44 +5,42 @@
 //  Created by yunli on 2023/5/20.
 //
 
-import SwiftUI
 import Alamofire
+import SwiftUI
 
-struct Constant{
-    var cookiesDefaultsKey:String
+struct Constant {
+    var cookiesDefaultsKey: String
 }
 
-let Constants:Constant=(Constant(cookiesDefaultsKey: "JSESSIONID"))
+let Constants: Constant = .init(cookiesDefaultsKey: "JSESSIONID")
 
 class CookieHandler {
-    
-    static let shared: CookieHandler = CookieHandler()
-    
+    static let shared: CookieHandler = .init()
+
     let defaults = UserDefaults.standard
     let cookieStorage = HTTPCookieStorage.shared
-    
+
     func getCookie(forURL url: String) -> [HTTPCookie] {
         let computedUrl = URL(string: url)
         let cookies = cookieStorage.cookies(for: computedUrl!) ?? []
-        
+
         return cookies
     }
-    
-    func backupCookies(forURL url: String) -> Void {
-        var cookieDict = [String : AnyObject]()
-        
-        for cookie in self.getCookie(forURL: url) {
+
+    func backupCookies(forURL url: String) {
+        var cookieDict = [String: AnyObject]()
+
+        for cookie in getCookie(forURL: url) {
             cookieDict[cookie.name] = cookie.properties as AnyObject?
         }
-        
+
         defaults.set(cookieDict, forKey: Constants.cookiesDefaultsKey)
     }
-    
+
     func restoreCookies() {
         if let cookieDictionary = defaults.dictionary(forKey: Constants.cookiesDefaultsKey) {
-            
             for (_, cookieProperties) in cookieDictionary {
-                if let cookie = HTTPCookie(properties: cookieProperties as! [HTTPCookiePropertyKey : Any] ) {
+                if let cookie = HTTPCookie(properties: cookieProperties as! [HTTPCookiePropertyKey: Any]) {
                     cookieStorage.setCookie(cookie)
                 }
             }
@@ -58,87 +56,87 @@ struct Login: Encodable {
 
 class LoginHandler {
     static let sharedInstance = LoginHandler()
-    
-    func getCookie(name:String)->HTTPCookie?{
-        if let cookie=CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter({cookie->Bool in
-            return cookie.name==name
+
+    func getCookie(name: String) -> HTTPCookie? {
+        if let cookie = CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter({ cookie -> Bool in
+            cookie.name == name
         }).first {
             return cookie
         }
         return nil
     }
-    
-    func fetchLoginCookie(action:@escaping(_:String)->Void) {
-        if let cookie=getCookie(name:"JSESSIONID") {
+
+    func fetchLoginCookie(action: @escaping (_: String) -> Void) {
+        if let cookie = getCookie(name: "JSESSIONID") {
             HTTPCookieStorage.shared.deleteCookie(cookie)
             print(cookie)
         }
-        let url = "https://debug.sdsz.icu:81/sso/login";
+        let url = "https://debug.sdsz.icu:81/sso/login"
 
-        AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response{res in
-            if let cookie=self.getCookie(name:"JSESSIONID"){
+        AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response { _ in
+            if let cookie = self.getCookie(name: "JSESSIONID") {
                 action(cookie.value)
             }
         }
     }
 }
 
-func doLogin(user:String,password:String,action:@escaping(_:Int)->Void){
-    LoginHandler().fetchLoginCookie(){(cookie:String) in
+func doLogin(user: String, password: String, action: @escaping (_: Int) -> Void) {
+    LoginHandler().fetchLoginCookie { (cookie: String) in
         let login = Login(user: user, pwd: password, cook: cookie)
         print(login)
-        AF.request("https://debug.sdsz.icu/andlogin", method: .post, parameters: login,encoder:JSONParameterEncoder.default, headers: nil).responseString{res in
+        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"))")
+            if res.value == "success" {
+                print("SUCCESS!\(LoginHandler().getCookie(name: "CASTGC"))")
                 action(1)
-            }else{
+            } else {
                 action(0)
             }
         }
     }
 }
 
-struct loginView:View{
+struct loginView: View {
     @State var username: String = ""
     @State var password: String = ""
     @State var btnText: String = "登录"
     @State var btnColor: Color = .blue
-    @FocusState private var isFocused:Bool
+    @FocusState private var isFocused: Bool
     @Binding var isLoggedIn: Int
     var body: some View {
-        VStack{
+        VStack {
             Form {
-                Section{
+                Section {
                     HStack {
-                        Image(systemName:"person.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
+                        Image(systemName: "person.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
                         TextField(text: $username, prompt: Text("数字校园号")) {
                             Text("数字校园号")
                         }.focused($isFocused)
                             .keyboardType(.numberPad)
                     }
                     HStack {
-                        Image(systemName:"lock.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
+                        Image(systemName: "lock.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
                         SecureField(text: $password, prompt: Text("密码")) {
                             Text("密码")
                         }.focused($isFocused)
                     }
-                }.onTapGesture  {
-                    isFocused=false
+                }.onTapGesture {
+                    isFocused = false
                 }
-                Section{
-                    HStack{
+                Section {
+                    HStack {
                         Spacer()
-                        Button(action:{
-                                doLogin(user:username,password: password){(ret:Int) in
-                                isLoggedIn=ret
-                                if ret==0{
+                        Button(action: {
+                            doLogin(user: username, password: password) { (ret: Int) in
+                                isLoggedIn = ret
+                                if ret == 0 {
                                     btnText = "登录失败"
                                     btnColor = .red
                                 }
                             }
-                        }){
-                            VStack{
+                        }) {
+                            VStack {
                                 Text(btnText)
                             }
                         }.font(.title3).foregroundColor(btnColor)
@@ -151,28 +149,28 @@ struct loginView:View{
     }
 }
 
-struct accountView:View{
+struct accountView: View {
     @State var username: String = ""
     @State var password: String = ""
-    @FocusState private var isFocused:Bool
+    @FocusState private var isFocused: Bool
     @Binding var isLoggedIn: Int
     var body: some View {
-        VStack{
+        VStack {
             Form {
-                Section(header:Text("hi")){
+                Section(header: Text("hi")) {
                     Text("一些")
                     Text("一些")
                     Text("东西")
                 }
-                Section{
-                    Button(action:{
-                        isLoggedIn=0
-                        if let cookie=LoginHandler().getCookie(name:"CASTGC") {
+                Section {
+                    Button(action: {
+                        isLoggedIn = 0
+                        if let cookie = LoginHandler().getCookie(name: "CASTGC") {
                             HTTPCookieStorage.shared.deleteCookie(cookie)
                             print(cookie)
                         }
-                    }){
-                        VStack{
+                    }) {
+                        VStack {
                             Text("退出").foregroundColor(.red)
                         }
                     }
@@ -186,6 +184,6 @@ struct accountView:View{
 struct accountView_Previews: PreviewProvider {
     @State static var isLoggedIn = 0
     static var previews: some View {
-        loginView(isLoggedIn:$isLoggedIn)
+        loginView(isLoggedIn: $isLoggedIn)
     }
 }

+ 3 - 3
sdsz74/exam.swift

@@ -10,11 +10,11 @@ import SwiftUI
 struct examListView: View {
     @Binding var count: Int
     var body: some View {
-        List{
-            ForEach(0..<count,id:\.self) { item in
+        List {
+            ForEach(0 ..< count, id: \.self) { item in
                 Text("\(item)")
             }
-            Button(action:{count+=1}) {
+            Button(action: { count += 1 }) {
                 Label("添加考试", systemImage: "plus")
             }
         }