完成后台管理页面-列出密钥与删除功能

This commit is contained in:
Kakune55 2023-12-11 10:55:44 +08:00
parent 3157ccd3a9
commit bfef66d293
5 changed files with 254 additions and 72 deletions

57
db.py
View File

@ -1,5 +1,16 @@
import pymysql , config
def dbIsOK():
#打开数据库连接
try:
db = pymysql.connect(host=config.readConf()["db"]["host"],
port=config.readConf()["db"]["port"],
user=config.readConf()["db"]["user"],
password=config.readConf()["db"]["passwd"],
database=config.readConf()["db"]["database"])
return True
except:
return False
def userSurplus(userkey):
#打开数据库连接
@ -55,4 +66,48 @@ def reduce_value(userkey, value): # 减去对应的值
db.close()
# 返回新值
return 0
return 0
def getAllKey():
#打开数据库连接
db = pymysql.connect(host=config.readConf()["db"]["host"],
port=config.readConf()["db"]["port"],
user=config.readConf()["db"]["user"],
password=config.readConf()["db"]["passwd"],
database=config.readConf()["db"]["database"])
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute(f"SELECT * FROM usersurplus ;")
# 使用 fetchall() 方法获取结果集
data = cursor.fetchall()
# 关闭连接
db.close()
return data
def delKey(userkey):
#打开数据库连接
db = pymysql.connect(host=config.readConf()["db"]["host"],
port=config.readConf()["db"]["port"],
user=config.readConf()["db"]["user"],
password=config.readConf()["db"]["passwd"],
database=config.readConf()["db"]["database"])
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = '{userkey}';")
# 提交事务
db.commit()
if cursor.rowcount > 0:
db.close() # 使用 rowcount() 方法查询受影响行数
return True
db.close()
return False

27
main.py
View File

@ -70,9 +70,34 @@ def login():
@app.route('/admin')
def admin():
if "admin" in flask.session :
return "管理页"
status = {}
status["db"] = db.dbIsOK()
return flask.render_template("status.html" ,status=status)
return "未登录"
@app.route('/admin/list')
def adminList():
if "admin" in flask.session :
data = db.getAllKey()
return flask.render_template("keylist.html",data=data)
return "未登录 "
@app.route('/admin/createkey')
def createkey():
if "admin" in flask.session :
return flask.render_template("createKey.html")
return "未登录 "
@app.route('/admin/operate', methods=['POST','GET'])
def operate():
if "admin" in flask.session :
if flask.request.args['type'] == "del":
if db.delKey(flask.request.args['target']):
return "成功 <a href='javascript:;' onclick='self.location=document.referrer;'>返回上一页并刷新</a>"
return "失败 <a href='javascript:;' onclick='self.location=document.referrer;'>返回上一页并刷新</a>"
return "拒绝访问"
if __name__ == '__main__':
app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"])

68
templates/keylist.html Normal file
View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>列出密钥</title>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background-color: #f7f7f7;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<h2>列出密钥</h2>
<table>
<thead>
<tr>
<th>Key</th>
<th>剩余Tokens</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td><a href="/admin/operate?type=del&target={{item[0]}}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<hr/>
<script>
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
</script>
</body>
</html>

View File

@ -56,7 +56,6 @@
<button type="submit" class="login-button">登录</button>
</form>
</div>
<script>
</script>

View File

@ -1,73 +1,108 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>网站后台展示页</title>
<style>
body {
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
background-color: #f7f7f7;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.green {
color: green;
}
.red {
color: red;
}
</style>
</head>
<body>
<div class="container">
<h2>网站后台展示页</h2>
<table>
<thead>
<tr>
<th>项目</th>
<th>状态</th>
</tr>
</thead>
<tbody>
<tr>
<td>网站访问速度</td>
<td class="green">正常</td>
</tr>
<tr>
<td>数据库连接</td>
<td class="green">正常</td>
</tr>
<tr>
<td>服务器负载</td>
<td class="red">较高</td>
</tr>
<tr>
<td>在线用户数</td>
<td class="green">正常</td>
</tr>
</tbody>
</table>
</div>
<script>
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
</script>
<head>
<meta charset="UTF-8">
<title>网站后台展示页</title>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background-color: #f7f7f7;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.green {
color: green;
}
.red {
color: red;
}
</style>
</head>
<body>
<div class="container">
<h2>状态</h2>
<table>
<thead>
<tr>
<th>项目</th>
<th>状态</th>
</tr>
</thead>
<tbody>
<tr>
<td>开发者心态</td>
<td class="green">正常</td>
</tr>
<tr>
<td>数据库连接</td>
{% if status["db"] == True %}
<td class="green">正常</td>
{% else %}
<td class="red">连接异常</td>
{% endif %}
</tr>
<tr>
<td>开发者大脑负载</td>
<td class="red">较高</td>
</tr>
</tbody>
</table>
</div>
<hr/>
<div class="container">
<h2>管理</h2>
<table>
<thead>
<tr>
<th>项目</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/admin/list">列出所有Key</a></td>
</tr>
<tr>
<td>查询密钥</td>
</tr>
<tr>
<td><a href="/admin/createkey">创建密钥</a></td>
</tr>
<tr>
<td>阿巴阿巴</td>
</tr>
</tbody>
</table>
</div>
<script>
</script>
</body>
</html>
</html>