Browse Source

update cookie

schtonn 1 year ago
parent
commit
c9bb617053
2 changed files with 30 additions and 36 deletions
  1. 6 6
      sdsz74/ContentView.swift
  2. 24 30
      sdsz74/account.swift

+ 6 - 6
sdsz74/ContentView.swift

@@ -63,9 +63,9 @@ extension View {
     }
 }
 
-//
-// struct ContentView_Previews: PreviewProvider {
-//    static var previews: some View {
-//        ContentView()
-//    }
-// }
+
+ struct ContentView_Previews: PreviewProvider {
+    static var previews: some View {
+        ContentView()
+    }
+ }

+ 24 - 30
sdsz74/account.swift

@@ -27,25 +27,13 @@ class CookieHandler {
         return cookies
     }
 
-    func backupCookies(forURL url: String) {
-        var cookieDict = [String: AnyObject]()
+    func getAllCookie() -> [HTTPCookie] {
+        let cookies = cookieStorage.cookies ?? []
 
-        for cookie in getCookie(forURL: url) {
-            cookieDict[cookie.name] = cookie.properties as AnyObject?
-        }
-
-        defaults.set(cookieDict, forKey: Constants.cookiesDefaultsKey)
+        return cookies
     }
 
-    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)
-                }
-            }
-        }
-    }
+    func delCookie() {}
 }
 
 struct Login: Encodable {
@@ -62,24 +50,27 @@ struct UserInfo: 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
+    func getCookie(name: String) -> [HTTPCookie] {
+        return CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter { cookie -> Bool in
+            cookie.name == name
+        }
+    }
+
+    func getAllCookie(name: String) -> [HTTPCookie] {
+        return CookieHandler().getAllCookie().filter { cookie -> Bool in
             cookie.name == name
-        }).first {
-            return cookie
         }
-        return nil
     }
 
     func fetchLoginCookie(action: @escaping (_: String) -> Void) {
-        if let cookie = getCookie(name: "JSESSIONID") {
+        if let cookie = getCookie(name: "JSESSIONID").first {
             HTTPCookieStorage.shared.deleteCookie(cookie)
             print(cookie)
         }
         let url = "https://debug.sdsz.icu:81/sso/login"
 
         AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response { _ in
-            if let cookie = self.getCookie(name: "JSESSIONID") {
+            if let cookie = self.getCookie(name: "JSESSIONID").first {
                 action(cookie.value)
             }
         }
@@ -99,12 +90,11 @@ class FetchHandler {
 
     func getUrl(url: String) -> String {
         var ret: String = url
-        print("PRE: \(ret)")
         ret = ret
             .replacing("dd.sdsz.com.cn", with: "debug.sdsz.icu:81")
             .replacing(/^http:/, with: "https:")
             .replacing("service=http%3A%2F%2Fdebug.sdsz.icu:81", with: "service=http%3A%2F%2Fdd.sdsz.com.cn")
-        print("AFTER: \(ret)")
+        print("URL: \(ret)")
         return ret
     }
 
@@ -113,7 +103,6 @@ class FetchHandler {
             action(url)
         } else {
             AF.request(getUrl(url: url), method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseString { res in
-                print(res)
                 if let resv = res.value {
                     self.workFetch(url: resv, action: action)
                 }
@@ -225,6 +214,13 @@ struct accountView: View {
     @FocusState private var isFocused: Bool
     @Binding var isLoggedIn: Int
     @Binding var userInfo: UserInfo
+    func delAllCookie(name: String) {
+        for cookie in LoginHandler().getAllCookie(name: name) {
+            print(cookie)
+            HTTPCookieStorage.shared.deleteCookie(cookie)
+        }
+    }
+
     var body: some View {
         VStack {
             Form {
@@ -243,10 +239,8 @@ struct accountView: View {
                 Section {
                     Button(action: {
                         isLoggedIn = 0
-                        if let cookie = LoginHandler().getCookie(name: "CASTGC") {
-                            HTTPCookieStorage.shared.deleteCookie(cookie)
-                            print(cookie)
-                        }
+                        delAllCookie(name: "CASTGC")
+                        delAllCookie(name: "JSESSIONID")
                     }) {
                         VStack {
                             Text("退出").foregroundColor(.red)