account.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // account.swift
  3. // 74 桌
  4. //
  5. // Created by yunli on 2023/5/20.
  6. //
  7. import Alamofire
  8. import SwiftUI
  9. struct Constant {
  10. var cookiesDefaultsKey: String
  11. }
  12. let Constants: Constant = .init(cookiesDefaultsKey: "JSESSIONID")
  13. class CookieHandler {
  14. static let shared: CookieHandler = .init()
  15. let defaults = UserDefaults.standard
  16. let cookieStorage = HTTPCookieStorage.shared
  17. func getCookie(forURL url: String) -> [HTTPCookie] {
  18. let computedUrl = URL(string: url)
  19. let cookies = cookieStorage.cookies(for: computedUrl!) ?? []
  20. return cookies
  21. }
  22. func backupCookies(forURL url: String) {
  23. var cookieDict = [String: AnyObject]()
  24. for cookie in getCookie(forURL: url) {
  25. cookieDict[cookie.name] = cookie.properties as AnyObject?
  26. }
  27. defaults.set(cookieDict, forKey: Constants.cookiesDefaultsKey)
  28. }
  29. func restoreCookies() {
  30. if let cookieDictionary = defaults.dictionary(forKey: Constants.cookiesDefaultsKey) {
  31. for (_, cookieProperties) in cookieDictionary {
  32. if let cookie = HTTPCookie(properties: cookieProperties as! [HTTPCookiePropertyKey: Any]) {
  33. cookieStorage.setCookie(cookie)
  34. }
  35. }
  36. }
  37. }
  38. }
  39. struct Login: Encodable {
  40. let user: String
  41. let pwd: String
  42. let cook: String
  43. }
  44. class LoginHandler {
  45. static let sharedInstance = LoginHandler()
  46. func getCookie(name: String) -> HTTPCookie? {
  47. if let cookie = CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter({ cookie -> Bool in
  48. cookie.name == name
  49. }).first {
  50. return cookie
  51. }
  52. return nil
  53. }
  54. func fetchLoginCookie(action: @escaping (_: String) -> Void) {
  55. if let cookie = getCookie(name: "JSESSIONID") {
  56. HTTPCookieStorage.shared.deleteCookie(cookie)
  57. print(cookie)
  58. }
  59. let url = "https://debug.sdsz.icu:81/sso/login"
  60. AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response { _ in
  61. if let cookie = self.getCookie(name: "JSESSIONID") {
  62. action(cookie.value)
  63. }
  64. }
  65. }
  66. }
  67. func doLogin(user: String, password: String, action: @escaping (_: Int) -> Void) {
  68. LoginHandler().fetchLoginCookie { (cookie: String) in
  69. let login = Login(user: user, pwd: password, cook: cookie)
  70. print(login)
  71. AF.request("https://debug.sdsz.icu/andlogin", method: .post, parameters: login, encoder: JSONParameterEncoder.default, headers: nil).responseString { res in
  72. print("\(res)")
  73. if res.value == "success" {
  74. print("SUCCESS!\(LoginHandler().getCookie(name: "CASTGC"))")
  75. action(1)
  76. } else {
  77. action(0)
  78. }
  79. }
  80. }
  81. }
  82. struct loginView: View {
  83. @State var username: String = ""
  84. @State var password: String = ""
  85. @State var btnText: String = "登录"
  86. @State var btnColor: Color = .blue
  87. @FocusState private var isFocused: Bool
  88. @Binding var isLoggedIn: Int
  89. var body: some View {
  90. VStack {
  91. Form {
  92. Section {
  93. HStack {
  94. Image(systemName: "person.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
  95. TextField(text: $username, prompt: Text("数字校园号")) {
  96. Text("数字校园号")
  97. }.focused($isFocused)
  98. .keyboardType(.numberPad)
  99. }
  100. HStack {
  101. Image(systemName: "lock.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
  102. SecureField(text: $password, prompt: Text("密码")) {
  103. Text("密码")
  104. }.focused($isFocused)
  105. }
  106. }.onTapGesture {
  107. isFocused = false
  108. }
  109. Section {
  110. HStack {
  111. Spacer()
  112. Button(action: {
  113. doLogin(user: username, password: password) { (ret: Int) in
  114. isLoggedIn = ret
  115. if ret == 0 {
  116. btnText = "登录失败"
  117. btnColor = .red
  118. }
  119. }
  120. }) {
  121. VStack {
  122. Text(btnText)
  123. }
  124. }.font(.title3).foregroundColor(btnColor)
  125. Spacer()
  126. }
  127. }
  128. }
  129. Spacer()
  130. }
  131. }
  132. }
  133. struct accountView: View {
  134. @State var username: String = ""
  135. @State var password: String = ""
  136. @FocusState private var isFocused: Bool
  137. @Binding var isLoggedIn: Int
  138. var body: some View {
  139. VStack {
  140. Form {
  141. Section(header: Text("hi")) {
  142. Text("一些")
  143. Text("一些")
  144. Text("东西")
  145. }
  146. Section {
  147. Button(action: {
  148. isLoggedIn = 0
  149. if let cookie = LoginHandler().getCookie(name: "CASTGC") {
  150. HTTPCookieStorage.shared.deleteCookie(cookie)
  151. print(cookie)
  152. }
  153. }) {
  154. VStack {
  155. Text("退出").foregroundColor(.red)
  156. }
  157. }
  158. }
  159. }
  160. Spacer()
  161. }
  162. }
  163. }
  164. struct accountView_Previews: PreviewProvider {
  165. @State static var isLoggedIn = 0
  166. static var previews: some View {
  167. loginView(isLoggedIn: $isLoggedIn)
  168. }
  169. }