huaisianhuang %!s(int64=3) %!d(string=hai) anos
pai
achega
5913c684a0
Modificáronse 1 ficheiros con 71 adicións e 5 borrados
  1. 71 5
      html/login.js

+ 71 - 5
html/login.js

@@ -21,7 +21,75 @@ function loginByEnter(e) {
 function login(){
     console.log('login!');
     var url = "https://www.choozmo.com:8887/login";
-    var xhr = new XMLHttpRequest();
+    var username = $('#username').val();
+    var password = $('#password').val();
+
+    if (username == '' || password == '') {
+        let title = "登入失敗";
+        let text = "請先輸入您的帳號/密碼";
+
+        if (lang == 'en') { 
+            title = "Login Failed!";
+            text = "Please enter your username and password";
+        }
+
+        Swal.fire({
+            title: title,
+            icon: 'error',
+            text: text,
+            confirmButtonColor: '#3085d6',
+        });
+
+        return;
+    }
+    
+    var data = "grant_type=&username=" + username + "&password="+password+"&scope=&client_id=&client_secret=";
+    const headers = {
+        "accept": "application/json",
+        "Content-Type": "application/x-www-form-urlencoded"
+    }
+    axios({
+        method: 'post',
+        url: url,
+        headers: headers,
+        data: data
+    }).then(res => {
+        console.log(res.data);
+        if(res.data.access_token!=null) {
+            var title = "登入成功";
+            if (lang == 'en') { // 英文版訊息
+                title = "Login Successfully!"
+            }
+
+            Swal.fire({
+                title: title,
+                icon: 'success',
+                confirmButtonColor: '#3085d6',
+            });
+            window.setTimeout(() => {
+                window.location.href = 'user_profile2.html';
+            }, 2000);
+            btnLoginPage.style.display = 'none';
+        } else {
+            var title = "登入失敗";
+
+            if (lang == 'en') { // 英文版訊息
+                title = "Login Failed!"
+            }
+
+            Swal.fire({
+                title: title,
+                icon: 'error',
+                text: responseOBJ.detail,
+                confirmButtonColor: '#3085d6',
+            });
+        }
+        
+    }).catch(err => {
+        console.log(err);
+    })
+}
+/*     var xhr = new XMLHttpRequest();
     xhr.open("POST", url);
     xhr.setRequestHeader("accept", "application/json");
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
@@ -67,8 +135,6 @@ function login(){
         }
     };
     
-    var username = $('#username').val();
-    var password = $('#password').val();
 
     // 登入資料檢查
     if (username == '' || password == '') {
@@ -90,10 +156,10 @@ function login(){
         return;
     }
 
-    var data = "grant_type=&username=" + username + "&password="+password+"&scope=&client_id=&client_secret=";
+    
     result = xhr.send(data);
     console.log(result);
-}
+} */
 
 function loginControl() {
     btnLoginPage.style.display = 'block';