schtonn 1 year ago
parent
commit
c5cfdc7214
2 changed files with 177 additions and 64 deletions
  1. 0 64
      sdsz74/ContentView.swift
  2. 177 0
      sdsz74/account.swift

+ 0 - 64
sdsz74/ContentView.swift

@@ -60,70 +60,6 @@ extension View {
     }
 }
 
-func doLogin(status:inout Int)->Void{
-    status=1
-}
-
-struct loginView:View{
-    @State var username: String = ""
-    @State var password: String = ""
-    @FocusState private var isFocused:Bool
-    @Binding var isLoggedIn: Int
-    var body: some View {
-        VStack{
-            Form {
-                Section{
-                    TextField(text: $username, prompt: Text("数字校园号")) {
-                        Text("Username")
-                    }.focused($isFocused)
-                    SecureField(text: $password, prompt: Text("密码")) {
-                        Text("Password")
-                    }.focused($isFocused)
-                }.onTapGesture  {
-                    isFocused=false
-                }
-                Section{
-                    HStack{
-                        Spacer()
-                        Button(action:{doLogin(status: &isLoggedIn)}){
-                            VStack{
-                                Text("登录")
-                            }
-                        }.font(.title3)
-                        Spacer()
-                    }
-                }
-            }
-            Spacer()
-        }
-    }
-}
-
-struct accountView:View{
-    @State var username: String = ""
-    @State var password: String = ""
-    @FocusState private var isFocused:Bool
-    @Binding var isLoggedIn: Int
-    var body: some View {
-        VStack{
-            Form {
-                Section(header:Text("hi")){
-                    Text("一些")
-                    Text("一些")
-                    Text("东西")
-                }
-                Section{
-                    Button(action:{isLoggedIn=0}){
-                        VStack{
-                            Text("退出").foregroundColor(.red)
-                        }
-                    }
-                }
-            }
-            Spacer()
-        }
-    }
-}
 
 struct ContentView_Previews: PreviewProvider {
     static var previews: some View {

+ 177 - 0
sdsz74/account.swift

@@ -0,0 +1,177 @@
+//
+//  account.swift
+//  74 桌
+//
+//  Created by yunli on 2023/5/20.
+//
+
+import SwiftUI
+import Alamofire
+
+struct Constant{
+    var cookiesDefaultsKey:String
+}
+
+let Constants:Constant=(Constant(cookiesDefaultsKey: "JSESSIONID"))
+
+class CookieHandler {
+    
+    static let shared: CookieHandler = CookieHandler()
+    
+    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) {
+            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] ) {
+                    cookieStorage.setCookie(cookie)
+                }
+            }
+        }
+    }
+}
+
+struct Login: Encodable {
+    let user: String
+    let password: String
+    let cook: String
+}
+
+
+class APIFetchHandler {
+    static let sharedInstance = APIFetchHandler()
+    
+    func getCookie(name:String)->HTTPCookie?{
+        if let cookie=CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter({cookie->Bool in
+            return cookie.name==name
+        }).first {
+            return cookie
+        }
+        return nil
+    }
+    
+    func fetchLoginCookie(action:@escaping(_:String)->Void) {
+        if let cookie=getCookie(name:"JSESSOINID") {HTTPCookieStorage.shared.deleteCookie(cookie)}
+        let url = "https://debug.sdsz.icu:81/sso/login";
+        Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response{res in
+            if let cookie=self.getCookie(name:"JSESSIONID"){
+                action(cookie.value)
+            }
+        }
+    }
+}
+
+struct Model:Codable {
+   let userId: Int
+   let id: Int
+   let title: String
+   let body: String
+}
+
+
+func doLogin(user:String,password:String,action:@escaping(_:Int)->Void){
+    APIFetchHandler().fetchLoginCookie(){(cookie:String) in
+        let login = Login(user: user, password: password, cook:cookie)
+        print(login)
+        Alamofire.request("https://debug.sdsz.icu/sso/andlogin", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON{res in
+            print(res)
+        }
+        action(1)
+    }
+}
+
+struct loginView:View{
+    @State var username: String = ""
+    @State var password: String = ""
+    @FocusState private var isFocused:Bool
+    @Binding var isLoggedIn: Int
+    var body: some View {
+        VStack{
+            Form {
+                Section{
+                    HStack {
+                        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))
+                        SecureField(text: $password, prompt: Text("密码")) {
+                            Text("密码")
+                        }.focused($isFocused)
+                    }
+                }.onTapGesture  {
+                    isFocused=false
+                }
+                Section{
+                    HStack{
+                        Spacer()
+                        Button(action:{doLogin(user:username,password: password){(ret:Int) in
+                            isLoggedIn=ret
+                        }}){
+                            VStack{
+                                Text("登录")
+                            }
+                        }.font(.title3)
+                        Spacer()
+                    }
+                }
+            }
+            Spacer()
+        }
+    }
+}
+
+struct accountView:View{
+    @State var username: String = ""
+    @State var password: String = ""
+    @FocusState private var isFocused:Bool
+    @Binding var isLoggedIn: Int
+    var body: some View {
+        VStack{
+            Form {
+                Section(header:Text("hi")){
+                    Text("一些")
+                    Text("一些")
+                    Text("东西")
+                }
+                Section{
+                    Button(action:{isLoggedIn=0}){
+                        VStack{
+                            Text("退出").foregroundColor(.red)
+                        }
+                    }
+                }
+            }
+            Spacer()
+        }
+    }
+}
+
+struct accountView_Previews: PreviewProvider {
+    @State static var isLoggedIn = 0
+    static var previews: some View {
+        loginView(isLoggedIn:$isLoggedIn)
+    }
+}