diff --git a/db.py b/db.py index f293752..0383951 100644 --- a/db.py +++ b/db.py @@ -1,7 +1,18 @@ -import pymysql , config +import pymysql , config , uuid +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): +def userSurplus(userkey): #查询userkey剩余配额 #打开数据库连接 db = pymysql.connect(host=config.readConf()["db"]["host"], port=config.readConf()["db"]["port"], @@ -46,7 +57,7 @@ def reduce_value(userkey, value): # 减去对应的值 new_value = current_value - value # 更新数据库中的值 - cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey=%s",[new_value,userkey]) + cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey= %s;",[new_value,userkey]) # 提交事务 db.commit() @@ -55,4 +66,77 @@ 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 = %s;", [userkey]) + + # 提交事务 + db.commit() + + if cursor.rowcount > 0: + db.close() # 使用 rowcount() 方法查询受影响行数 + return True + 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 (%s, %s);", [key, quota]) + else: + cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota]) + output.append(key) + + # 提交事务 + db.commit() + + db.close() + + return output + diff --git a/main.py b/main.py index a626ca9..aeb038e 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,70 @@ 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 : + 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', methods=['POST','GET']) +def createkey(): + if "admin" in flask.session : + 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/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 : + 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/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/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 new file mode 100644 index 0000000..4f90bc7 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,64 @@ + + + + + + + 登录页面 + + + + +
+

登录

+
+ + +
+
+ + + + \ No newline at end of file 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 new file mode 100644 index 0000000..278f627 --- /dev/null +++ b/templates/status.html @@ -0,0 +1,112 @@ + + + + + + 后台管理 + + + + +
+

状态

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

管理

+ + + + + + + + + + + + + + + + + + + + +
项目
列出所有Key
查询密钥
创建密钥
阿巴阿巴
+
+ + + + + \ No newline at end of file