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 @@ + + + +
+ +