From 3157ccd3a91b852331bbdec2efe5341f57399cb8 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Sun, 10 Dec 2023 21:12:52 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=A1=B5=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=B3=BB=E7=BB=9F=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 20 ++++++++++++ templates/login.html | 65 ++++++++++++++++++++++++++++++++++++++ templates/status.html | 73 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 templates/login.html create mode 100644 templates/status.html diff --git a/main.py b/main.py index a626ca9..156b0e3 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from apiModule import qwenTurbo , chatglmTurbo , gpt35Turbo , gpt4Turbo app = flask.Flask(__name__) CORS(app,origins="*") +app.secret_key = b'SQ-{kJE;m(jEBi|{yq]v' @app.route('/api/user', methods=['POST']) def post_data(): @@ -49,10 +50,29 @@ def post_data(): db.reduce_value(userRequest['userkey'], tokenUsed) return {"code":code,"output":output,"surplus":surplusToken} + @app.route('/') def index(): return flask.render_template('index.html') +@app.route('/login', methods=['POST','GET']) +def login(): + if flask.request.method == 'GET': + return flask.render_template('login.html') + userRequest = flask.request.form + if userRequest["password"] != config.readConf()["appconf"]["adminkey"]: + return flask.render_template('login.html') + flask.session["admin"] = True + return flask.redirect(flask.url_for('admin')) + + +@app.route('/admin') +def admin(): + if "admin" in flask.session : + return "管理页" + return "未登录" + + if __name__ == '__main__': app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"]) \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..9710968 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,65 @@ + + + + + + + 登录页面 + + + + +
+

登录

+
+ + +
+
+ + + + + \ No newline at end of file diff --git a/templates/status.html b/templates/status.html new file mode 100644 index 0000000..141ea68 --- /dev/null +++ b/templates/status.html @@ -0,0 +1,73 @@ + + + + +网站后台展示页 + + + +
+

网站后台展示页

+ + + + + + + + + + + + + + + + + + + + + + + + + +
项目状态
网站访问速度正常
数据库连接正常
服务器负载较高
在线用户数正常
+
+ + + + From bfef66d29332bdd136a0145ff7bf2c13c569b15f Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Mon, 11 Dec 2023 10:55:44 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=A1=B5=E9=9D=A2-=E5=88=97=E5=87=BA?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E4=B8=8E=E5=88=A0=E9=99=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.py | 57 +++++++++++++- main.py | 27 ++++++- templates/keylist.html | 68 ++++++++++++++++ templates/login.html | 1 - templates/status.html | 173 +++++++++++++++++++++++++---------------- 5 files changed, 254 insertions(+), 72 deletions(-) create mode 100644 templates/keylist.html diff --git a/db.py b/db.py index 852baf0..1351cf6 100644 --- a/db.py +++ b/db.py @@ -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 \ No newline at end of file + 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 + diff --git a/main.py b/main.py index 156b0e3..0215fd5 100644 --- a/main.py +++ b/main.py @@ -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 "成功 返回上一页并刷新" + return "失败 返回上一页并刷新" + return "拒绝访问" + + if __name__ == '__main__': app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"]) \ No newline at end of file diff --git a/templates/keylist.html b/templates/keylist.html new file mode 100644 index 0000000..5e1a721 --- /dev/null +++ b/templates/keylist.html @@ -0,0 +1,68 @@ + + + + + + 列出密钥 + + + + +
+

列出密钥

+ + + + + + + + + + {% for item in data %} + + + + + + {% endfor %} + +
Key剩余Tokens操作
{{item[0]}}{{item[1]}}删除
+
+
+ + + + + \ No newline at end of file diff --git a/templates/login.html b/templates/login.html index 9710968..4f90bc7 100644 --- a/templates/login.html +++ b/templates/login.html @@ -56,7 +56,6 @@ - diff --git a/templates/status.html b/templates/status.html index 141ea68..fe634af 100644 --- a/templates/status.html +++ b/templates/status.html @@ -1,73 +1,108 @@ - - -网站后台展示页 - - - -
-

网站后台展示页

- - - - - - - - - - - - - - - - - - - - - - - - - -
项目状态
网站访问速度正常
数据库连接正常
服务器负载较高
在线用户数正常
-
- + + + 网站后台展示页 + + + + +
+

状态

+ + + + + + + + + + + + + + + {% if status["db"] == True %} + + {% else %} + + {% endif %} + + + + + + +
项目状态
开发者心态正常
数据库连接正常连接异常
开发者大脑负载较高
+
+
+
+

管理

+ + + + + + + + + + + + + + + + + + + + +
项目
列出所有Key
查询密钥
创建密钥
阿巴阿巴
+
+ + - + + \ No newline at end of file From e42a2042a9409ec519a729d619cd1ceb0cd456e7 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Mon, 11 Dec 2023 13:37:04 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=AF=86=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 查询密钥 - 创建密钥 + 创建密钥 阿巴阿巴 From f16d34e5c32e56c13d1e28f0c3ac4f54edb7e416 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Wed, 13 Dec 2023 08:36:07 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=AF=86=E9=92=A5=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.py | 12 +++++----- main.py | 9 ++++++++ templates/lookupKey.html | 48 ++++++++++++++++++++++++++++++++++++++++ templates/status.html | 2 +- 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 templates/lookupKey.html diff --git a/db.py b/db.py index 8b033ba..3647fd4 100644 --- a/db.py +++ b/db.py @@ -12,7 +12,7 @@ def dbIsOK(): except: return False -def userSurplus(userkey): +def userSurplus(userkey): #查询userkey剩余配额 #打开数据库连接 db = pymysql.connect(host=config.readConf()["db"]["host"], port=config.readConf()["db"]["port"], @@ -45,7 +45,7 @@ def reduce_value(userkey, value): # 减去对应的值 cursor = db.cursor() # 执行 SQL 查询以获取当前值 - cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = '{userkey}';") + cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey]) current_value = cursor.fetchone()[0] # 如果没有找到用户,则返回错误信息 @@ -57,7 +57,7 @@ def reduce_value(userkey, value): # 减去对应的值 new_value = current_value - value # 更新数据库中的值 - cursor.execute(f"UPDATE usersurplus SET surplus={new_value} WHERE userkey='{userkey}'") + cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey= %s;",[new_value,userkey]) # 提交事务 db.commit() @@ -100,7 +100,7 @@ def delKey(userkey): cursor = db.cursor() # 使用 execute() 方法执行 SQL 查询 - cursor.execute(f"DELETE FROM usersurplus WHERE userkey = '{userkey}';") + cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey]) # 提交事务 db.commit() @@ -128,9 +128,9 @@ def createKey(quota,number=1,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)});") + cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota]) else: - cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES ('{key}',{int(quota)});") + cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota]) output.append(key) # 提交事务 diff --git a/main.py b/main.py index ada298a..aeb038e 100644 --- a/main.py +++ b/main.py @@ -96,6 +96,15 @@ def createkey(): return flask.render_template("createKey.html",resq=resq) return "未登录 " +@app.route('/admin/lookupkey', methods=['POST','GET']) +def lookupkey(): + if "admin" in flask.session : + if flask.request.method == "GET": + return flask.render_template("lookupKey.html",resq="null") + resq = db.userSurplus(flask.request.form["key"]) + return flask.render_template("lookupKey.html",resq=resq) + return "未登录 " + @app.route('/admin/operate', methods=['POST','GET']) def operate(): if "admin" in flask.session : diff --git a/templates/lookupKey.html b/templates/lookupKey.html new file mode 100644 index 0000000..20504e3 --- /dev/null +++ b/templates/lookupKey.html @@ -0,0 +1,48 @@ + + + + + + 查询密钥 + + + + +
+

查询密钥

+
+ UserKey + + +
+
+ {% if resq != "null" %} +
+
+

执行结果

+ {% if resq == -99999 %} +

未找到UserKey

+ {% else %} +

配额剩余 {{ resq }}

+ {% endif %} +
+ {% endif %} + + + + \ No newline at end of file diff --git a/templates/status.html b/templates/status.html index 616bbc3..278f627 100644 --- a/templates/status.html +++ b/templates/status.html @@ -92,7 +92,7 @@ 列出所有Key - 查询密钥 + 查询密钥 创建密钥