Compare commits

...

4 Commits

Author SHA1 Message Date
dfc0071d1f 更改跳转方式为重定向 2023-12-15 11:53:56 +08:00
dc374e5259 完善未登录跳转功能 2023-12-15 11:40:57 +08:00
afdbdec803 移除对MySQL服务器的支持 2023-12-15 11:20:47 +08:00
7b66cbb7e4 添加对SQLite3数据库的支持 2023-12-14 10:56:16 +08:00
5 changed files with 67 additions and 44 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/__pycache__
*.json
APPData.db

View File

@ -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
View File

@ -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
View File

@ -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():

View File

@ -57,7 +57,6 @@
</form>
</div>
<script>
</script>
</body>