完成创建密钥功能

This commit is contained in:
Kakune55 2023-12-11 13:37:04 +08:00
parent bfef66d293
commit e42a2042a9
4 changed files with 107 additions and 6 deletions

31
db.py
View File

@ -1,4 +1,4 @@
import pymysql , config import pymysql , config , uuid
def dbIsOK(): def dbIsOK():
#打开数据库连接 #打开数据库连接
@ -111,3 +111,32 @@ def delKey(userkey):
db.close() db.close()
return False return False
def createKey(quota,number=1,key="null"):
#打开数据库连接
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 查询
output = []
if key == "null":
for i in range(int(number)):
key = str(uuid.uuid1())
output.append(key)
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES ('{key}',{int(quota)});")
else:
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES ('{key}',{int(quota)});")
output.append(key)
# 提交事务
db.commit()
db.close()
return output

11
main.py
View File

@ -83,10 +83,17 @@ def adminList():
return flask.render_template("keylist.html",data=data) return flask.render_template("keylist.html",data=data)
return "未登录 " return "未登录 "
@app.route('/admin/createkey') @app.route('/admin/createkey', methods=['POST','GET'])
def createkey(): def createkey():
if "admin" in flask.session : if "admin" in flask.session :
return flask.render_template("createKey.html") if flask.request.method == "GET":
return flask.render_template("createKey.html",resq="null")
if "number" in flask.request.form: # 创建单个还是多个
resq = db.createKey(flask.request.form["quota"], flask.request.form["number"])
elif "key" in flask.request.form:
resq = db.createKey(flask.request.form["quota"],1,flask.request.form["key"])
return flask.render_template("createKey.html",resq=resq)
return "未登录 " return "未登录 "
@app.route('/admin/operate', methods=['POST','GET']) @app.route('/admin/operate', methods=['POST','GET'])

61
templates/createKey.html Normal file
View File

@ -0,0 +1,61 @@
<!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);
}
</style>
</head>
<body>
<div class="container">
<h2>创建单个密钥</h2>
<form method="post">
<span>密钥</span>
<input name="key" required>
<span>配额</span>
<input name="quota" required>
<button type="submit">创建</button>
</form>
</div>
<hr>
<div class="container">
<h2>批量创建密钥</h2>
<form method="post">
<input type="radio" id="type" name="type" value="uuid" checked="true">
<label for="type">使用UUID</label><br>
<span>个数</span>
<input name="number" required>
<span>配额</span>
<input name="quota" required>
<button type="submit">批量创建</button>
</form>
</div>
{% if resq != "null" %}
<hr>
<div class="container">
<h2>执行结果</h2>
{% for i in resq %}
<h4>{{ i }}</h4>
{% endfor %}
</div>
{% endif %}
<script>
</script>
</body>
</html>

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>网站后台展示页</title> <title>后台管理</title>
<style> <style>
body { body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
@ -41,6 +41,10 @@
.red { .red {
color: red; color: red;
} }
.nodecoration{
text-decoration: none;
}
</style> </style>
</head> </head>
@ -85,13 +89,13 @@
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td><a href="/admin/list">列出所有Key</a></td> <td><a href="/admin/list" class="nodecoration">列出所有Key</a></td>
</tr> </tr>
<tr> <tr>
<td>查询密钥</td> <td>查询密钥</td>
</tr> </tr>
<tr> <tr>
<td><a href="/admin/createkey">创建密钥</a></td> <td><a href="/admin/createkey" class="nodecoration">创建密钥</a></td>
</tr> </tr>
<tr> <tr>
<td>阿巴阿巴</td> <td>阿巴阿巴</td>