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