|
@@ -56,8 +56,8 @@ struct Login: Encodable {
|
|
|
let cook: String
|
|
|
}
|
|
|
|
|
|
-class APIFetchHandler {
|
|
|
- static let sharedInstance = APIFetchHandler()
|
|
|
+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
|
|
@@ -69,7 +69,10 @@ class APIFetchHandler {
|
|
|
}
|
|
|
|
|
|
func fetchLoginCookie(action:@escaping(_:String)->Void) {
|
|
|
- if let cookie=getCookie(name:"JSESSOINID") {HTTPCookieStorage.shared.deleteCookie(cookie)}
|
|
|
+ if let cookie=getCookie(name:"JSESSIONID") {
|
|
|
+ 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{res in
|
|
@@ -81,19 +84,26 @@ class APIFetchHandler {
|
|
|
}
|
|
|
|
|
|
func doLogin(user:String,password:String,action:@escaping(_:Int)->Void){
|
|
|
- APIFetchHandler().fetchLoginCookie(){(cookie:String) in
|
|
|
+ LoginHandler().fetchLoginCookie(){(cookie:String) in
|
|
|
let login = Login(user: user, pwd: password, cook: cookie)
|
|
|
print(login)
|
|
|
AF.request("https://debug.sdsz.icu/andlogin", method: .post, parameters: login,encoder:JSONParameterEncoder.default, headers: nil).responseString{res in
|
|
|
print("\(res)")
|
|
|
+ if res.value=="success"{
|
|
|
+ print("SUCCESS!\(LoginHandler().getCookie(name:"CASTGC"))")
|
|
|
+ action(1)
|
|
|
+ }else{
|
|
|
+ action(0)
|
|
|
+ }
|
|
|
}
|
|
|
- action(1)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
struct loginView:View{
|
|
|
@State var username: String = ""
|
|
|
@State var password: String = ""
|
|
|
+ @State var btnText: String = "登录"
|
|
|
+ @State var btnColor: Color = .blue
|
|
|
@FocusState private var isFocused:Bool
|
|
|
@Binding var isLoggedIn: Int
|
|
|
var body: some View {
|
|
@@ -119,13 +129,19 @@ struct loginView:View{
|
|
|
Section{
|
|
|
HStack{
|
|
|
Spacer()
|
|
|
- Button(action:{doLogin(user:username,password: password){(ret:Int) in
|
|
|
- isLoggedIn=ret
|
|
|
- }}){
|
|
|
+ Button(action:{
|
|
|
+ doLogin(user:username,password: password){(ret:Int) in
|
|
|
+ isLoggedIn=ret
|
|
|
+ if ret==0{
|
|
|
+ btnText = "登录失败"
|
|
|
+ btnColor = .red
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }){
|
|
|
VStack{
|
|
|
- Text("登录")
|
|
|
+ Text(btnText)
|
|
|
}
|
|
|
- }.font(.title3)
|
|
|
+ }.font(.title3).foregroundColor(btnColor)
|
|
|
Spacer()
|
|
|
}
|
|
|
}
|
|
@@ -149,7 +165,13 @@ struct accountView:View{
|
|
|
Text("东西")
|
|
|
}
|
|
|
Section{
|
|
|
- Button(action:{isLoggedIn=0}){
|
|
|
+ Button(action:{
|
|
|
+ isLoggedIn=0
|
|
|
+ if let cookie=LoginHandler().getCookie(name:"CASTGC") {
|
|
|
+ HTTPCookieStorage.shared.deleteCookie(cookie)
|
|
|
+ print(cookie)
|
|
|
+ }
|
|
|
+ }){
|
|
|
VStack{
|
|
|
Text("退出").foregroundColor(.red)
|
|
|
}
|