account.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // account.swift
  3. // 74 桌
  4. //
  5. // Created by yunli on 2023/5/20.
  6. //
  7. import SwiftUI
  8. import Alamofire
  9. struct Constant{
  10. var cookiesDefaultsKey:String
  11. }
  12. let Constants:Constant=(Constant(cookiesDefaultsKey: "JSESSIONID"))
  13. class CookieHandler {
  14. static let shared: CookieHandler = CookieHandler()
  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) -> Void {
  23. var cookieDict = [String : AnyObject]()
  24. for cookie in self.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 APIFetchHandler {
  45. static let sharedInstance = APIFetchHandler()
  46. func getCookie(name:String)->HTTPCookie?{
  47. if let cookie=CookieHandler().getCookie(forURL: "https://debug.sdsz.icu:81/").filter({cookie->Bool in
  48. return 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:"JSESSOINID") {HTTPCookieStorage.shared.deleteCookie(cookie)}
  56. let url = "https://debug.sdsz.icu:81/sso/login";
  57. AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response{res in
  58. if let cookie=self.getCookie(name:"JSESSIONID"){
  59. action(cookie.value)
  60. }
  61. }
  62. }
  63. }
  64. func doLogin(user:String,password:String,action:@escaping(_:Int)->Void){
  65. APIFetchHandler().fetchLoginCookie(){(cookie:String) in
  66. let login = Login(user: user, pwd: password, cook: cookie)
  67. print(login)
  68. AF.request("https://debug.sdsz.icu/andlogin", method: .post, parameters: login,encoder:JSONParameterEncoder.default, headers: nil).responseString{res in
  69. print("\(res)")
  70. }
  71. action(1)
  72. }
  73. }
  74. struct loginView:View{
  75. @State var username: String = ""
  76. @State var password: String = ""
  77. @FocusState private var isFocused:Bool
  78. @Binding var isLoggedIn: Int
  79. var body: some View {
  80. VStack{
  81. Form {
  82. Section{
  83. HStack {
  84. Image(systemName:"person.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
  85. TextField(text: $username, prompt: Text("数字校园号")) {
  86. Text("数字校园号")
  87. }.focused($isFocused)
  88. .keyboardType(.numberPad)
  89. }
  90. HStack {
  91. Image(systemName:"lock.fill").foregroundColor(Color(red: 0.7, green: 0.7, blue: 0.7))
  92. SecureField(text: $password, prompt: Text("密码")) {
  93. Text("密码")
  94. }.focused($isFocused)
  95. }
  96. }.onTapGesture {
  97. isFocused=false
  98. }
  99. Section{
  100. HStack{
  101. Spacer()
  102. Button(action:{doLogin(user:username,password: password){(ret:Int) in
  103. isLoggedIn=ret
  104. }}){
  105. VStack{
  106. Text("登录")
  107. }
  108. }.font(.title3)
  109. Spacer()
  110. }
  111. }
  112. }
  113. Spacer()
  114. }
  115. }
  116. }
  117. struct accountView:View{
  118. @State var username: String = ""
  119. @State var password: String = ""
  120. @FocusState private var isFocused:Bool
  121. @Binding var isLoggedIn: Int
  122. var body: some View {
  123. VStack{
  124. Form {
  125. Section(header:Text("hi")){
  126. Text("一些")
  127. Text("一些")
  128. Text("东西")
  129. }
  130. Section{
  131. Button(action:{isLoggedIn=0}){
  132. VStack{
  133. Text("退出").foregroundColor(.red)
  134. }
  135. }
  136. }
  137. }
  138. Spacer()
  139. }
  140. }
  141. }
  142. struct accountView_Previews: PreviewProvider {
  143. @State static var isLoggedIn = 0
  144. static var previews: some View {
  145. loginView(isLoggedIn:$isLoggedIn)
  146. }
  147. }