Compare commits

..

No commits in common. "dfc0071d1fd6f202a720a9124b86659a3cdd3977" and "31aa97decae6b31e5a9ccad30cb5fbb7169d4ce3" have entirely different histories.

5 changed files with 44 additions and 67 deletions

1
.gitignore vendored
View File

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

View File

@ -2,18 +2,4 @@ import json
def readConf():
with open('config.json') as 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)
return json.load(f)

83
db.py
View File

@ -1,52 +1,29 @@
import config , uuid , pathlib
def getconn():
try:
import sqlite3
conn = sqlite3.connect(config.readConf()["db"]["path"])
return conn
except:
print("DB ERROR")
return 0
import pymysql , config , uuid
def dbIsOK():
#打开数据库连接
path = pathlib.Path(config.readConf()["db"]["path"])
if not path.is_file():
init()
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
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 = 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 = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
@ -59,12 +36,16 @@ def userSurplus(userkey): #查询userkey剩余配额
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 = db.cursor()
# 执行 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]
# 如果没有找到用户,则返回错误信息
@ -76,7 +57,7 @@ def reduce_value(userkey, 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()
@ -89,7 +70,11 @@ def reduce_value(userkey, value): # 减去对应的值
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 = db.cursor()
@ -106,12 +91,16 @@ def getAllKey():
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 = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = ?;", [userkey])
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey])
# 提交事务
db.commit()
@ -125,23 +114,25 @@ def delKey(userkey):
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 = 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 (?, ?);", [key, quota])
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
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)
# 提交事务
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()["adminkey"]:
if userRequest["password"] != config.readConf()["appconf"]["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 flask.redirect('login')
return "未登录"
@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 flask.redirect('login')
return "未登录 "
@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 flask.redirect('login')
return "未登录 "
@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 flask.redirect('login')
return "未登录 "
@app.route('/admin/operate', methods=['POST','GET'])
def operate():

View File

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