mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-05-06 18:29:24 +08:00
Compare commits
4 Commits
31aa97deca
...
dfc0071d1f
Author | SHA1 | Date | |
---|---|---|---|
dfc0071d1f | |||
dc374e5259 | |||
afdbdec803 | |||
7b66cbb7e4 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
/__pycache__
|
||||
*.json
|
||||
APPData.db
|
||||
|
16
config.py
16
config.py
@ -2,4 +2,18 @@ import json
|
||||
|
||||
def readConf():
|
||||
with open('config.json') as f:
|
||||
return json.load(f)
|
||||
return json.load(f)
|
||||
|
||||
def updateConf(data_dict):
|
||||
# 打开JSON文件并读取内容
|
||||
file_path = 'config.json'
|
||||
with open(file_path, 'r') as json_file:
|
||||
existing_data = json.load(json_file)
|
||||
|
||||
# 将新的数据合并到现有的数据中
|
||||
existing_data.update(data_dict)
|
||||
|
||||
# 再次打开文件(这次是以写模式),并将更新后的数据保存回文件
|
||||
with open(file_path, 'w') as json_file:
|
||||
json.dump(existing_data, json_file, indent=4, ensure_ascii=False)
|
||||
|
||||
|
83
db.py
83
db.py
@ -1,29 +1,52 @@
|
||||
import pymysql , config , uuid
|
||||
import config , uuid , pathlib
|
||||
|
||||
def getconn():
|
||||
try:
|
||||
import sqlite3
|
||||
conn = sqlite3.connect(config.readConf()["db"]["path"])
|
||||
return conn
|
||||
except:
|
||||
print("DB ERROR")
|
||||
return 0
|
||||
|
||||
def dbIsOK():
|
||||
#打开数据库连接
|
||||
path = pathlib.Path(config.readConf()["db"]["path"])
|
||||
if not path.is_file():
|
||||
init()
|
||||
|
||||
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"])
|
||||
getconn()
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def init():
|
||||
#打开数据库连接
|
||||
db = getconn()
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
#建表
|
||||
cursor.execute(
|
||||
'''
|
||||
CREATE TABLE usersurplus (
|
||||
userkey TEXT,
|
||||
surplus INT);
|
||||
''')
|
||||
# 提交事务
|
||||
db.commit()
|
||||
|
||||
# 关闭连接
|
||||
db.close()
|
||||
|
||||
def userSurplus(userkey): #查询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"])
|
||||
db = getconn()
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 使用 execute() 方法执行 SQL 查询
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
|
||||
# 使用 fetchone() 方法获取单条数据.
|
||||
data = cursor.fetchone()
|
||||
|
||||
@ -36,16 +59,12 @@ def userSurplus(userkey): #查询userkey剩余配额
|
||||
|
||||
def reduce_value(userkey, value): # 减去对应的值
|
||||
#打开数据库连接
|
||||
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"])
|
||||
db = getconn()
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 执行 SQL 查询以获取当前值
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
|
||||
current_value = cursor.fetchone()[0]
|
||||
|
||||
# 如果没有找到用户,则返回错误信息
|
||||
@ -57,7 +76,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= ? WHERE userkey= ?;",[new_value,userkey])
|
||||
|
||||
# 提交事务
|
||||
db.commit()
|
||||
@ -70,11 +89,7 @@ def reduce_value(userkey, value): # 减去对应的值
|
||||
|
||||
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"])
|
||||
db = getconn()
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
@ -91,16 +106,12 @@ def getAllKey():
|
||||
|
||||
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"])
|
||||
db = getconn()
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 使用 execute() 方法执行 SQL 查询
|
||||
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey])
|
||||
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = ?;", [userkey])
|
||||
|
||||
# 提交事务
|
||||
db.commit()
|
||||
@ -114,25 +125,23 @@ def delKey(userkey):
|
||||
|
||||
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"])
|
||||
db = getconn()
|
||||
# 使用 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])
|
||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
|
||||
else:
|
||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
|
||||
output.append(key)
|
||||
|
||||
|
||||
# 提交事务
|
||||
db.commit()
|
||||
|
||||
|
10
main.py
10
main.py
@ -61,7 +61,7 @@ def login():
|
||||
if flask.request.method == 'GET':
|
||||
return flask.render_template('login.html')
|
||||
userRequest = flask.request.form
|
||||
if userRequest["password"] != config.readConf()["appconf"]["adminkey"]:
|
||||
if userRequest["password"] != config.readConf()["adminkey"]:
|
||||
return flask.render_template('login.html')
|
||||
flask.session["admin"] = True
|
||||
return flask.redirect(flask.url_for('admin'))
|
||||
@ -73,7 +73,7 @@ def admin():
|
||||
status = {}
|
||||
status["db"] = db.dbIsOK()
|
||||
return flask.render_template("status.html" ,status=status)
|
||||
return "未登录"
|
||||
return flask.redirect('login')
|
||||
|
||||
|
||||
@app.route('/admin/list')
|
||||
@ -81,7 +81,7 @@ def adminList():
|
||||
if "admin" in flask.session :
|
||||
data = db.getAllKey()
|
||||
return flask.render_template("keylist.html",data=data)
|
||||
return "未登录 "
|
||||
return flask.redirect('login')
|
||||
|
||||
@app.route('/admin/createkey', methods=['POST','GET'])
|
||||
def createkey():
|
||||
@ -94,7 +94,7 @@ def createkey():
|
||||
resq = db.createKey(flask.request.form["quota"],1,flask.request.form["key"])
|
||||
|
||||
return flask.render_template("createKey.html",resq=resq)
|
||||
return "未登录 "
|
||||
return flask.redirect('login')
|
||||
|
||||
@app.route('/admin/lookupkey', methods=['POST','GET'])
|
||||
def lookupkey():
|
||||
@ -103,7 +103,7 @@ def lookupkey():
|
||||
return flask.render_template("lookupKey.html",resq="null")
|
||||
resq = db.userSurplus(flask.request.form["key"])
|
||||
return flask.render_template("lookupKey.html",resq=resq)
|
||||
return "未登录 "
|
||||
return flask.redirect('login')
|
||||
|
||||
@app.route('/admin/operate', methods=['POST','GET'])
|
||||
def operate():
|
||||
|
@ -57,7 +57,6 @@
|
||||
</form>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user