account.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. struct UserInfo: Encodable {
  45. var name: String
  46. var o: String
  47. }
  48. class LoginHandler {
  49. static let sharedInstance = LoginHandler()
  50. func getCookie(name: String) -> HTTPCookie? {
  51. if let cookie = CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter({ cookie -> Bool in
  52. cookie.name == name
  53. }).first {
  54. return cookie
  55. }
  56. return nil
  57. }
  58. func fetchLoginCookie(action: @escaping (_: String) -> Void) {
  59. if let cookie = getCookie(name: "JSESSIONID") {
  60. HTTPCookieStorage.shared.deleteCookie(cookie)
  61. print(cookie)
  62. }
  63. let url = "https://debug.sdsz.icu:81/sso/login"
  64. AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response { _ in
  65. if let cookie = self.getCookie(name: "JSESSIONID") {
  66. action(cookie.value)
  67. }
  68. }
  69. }
  70. }
  71. func doLogin(user: String, password: String, action: @escaping (_: Int) -> Void) {
  72. LoginHandler().fetchLoginCookie { (cookie: String) in
  73. let login = Login(user: user, pwd: password, cook: cookie)
  74. print(login)
  75. AF.request("https://debug.sdsz.icu/andlogin", method: .post, parameters: login, encoder: JSONParameterEncoder.default, headers: nil).responseString { res in
  76. print("\(res)")
  77. if res.value == "success" {
  78. action(1)
  79. } else {
  80. action(0)
  81. }
  82. }
  83. }
  84. }
  85. func getUserInfo(userInfo: inout UserInfo) {
  86. userInfo.name = "向量"
  87. }
  88. struct loginView: View {
  89. @State var username: String = ""
  90. @State var password: String = ""
  91. @State var btnText: String = "登录"
  92. @State var btnColor: Color = .blue
  93. @State var inProgress: Bool = false
  94. @FocusState private var isFocused: Bool
  95. @Binding var isLoggedIn: Int
  96. @Binding var userInfo: UserInfo
  97. var body: some View {
  98. VStack {
  99. Form {
  100. Section {
  101. HStack {
  102. Image(systemName: "person.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
  103. TextField(text: $username, prompt: Text("数字校园号")) {
  104. Text("数字校园号")
  105. }.focused($isFocused)
  106. .keyboardType(.numberPad)
  107. }
  108. HStack {
  109. Image(systemName: "lock.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
  110. SecureField(text: $password, prompt: Text("密码")) {
  111. Text("密码")
  112. }.focused($isFocused)
  113. }
  114. }.onTapGesture {
  115. isFocused = false
  116. }
  117. HStack {
  118. Spacer()
  119. Button(action: {
  120. inProgress = true
  121. doLogin(user: username, password: password) { (ret: Int) in
  122. isLoggedIn = ret
  123. inProgress = false
  124. if ret == 0 {
  125. btnText = "登录失败"
  126. btnColor = .red
  127. } else {
  128. getUserInfo(userInfo: &userInfo)
  129. }
  130. }
  131. }) {
  132. HStack {
  133. Text(btnText)
  134. if inProgress {
  135. ProgressView()
  136. }
  137. }
  138. }.foregroundColor(btnColor).buttonStyle(.bordered)
  139. }
  140. }
  141. }
  142. }
  143. }
  144. struct accountView: View {
  145. @State var username: String = ""
  146. @State var password: String = ""
  147. @FocusState private var isFocused: Bool
  148. @Binding var isLoggedIn: Int
  149. @Binding var userInfo: UserInfo
  150. var body: some View {
  151. VStack {
  152. Form {
  153. Section(header: Text("基本信息")) {
  154. HStack {
  155. Text("姓名")
  156. Spacer()
  157. Text(userInfo.name)
  158. }
  159. HStack {
  160. Text("所属班级")
  161. Spacer()
  162. Text(userInfo.name)
  163. }
  164. }
  165. Section {
  166. Button(action: {
  167. isLoggedIn = 0
  168. if let cookie = LoginHandler().getCookie(name: "CASTGC") {
  169. HTTPCookieStorage.shared.deleteCookie(cookie)
  170. print(cookie)
  171. }
  172. }) {
  173. VStack {
  174. Text("退出").foregroundColor(.red)
  175. }
  176. }
  177. }
  178. }
  179. Spacer()
  180. }
  181. }
  182. }
  183. struct accountView_Previews: PreviewProvider {
  184. @State static var isLoggedIn = 0
  185. @State static var userInfo: UserInfo = .init(name: "", o: "")
  186. static var previews: some View {
  187. loginView(isLoggedIn: $isLoggedIn, userInfo: $userInfo)
  188. }
  189. }