Browse Source

登入瀏覽權限管控

mike 3 years ago
parent
commit
5d6e3363e7
6 changed files with 142 additions and 85 deletions
  1. 29 3
      assets/js/ark.js
  2. 39 31
      mint.html
  3. 31 25
      nft.html
  4. 5 0
      nft_add.html
  5. 31 25
      transaction.html
  6. 7 1
      user.html

+ 29 - 3
assets/js/ark.js

@@ -1,3 +1,19 @@
+// 取得access_token
+function get_access_token() {
+    console.log(localStorage.access_token); // test
+    
+    return localStorage.access_token;
+}
+
+// 檢查是否可存取該頁面
+function checkRoute() {
+    let access_token = get_access_token();
+
+    if(access_token == undefined) {
+      window.location.replace("login.html");
+    }
+}
+
 // 帳號登入
 $("#btn_login").click(function() {
     var username = $('#username').val();
@@ -17,22 +33,32 @@ $("#btn_login").click(function() {
         url: 'https://api.ptt.cx:8750/api/v1/login/access-token',
         data: param_string,
         success: function(res, textStatus, jqXHR) {
+            // {access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2N…kyIn0.wwRK7OKPICdbcOhLfgqA5u5uxkhRVP2gQeqEF8tCY4g', token_type: 'bearer'}
             console.log(res); // test
 
             // 將access_token寫入localStorage
-            // {access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2N…kyIn0.wwRK7OKPICdbcOhLfgqA5u5uxkhRVP2gQeqEF8tCY4g', token_type: 'bearer'}
-            var access_token = res.access_token;
+            localStorage.access_token = res.access_token;
+
+            // 檢查是否已登入
+            let access_token = get_access_token();
 
-            if (access_token != null) {
+            if (access_token == undefined) {
+                window.location.replace("login.html");
+            }
+
+            if (access_token) {
                 // 導頁進Dashboard介面
                 Swal.fire({
                     title: '登入成功',
                     icon: 'success',
                     confirmButtonColor: '#3085d6'
                 });
+
                 window.setTimeout(() => {
                     window.location.href = 'index.html';
                 }, 1500);
+            } else {
+                console.log('else'); // test
             }
         }, error: function(xhr, status, error) {
             console.log(xhr.responseText); // test

+ 39 - 31
mint.html

@@ -617,39 +617,47 @@
   <!-- Template Main JS File -->
   <script src="assets/js/main.js"></script>
 
+  <!-- Ark Cards JS File -->
+  <script src="assets/js/ark.js"></script>
+
 <script>
-// 帳號登入
-$("#btn_mint").click(function() {
-  var toaddress = $('#toaddress').val();
-  var amount = $('#amount').val();
-
-  var param_string = {
-    toaddress: toaddress,
-    amount: amount
-  };
-
-  $.ajax({
-    type: 'POST',
-    url: 'https://api.ptt.cx:8750/api/v1/joyso/mint',
-    data: param_string,
-    success: function(res, textStatus, jqXHR) {
-      console.log(res); // test
-    }, error: function(xhr, status, error) {
-      // {"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}
-      var res = JSON.parse(xhr.responseText);
-      console.log(res.detail[0].msg); // test
-      
-      Swal.fire({
-          title: '注意',
-          icon: 'error',
-          // text: res.detail[0].msg,
-          text: 'API目前有問題,暫時無法執行此操作',
-          confirmButtonColor: '#3085d6'
-      });
-    }
+$(document).ready(function($) {
+  // 檢查是否可存取該頁面
+  checkRoute();
+
+  // 帳號登入
+  $("#btn_mint").click(function() {
+    var toaddress = $('#toaddress').val();
+    var amount = $('#amount').val();
+
+    var param_string = {
+      toaddress: toaddress,
+      amount: amount
+    };
+
+    $.ajax({
+      type: 'POST',
+      url: 'https://api.ptt.cx:8750/api/v1/joyso/mint',
+      data: param_string,
+      success: function(res, textStatus, jqXHR) {
+        console.log(res); // test
+      }, error: function(xhr, status, error) {
+        // {"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}
+        var res = JSON.parse(xhr.responseText);
+        console.log(res.detail[0].msg); // test
+        
+        Swal.fire({
+            title: '注意',
+            icon: 'error',
+            // text: res.detail[0].msg,
+            text: 'API目前有問題,暫時無法執行此操作',
+            confirmButtonColor: '#3085d6'
+        });
+      }
+    });
+
+    return false;
   });
-
-  return false;
 });
 </script>
   

+ 31 - 25
nft.html

@@ -636,35 +636,41 @@
   <!-- Template Main JS File -->
   <script src="assets/js/main.js"></script>
 
+  <!-- Ark Cards JS File -->
+  <script src="assets/js/ark.js"></script>
+
 <script>
-  $(document).ready(function($) {
-    var url = 'https://api.ptt.cx:8750/api/v1/nft/?skip=0&limit=100';
-    var access_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDEyOTIyMzEsInN1YiI6IjkyIn0.9GuI_mP-dHxKOEMo6mTmtweQmis86L5Y6gOscZx5UIg';
-    var headers = {'Authorization': 'Bearer ' + access_token}
-  
-    $.ajax({
-      url: url,
-      headers: headers,
-      success: function(res, status, jqXHR) {
-        console.log(res); // test
-    
-        var temp = "";
+$(document).ready(function($) {
+  // 檢查是否可存取該頁面
+  checkRoute();
+
+  var url = 'https://api.ptt.cx:8750/api/v1/nft/?skip=0&limit=100';
+  var access_token = get_access_token();
+  var headers = {'Authorization': 'Bearer ' + access_token}
+
+  $.ajax({
+    url: url,
+    headers: headers,
+    success: function(res, status, jqXHR) {
+      console.log(res); // test
   
-        for (let i=0; i < res.length; i++) {
-          temp += '<tr> \
-            <th scope="row"><img src="' + res[i].Nft.imgurl + '" alt="' + res[i].Nft.title + '"></th> \
-            <td class="fw-bold">' + res[i].Nft.title + '</td> \
-            <td>' + res[i].count + '</td>';
-          
-          temp += '</tr>';
-        }
-  
-        $(".nftdata").html(temp);
-      }, error: function(xhr, status, error) {
-        console.log(xhr.responseText); // test
+      var temp = "";
+
+      for (let i=0; i < res.length; i++) {
+        temp += '<tr> \
+          <th scope="row"><img src="' + res[i].Nft.imgurl + '" alt="' + res[i].Nft.title + '"></th> \
+          <td class="fw-bold">' + res[i].Nft.title + '</td> \
+          <td>' + res[i].count + '</td>';
+        
+        temp += '</tr>';
       }
-    });
+
+      $(".nftdata").html(temp);
+    }, error: function(xhr, status, error) {
+      console.log(xhr.responseText); // test
+    }
   });
+});
 </script>
 </body>
 

+ 5 - 0
nft_add.html

@@ -886,7 +886,12 @@
   <!-- Template Main JS File -->
   <script src="assets/js/main.js"></script>
 
+  <!-- Ark Cards JS File -->
+  <script src="assets/js/ark.js"></script>
+
 <script>
+// 檢查是否可存取該頁面
+checkRoute();
 
 $("#btn_nft_add").click(function() {
   // 導頁進NFT管理介面

+ 31 - 25
transaction.html

@@ -624,36 +624,42 @@
   <!-- Template Main JS File -->
   <script src="assets/js/main.js"></script>
 
+  <!-- Ark Cards JS File -->
+  <script src="assets/js/ark.js"></script>
+
 <script>
-  $(document).ready(function($) {
-    var url = 'https://api.ptt.cx:8750/api/v1/line/transactions/';
-    var access_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDEyOTIyMzEsInN1YiI6IjkyIn0.9GuI_mP-dHxKOEMo6mTmtweQmis86L5Y6gOscZx5UIg';
-    var headers = {'Authorization': 'Bearer ' + access_token}
-  
-    $.ajax({
-      url: url,
-      headers: headers,
-      success: function(res, status, jqXHR) {
-        console.log(res); // test
-    
-        var temp = "";
+$(document).ready(function($) {
+  // 檢查是否可存取該頁面
+  checkRoute();
   
-        for (let i=0; i < res.length; i++) {
-          temp += '<tr> \
-            <td>' + res[i].id + '</td> \
-            <td>' + res[i].tfrom + '</td> \
-            <td>' + res[i].to + '</td> \
-            <td>' + res[i].nft + '</td> \
-            <td>' + res[i].transaction_at + '</td> \
-          </tr>';
-        }
+  var url = 'https://api.ptt.cx:8750/api/v1/line/transactions/';
+  var access_token = get_access_token();
+  var headers = {'Authorization': 'Bearer ' + access_token}
+
+  $.ajax({
+    url: url,
+    headers: headers,
+    success: function(res, status, jqXHR) {
+      console.log(res); // test
   
-        $(".transaction_data").html(temp);
-      }, error: function(xhr, status, error) {
-        console.log(xhr.responseText); // test
+      var temp = "";
+
+      for (let i=0; i < res.length; i++) {
+        temp += '<tr> \
+          <td>' + res[i].id + '</td> \
+          <td>' + res[i].tfrom + '</td> \
+          <td>' + res[i].to + '</td> \
+          <td>' + res[i].nft + '</td> \
+          <td>' + res[i].transaction_at + '</td> \
+        </tr>';
       }
-    });
+
+      $(".transaction_data").html(temp);
+    }, error: function(xhr, status, error) {
+      console.log(xhr.responseText); // test
+    }
   });
+});
 </script>
 </body>
 

+ 7 - 1
user.html

@@ -629,10 +629,16 @@
   <!-- Template Main JS File -->
   <script src="assets/js/main.js"></script>
 
+  <!-- Ark Cards JS File -->
+  <script src="assets/js/ark.js"></script>
+
 <script>
 $(document).ready(function($) {
+  // 檢查是否可存取該頁面
+  checkRoute();
+
   var url = 'https://api.ptt.cx:8750/api/v1/user/';
-  var access_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDEyOTIyMzEsInN1YiI6IjkyIn0.9GuI_mP-dHxKOEMo6mTmtweQmis86L5Y6gOscZx5UIg';
+  var access_token = get_access_token();
   var headers = {'Authorization': 'Bearer ' + access_token}
 
   $.ajax({