From e42a2042a9409ec519a729d619cd1ceb0cd456e7 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Mon, 11 Dec 2023 13:37:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=88=9B=E5=BB=BA=E5=AF=86?= =?UTF-8?q?=E9=92=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.py | 31 +++++++++++++++++++- main.py | 11 ++++++-- templates/createKey.html | 61 ++++++++++++++++++++++++++++++++++++++++ templates/status.html | 10 +++++-- 4 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 templates/createKey.html diff --git a/db.py b/db.py index 1351cf6..8b033ba 100644 --- a/db.py +++ b/db.py @@ -1,4 +1,4 @@ -import pymysql , config +import pymysql , config , uuid def dbIsOK(): #打开数据库连接 @@ -111,3 +111,32 @@ def delKey(userkey): db.close() 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 + diff --git a/main.py b/main.py index 0215fd5..ada298a 100644 --- a/main.py +++ b/main.py @@ -83,10 +83,17 @@ def adminList(): return flask.render_template("keylist.html",data=data) return "未登录 " -@app.route('/admin/createkey') +@app.route('/admin/createkey', methods=['POST','GET']) def createkey(): 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 "未登录 " @app.route('/admin/operate', methods=['POST','GET']) diff --git a/templates/createKey.html b/templates/createKey.html new file mode 100644 index 0000000..1814dac --- /dev/null +++ b/templates/createKey.html @@ -0,0 +1,61 @@ + + + + + + 创建密钥 + + + + +
+

创建单个密钥

+
+ 密钥 + + 配额 + + +
+
+
+
+

批量创建密钥

+
+ +
+ 个数 + + 配额 + + +
+
+ {% if resq != "null" %} +
+
+

执行结果

+ {% for i in resq %} +

{{ i }}

+ {% endfor %} +
+ {% endif %} + + + + \ No newline at end of file diff --git a/templates/status.html b/templates/status.html index fe634af..616bbc3 100644 --- a/templates/status.html +++ b/templates/status.html @@ -3,7 +3,7 @@ - 网站后台展示页 + 后台管理 @@ -85,13 +89,13 @@ - 列出所有Key + 列出所有Key 查询密钥 - 创建密钥 + 创建密钥 阿巴阿巴