mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-06-28 15:58:04 +08:00
Compare commits
No commits in common. "dfc0071d1fd6f202a720a9124b86659a3cdd3977" and "31aa97decae6b31e5a9ccad30cb5fbb7169d4ce3" have entirely different histories.
dfc0071d1f
...
31aa97deca
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
|||||||
/__pycache__
|
/__pycache__
|
||||||
*.json
|
*.json
|
||||||
APPData.db
|
|
||||||
|
16
config.py
16
config.py
@ -2,18 +2,4 @@ import json
|
|||||||
|
|
||||||
def readConf():
|
def readConf():
|
||||||
with open('config.json') as f:
|
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,52 +1,29 @@
|
|||||||
import config , uuid , pathlib
|
import pymysql , config , uuid
|
||||||
|
|
||||||
def getconn():
|
|
||||||
try:
|
|
||||||
import sqlite3
|
|
||||||
conn = sqlite3.connect(config.readConf()["db"]["path"])
|
|
||||||
return conn
|
|
||||||
except:
|
|
||||||
print("DB ERROR")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def dbIsOK():
|
def dbIsOK():
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
path = pathlib.Path(config.readConf()["db"]["path"])
|
|
||||||
if not path.is_file():
|
|
||||||
init()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
getconn()
|
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
|
return True
|
||||||
except:
|
except:
|
||||||
return False
|
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剩余配额
|
def userSurplus(userkey): #查询userkey剩余配额
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
db = getconn()
|
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() 方法创建一个游标对象 cursor
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 使用 execute() 方法执行 SQL 查询
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
|
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
|
||||||
# 使用 fetchone() 方法获取单条数据.
|
# 使用 fetchone() 方法获取单条数据.
|
||||||
data = cursor.fetchone()
|
data = cursor.fetchone()
|
||||||
|
|
||||||
@ -59,12 +36,16 @@ def userSurplus(userkey): #查询userkey剩余配额
|
|||||||
|
|
||||||
def reduce_value(userkey, value): # 减去对应的值
|
def reduce_value(userkey, value): # 减去对应的值
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
db = getconn()
|
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() 方法创建一个游标对象 cursor
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 执行 SQL 查询以获取当前值
|
# 执行 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]
|
current_value = cursor.fetchone()[0]
|
||||||
|
|
||||||
# 如果没有找到用户,则返回错误信息
|
# 如果没有找到用户,则返回错误信息
|
||||||
@ -76,7 +57,7 @@ def reduce_value(userkey, value): # 减去对应的值
|
|||||||
new_value = current_value - value
|
new_value = current_value - value
|
||||||
|
|
||||||
# 更新数据库中的值
|
# 更新数据库中的值
|
||||||
cursor.execute(f"UPDATE usersurplus SET surplus= ? WHERE userkey= ?;",[new_value,userkey])
|
cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey= %s;",[new_value,userkey])
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -89,7 +70,11 @@ def reduce_value(userkey, value): # 减去对应的值
|
|||||||
|
|
||||||
def getAllKey():
|
def getAllKey():
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
db = getconn()
|
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() 方法创建一个游标对象 cursor
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
@ -106,12 +91,16 @@ def getAllKey():
|
|||||||
|
|
||||||
def delKey(userkey):
|
def delKey(userkey):
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
db = getconn()
|
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() 方法创建一个游标对象 cursor
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 使用 execute() 方法执行 SQL 查询
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = ?;", [userkey])
|
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey])
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -125,23 +114,25 @@ def delKey(userkey):
|
|||||||
|
|
||||||
def createKey(quota,number=1,key="null"):
|
def createKey(quota,number=1,key="null"):
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
db = getconn()
|
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() 方法创建一个游标对象 cursor
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 使用 execute() 方法执行 SQL 查询
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
if key == "null":
|
if key == "null":
|
||||||
for i in range(int(number)):
|
for i in range(int(number)):
|
||||||
key = str(uuid.uuid1())
|
key = str(uuid.uuid1())
|
||||||
output.append(key)
|
output.append(key)
|
||||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
|
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
||||||
else:
|
else:
|
||||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
|
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
||||||
output.append(key)
|
output.append(key)
|
||||||
|
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
10
main.py
10
main.py
@ -61,7 +61,7 @@ def login():
|
|||||||
if flask.request.method == 'GET':
|
if flask.request.method == 'GET':
|
||||||
return flask.render_template('login.html')
|
return flask.render_template('login.html')
|
||||||
userRequest = flask.request.form
|
userRequest = flask.request.form
|
||||||
if userRequest["password"] != config.readConf()["adminkey"]:
|
if userRequest["password"] != config.readConf()["appconf"]["adminkey"]:
|
||||||
return flask.render_template('login.html')
|
return flask.render_template('login.html')
|
||||||
flask.session["admin"] = True
|
flask.session["admin"] = True
|
||||||
return flask.redirect(flask.url_for('admin'))
|
return flask.redirect(flask.url_for('admin'))
|
||||||
@ -73,7 +73,7 @@ def admin():
|
|||||||
status = {}
|
status = {}
|
||||||
status["db"] = db.dbIsOK()
|
status["db"] = db.dbIsOK()
|
||||||
return flask.render_template("status.html" ,status=status)
|
return flask.render_template("status.html" ,status=status)
|
||||||
return flask.redirect('login')
|
return "未登录"
|
||||||
|
|
||||||
|
|
||||||
@app.route('/admin/list')
|
@app.route('/admin/list')
|
||||||
@ -81,7 +81,7 @@ def adminList():
|
|||||||
if "admin" in flask.session :
|
if "admin" in flask.session :
|
||||||
data = db.getAllKey()
|
data = db.getAllKey()
|
||||||
return flask.render_template("keylist.html",data=data)
|
return flask.render_template("keylist.html",data=data)
|
||||||
return flask.redirect('login')
|
return "未登录 "
|
||||||
|
|
||||||
@app.route('/admin/createkey', methods=['POST','GET'])
|
@app.route('/admin/createkey', methods=['POST','GET'])
|
||||||
def createkey():
|
def createkey():
|
||||||
@ -94,7 +94,7 @@ def createkey():
|
|||||||
resq = db.createKey(flask.request.form["quota"],1,flask.request.form["key"])
|
resq = db.createKey(flask.request.form["quota"],1,flask.request.form["key"])
|
||||||
|
|
||||||
return flask.render_template("createKey.html",resq=resq)
|
return flask.render_template("createKey.html",resq=resq)
|
||||||
return flask.redirect('login')
|
return "未登录 "
|
||||||
|
|
||||||
@app.route('/admin/lookupkey', methods=['POST','GET'])
|
@app.route('/admin/lookupkey', methods=['POST','GET'])
|
||||||
def lookupkey():
|
def lookupkey():
|
||||||
@ -103,7 +103,7 @@ def lookupkey():
|
|||||||
return flask.render_template("lookupKey.html",resq="null")
|
return flask.render_template("lookupKey.html",resq="null")
|
||||||
resq = db.userSurplus(flask.request.form["key"])
|
resq = db.userSurplus(flask.request.form["key"])
|
||||||
return flask.render_template("lookupKey.html",resq=resq)
|
return flask.render_template("lookupKey.html",resq=resq)
|
||||||
return flask.redirect('login')
|
return "未登录 "
|
||||||
|
|
||||||
@app.route('/admin/operate', methods=['POST','GET'])
|
@app.route('/admin/operate', methods=['POST','GET'])
|
||||||
def operate():
|
def operate():
|
||||||
|
@ -57,6 +57,7 @@
|
|||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user