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

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

55
db.py
View File

@ -1,5 +1,16 @@
import pymysql , config 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): def userSurplus(userkey):
#打开数据库连接 #打开数据库连接
@ -56,3 +67,47 @@ def reduce_value(userkey, value): # 减去对应的值
# 返回新值 # 返回新值
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') @app.route('/admin')
def admin(): def admin():
if "admin" in flask.session : if "admin" in flask.session :
return "管理页" status = {}
status["db"] = db.dbIsOK()
return flask.render_template("status.html" ,status=status)
return "未登录" 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__': if __name__ == '__main__':
app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"]) 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> <button type="submit" class="login-button">登录</button>
</form> </form>
</div> </div>
<script> <script>
</script> </script>

View File

@ -1,5 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh-CN"> <html lang="zh-CN">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>网站后台展示页</title> <title>网站后台展示页</title>
@ -9,35 +10,43 @@
background-color: #f7f7f7; background-color: #f7f7f7;
padding: 20px; padding: 20px;
} }
.container { .container {
background-color: #fff; background-color: #fff;
padding: 20px; padding: 20px;
border-radius: 8px; border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
} }
table { table {
width: 100%; width: 100%;
border-collapse: collapse; border-collapse: collapse;
} }
th, td {
th,
td {
border: 1px solid #ddd; border: 1px solid #ddd;
padding: 8px; padding: 8px;
text-align: left; text-align: left;
} }
th { th {
background-color: #f2f2f2; background-color: #f2f2f2;
} }
.green { .green {
color: green; color: green;
} }
.red { .red {
color: red; color: red;
} }
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h2>网站后台展示页</h2> <h2>状态</h2>
<table> <table>
<thead> <thead>
<tr> <tr>
@ -47,27 +56,53 @@
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>网站访问速度</td> <td>开发者心态</td>
<td class="green">正常</td> <td class="green">正常</td>
</tr> </tr>
<tr> <tr>
<td>数据库连接</td> <td>数据库连接</td>
{% if status["db"] == True %}
<td class="green">正常</td> <td class="green">正常</td>
{% else %}
<td class="red">连接异常</td>
{% endif %}
</tr> </tr>
<tr> <tr>
<td>服务器负载</td> <td>开发者大脑负载</td>
<td class="red">较高</td> <td class="red">较高</td>
</tr> </tr>
</tbody>
</table>
</div>
<hr/>
<div class="container">
<h2>管理</h2>
<table>
<thead>
<tr> <tr>
<td>在线用户数</td> <th>项目</th>
<td class="green">正常</td> </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> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
<script> <script>
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
</script> </script>
</body> </body>
</html> </html>