Compare commits

..

No commits in common. "e42a2042a9409ec519a729d619cd1ceb0cd456e7" and "3157ccd3a91b852331bbdec2efe5341f57399cb8" have entirely different histories.

6 changed files with 70 additions and 353 deletions

88
db.py
View File

@ -1,16 +1,5 @@
import pymysql , config , uuid import pymysql , config
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):
#打开数据库连接 #打开数据库连接
@ -66,77 +55,4 @@ def reduce_value(userkey, value): # 减去对应的值
db.close() db.close()
# 返回新值 # 返回新值
return 0 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 = '{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 ('{key}',{int(quota)});")
else:
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES ('{key}',{int(quota)});")
output.append(key)
# 提交事务
db.commit()
db.close()
return output

34
main.py
View File

@ -70,41 +70,9 @@ def login():
@app.route('/admin') @app.route('/admin')
def admin(): def admin():
if "admin" in flask.session : if "admin" in flask.session :
status = {} return "管理页"
status["db"] = db.dbIsOK()
return flask.render_template("status.html" ,status=status)
return "未登录" 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/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 "成功 <a href='javascript:;' onclick='self.location=document.referrer;'>返回上一页并刷新</a>"
return "失败 <a href='javascript:;' onclick='self.location=document.referrer;'>返回上一页并刷新</a>"
return "拒绝访问"
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"]) app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"])

View File

@ -1,61 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>创建密钥</title>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background-color: #f7f7f7;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="container">
<h2>创建单个密钥</h2>
<form method="post">
<span>密钥</span>
<input name="key" required>
<span>配额</span>
<input name="quota" required>
<button type="submit">创建</button>
</form>
</div>
<hr>
<div class="container">
<h2>批量创建密钥</h2>
<form method="post">
<input type="radio" id="type" name="type" value="uuid" checked="true">
<label for="type">使用UUID</label><br>
<span>个数</span>
<input name="number" required>
<span>配额</span>
<input name="quota" required>
<button type="submit">批量创建</button>
</form>
</div>
{% if resq != "null" %}
<hr>
<div class="container">
<h2>执行结果</h2>
{% for i in resq %}
<h4>{{ i }}</h4>
{% endfor %}
</div>
{% endif %}
<script>
</script>
</body>
</html>

View File

@ -1,68 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>列出密钥</title>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background-color: #f7f7f7;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<h2>列出密钥</h2>
<table>
<thead>
<tr>
<th>Key</th>
<th>剩余Tokens</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td><a href="/admin/operate?type=del&target={{item[0]}}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<hr/>
<script>
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
</script>
</body>
</html>

View File

@ -56,6 +56,7 @@
<button type="submit" class="login-button">登录</button> <button type="submit" class="login-button">登录</button>
</form> </form>
</div> </div>
<script> <script>
</script> </script>

View File

@ -1,112 +1,73 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh-CN"> <html lang="zh-CN">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>后台管理</title> <title>网站后台展示页</title>
<style> <style>
body { body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
background-color: #f7f7f7; background-color: #f7f7f7;
padding: 20px; padding: 20px;
} }
.container {
.container { background-color: #fff;
background-color: #fff; padding: 20px;
padding: 20px; border-radius: 8px;
border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); }
} table {
width: 100%;
table { border-collapse: collapse;
width: 100%; }
border-collapse: collapse; th, td {
} border: 1px solid #ddd;
padding: 8px;
th, text-align: left;
td { }
border: 1px solid #ddd; th {
padding: 8px; background-color: #f2f2f2;
text-align: left; }
} .green {
color: green;
th { }
background-color: #f2f2f2; .red {
} color: red;
}
.green { </style>
color: green;
}
.red {
color: red;
}
.nodecoration{
text-decoration: none;
}
</style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h2>状态</h2> <h2>网站后台展示页</h2>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>项目</th> <th>项目</th>
<th>状态</th> <th>状态</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>开发者心态</td> <td>网站访问速度</td>
<td class="green">正常</td> <td class="green">正常</td>
</tr> </tr>
<tr> <tr>
<td>数据库连接</td> <td>数据库连接</td>
{% if status["db"] == True %} <td class="green">正常</td>
<td class="green">正常</td> </tr>
{% else %} <tr>
<td class="red">连接异常</td> <td>服务器负载</td>
{% endif %} <td class="red">较高</td>
</tr> </tr>
<tr> <tr>
<td>开发者大脑负载</td> <td>在线用户数</td>
<td class="red">较高</td> <td class="green">正常</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
<hr/>
<div class="container">
<h2>管理</h2>
<table>
<thead>
<tr>
<th>项目</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/admin/list" class="nodecoration">列出所有Key</a></td>
</tr>
<tr>
<td>查询密钥</td>
</tr>
<tr>
<td><a href="/admin/createkey" class="nodecoration">创建密钥</a></td>
</tr>
<tr>
<td>阿巴阿巴</td>
</tr>
</tbody>
</table>
</div>
<script> <script>
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
</script> </script>
</body> </body>
</html>
</html>