xx 2 лет назад
Родитель
Сommit
ea40b1be5c
8 измененных файлов с 149 добавлено и 79 удалено
  1. 0 4
      www/api/admin/admin.lua
  2. 0 0
      www/api/dkjson.lua
  3. 0 6
      www/api/lib/init.lua
  4. 0 17
      www/api/lib/interceptor.lua
  5. 0 2
      www/api/public/info.lua
  6. 50 0
      www/api/user.lua
  7. 3 2
      www/api/website.lua
  8. 96 48
      www/index.html

+ 0 - 4
www/api/admin/admin.lua

@@ -1,4 +0,0 @@
-local dkjson = require("dkjson")
-
-
-response:send("这是需要权限的接口!可以看到这条消息说明你拥有权限。")

+ 0 - 0
www/api/lib/dkjson.lua → www/api/dkjson.lua


+ 0 - 6
www/api/lib/init.lua

@@ -1,6 +0,0 @@
-
-
-print("init fast web success!")
--- 增加拦截器
-interceptor.add("/api/*.*","/api/lib/interceptor.lua")
-return true

+ 0 - 17
www/api/lib/interceptor.lua

@@ -1,17 +0,0 @@
-require "website"
-
-if string.sub(request:filepath(),1,12) == "/api/public/" then
-	--公共开放接口
-	return true
-elseif string.sub(request:filepath(),1,11) == "/api/admin/" then
-	--管理员目录,需要验证权限
-	print(param("key"),"\t")
-	if param("key") == nil or param("key") ~= "123456" then
-		response:send("key不正确")
-		return false
-	end
-	return true
-else
-	response:send("仅允许访问 /api/public 和 /api/admin 目录")
-	return false
-end

+ 0 - 2
www/api/public/info.lua

@@ -1,2 +0,0 @@
- 
- response:send("这是一个公共public接口,可以随意访问")

+ 50 - 0
www/api/user.lua

@@ -0,0 +1,50 @@
+require("api.website")
+local dkjson = require("api.dkjson")
+
+-- 登录
+local function login()
+    -- 获取session
+    local session = session()
+    -- 验证session有效
+    if session:check() then
+        reply(201,"你已经登录过了")
+        return
+    end
+
+    -- 验证账号密码
+    if param("username") ~= "fastweb" or param("password") ~= "123456" then
+        reply(201,"账号或密码不正确")
+        return
+    end
+
+    -- 生成TOKEN
+    local token = make_software_guid()
+    session:init(request,token)
+
+
+    -- 填充用户信息
+    session:set("login_time",time.now_time("%Y-%m-%d %H:%M:%S"))
+
+    reply(200,"",{
+        token = token
+    })
+end
+-- 获取个人信息
+local function getinfo()
+    local session = session()
+    if session:check() == false then
+        reply(401,"登录信息已过期")
+        return
+    end
+
+    reply(200,"",{
+        login_time = session:get("login_time")
+    })
+end
+
+-- 判断请求类型
+if param("action") == "login" then
+    login()
+elseif param("action") == "getinfo" then
+    getinfo()
+end

+ 3 - 2
www/api/lib/website.lua → www/api/website.lua

@@ -1,11 +1,12 @@
-local dkjson = require 'dkjson'
+local dkjson = require("api.dkjson")
 
 function json(data)
 	response:header("Content-Type","application/json")
 	response:send(dkjson.encode(data))
 end
 function session()
-	 return request:session(request:token())
+	 local token = request:header("token")
+	 return request:session(token)
 end
 function param(name)
 	return request:param(name,false)

+ 96 - 48
www/index.html

@@ -1,65 +1,113 @@
 <!DOCTYPE html>
-<html lang="zh-CN">
+<html lang="en">
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>Fast Web</title>
-    <script src="js/jquery-3.4.1.min.js"></script>
+    <title>用户登录</title>
     <style>
-        .button-container {
-            margin-bottom: 10px;
+        body {
+            font-family: Arial, sans-serif;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            height: 100vh;
+            background-color: #f0f0f0;
+            margin: 0;
         }
-        .button-container button {
-            margin-right: 10px;
+        .login-container {
+            background-color: white;
+            padding: 30px;
+            border-radius: 10px;
+            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
+            text-align: center;
+            width: 350px;
+        }
+        .login-container h1 {
+            margin-bottom: 20px;
+            font-size: 24px;
+        }
+        .login-container input[type="text"], .login-container input[type="password"] {
+            width: 100%;
+            padding-top: 15px;
+            padding-bottom: 15px;
+            margin-bottom: 20px;
+            border: 1px solid #ccc;
+            border-radius: 5px;
+        }
+        .login-container button {
+            width: 100%;
+            padding: 15px;
+            background-color: #007BFF;
+            border: none;
+            border-radius: 5px;
+            color: white;
+            font-size: 16px;
+            cursor: pointer;
+        }
+        .login-container button:hover {
+            background-color: #0056b3;
+        }
+        .error-message {
+            color: red;
+            margin-top: 10px;
         }
     </style>
-</head>
-<body>
-    <h1>Fast Web</h1>
-    <div class="button-container">
-        <button id="admin-btn">需要权限地址</button>
-        <button id="public-btn">公共接口</button>
-    </div>
-    <form id="interceptor-form">
-        <label for="path">访问路径:</label>
-        <input type="text" id="path" name="path" required>
-        <br><br>
-        <label for="key">Key:</label>
-        <input type="text" id="key" name="key" required>
-        <br><br>
-        <button type="submit">提交</button>
-    </form>
-
+    <script src="/js/jquery-3.4.1.min.js"></script>
     <script>
-        $(document).ready(function(){
-            $('#admin-btn').click(function(){
-                $('#path').val('/api/admin/admin.lua');
-            });
+        var token = ""
+        function login() {
+            const username = $('#username').val();
+            const password = $('#password').val();
+            if (!username || !password) {
+                alert('请输入用户名和密码');
+                return;
+            }
 
-            $('#public-btn').click(function(){
-                $('#path').val('/api/public/info.lua');
+            $.ajax({
+                url: '/api/user.lua?action=login',
+                type: 'POST',
+                contentType: 'application/json',
+                data: JSON.stringify({ username: username, password: password }),
+                success: function(response) {
+                    if (response.code === 200) {
+                        token = response.data.token;
+                        fetchUserInfo();
+                    } else {
+                        $('#errorMessage').text(response.msg || '登录失败,请重试');
+                    }
+                }
             });
+        }
 
-            $('#interceptor-form').on('submit', function(event){
-                event.preventDefault();
-                var path = $('#path').val();
-                var key = $('#key').val();
-                var data = JSON.stringify({ key: key });
-
-                $.ajax({
-                    url: path,
-                    method: 'POST',
-                    contentType: 'application/json',
-                    data: data,
-                    success: function(response) {
-                        alert('请求成功: ' + response);
-                    },
-                    error: function(jqXHR, textStatus, errorThrown) {
-                        alert('请求失败: ' + textStatus + ' - ' + errorThrown);
+        function fetchUserInfo() {
+            $.ajax({
+                url: '/api/user.lua?action=getinfo',
+                type: 'GET',
+                headers:{
+                    "token":token
+                },
+                success: function(response) {
+                    if (response.code === 200) {
+                        const userInfo = response.data;
+                        alert(`用户信息:\n登录时间: ${userInfo.login_time}`);
+                    } else {
+                        alert('获取用户信息失败,请重试');
                     }
-                });
+                },
+                error: function() {
+                    alert('获取用户信息失败,请重试');
+                }
             });
-        });
+        }
     </script>
+</head>
+<body>
+    <div class="login-container">
+        <h1>用户登录</h1>
+        <input type="text" id="username" placeholder="请输入用户名">
+        <input type="password" id="password" placeholder="请输入密码">
+        <button onclick="login()">确认</button>
+        <div id="errorMessage" class="error-message"></div>
+    </div>
 </body>
 </html>