mirror of
https://github.com/xzb23/get_cao_Chart.git
synced 2026-11-22 06:34:36 +08:00
第一次提交
This commit is contained in:
1 parent
08939e17b4
commit
f93cfb6128
11 files changed
+662
No files matched your search
@@ -205,3 +205,5 @@ cython_debug/
|
||||
marimo/_static/
|
||||
marimo/_lsp/
|
||||
__marimo__/
|
||||
|
||||
/data
|
||||
@@ -1,2 +1,6 @@
|
||||
# get_cao_Chart
|
||||
艾草榜
|
||||
## 部署步骤
|
||||
1. 安装依赖 项目根目录执行`pip install -r requirements.txt`
|
||||
2. 修改`config_.py`中的`KEY_ADMIN`(管理员密码)和`PORT`(监听端口)
|
||||
3. 启动 `python main.py`
|
||||
@@ -0,0 +1,74 @@
|
||||
from package import *
|
||||
import data_io
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.route('/api/getcao')
|
||||
def give_user():
|
||||
uid = flask.request.args.get('uid')
|
||||
count = data_io.get_user_count(uid)
|
||||
print(uid, count)
|
||||
if count == -1: # -1保留,当管理员手动设置count=-1时,用户无法被cao
|
||||
re_text = "err:\t未查询到用户"
|
||||
else:
|
||||
data_io.give_user(uid)
|
||||
re_text = "ok"
|
||||
return flask.redirect(flask.url_for('get_users_cao'))
|
||||
|
||||
@app.route('/')
|
||||
def get_users_cao():
|
||||
users = data_io.get_all_users()
|
||||
return flask.render_template('index.html', users=users)
|
||||
|
||||
|
||||
@app.route('/admin')
|
||||
def admin():
|
||||
password = flask.request.cookies.get("key")
|
||||
if not password:
|
||||
return flask.redirect(flask.url_for('login'))
|
||||
users = data_io.get_all_users()
|
||||
return flask.render_template('admin.html', users=users)
|
||||
|
||||
@app.route('/add_user', methods=['GET', 'POST'])
|
||||
def add_user():
|
||||
password = flask.request.cookies.get("key")
|
||||
uid = flask.request.form.get('name')
|
||||
description = flask.request.form.get('description')
|
||||
if data_io.add_user(uid, description, password):
|
||||
re_text = "ok"
|
||||
else:
|
||||
re_text = "err:\t用户已存在"
|
||||
return flask.redirect(flask.url_for('admin'))
|
||||
|
||||
@app.route('/delete_user/<uid>', methods=['GET', 'POST'])
|
||||
def delete_user(uid):
|
||||
password = flask.request.cookies.get("key")
|
||||
data_io.delete_user(uid, password)
|
||||
return flask.redirect(flask.url_for('admin'))
|
||||
|
||||
@app.route('/update_user/<uid>', methods=['GET', 'POST'])
|
||||
def update_user(uid):
|
||||
name_new = flask.request.form.get('name')
|
||||
count = flask.request.form.get('count')
|
||||
description = flask.request.form.get('description')
|
||||
password = flask.request.cookies.get("key")
|
||||
data_io.update_user(uid, name_new, count, description, password)
|
||||
return flask.redirect(flask.url_for('admin'))
|
||||
|
||||
@app.route('/login/check', methods=['GET', 'POST'])
|
||||
def check():
|
||||
password = flask.request.form.get('password')
|
||||
if password:
|
||||
resp = flask.make_response("ok, <a href='/admin'>返回管理页面</a>")
|
||||
resp.set_cookie("key", password, max_age=3600)
|
||||
return resp
|
||||
else:
|
||||
return "err", 401
|
||||
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
return flask.render_template('login.html')
|
||||
|
||||
def run(host:str, port:int):
|
||||
app.run(host, port, debug=False)
|
||||
return
|
||||
@@ -0,0 +1,2 @@
|
||||
KEY_ADMIN = "adminkey"
|
||||
PORT = 8080
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
from package import *
|
||||
import config_
|
||||
|
||||
class User:
|
||||
def __init__(self, id:int, name:str, count:int, description:str):
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.count = count
|
||||
|
||||
db_local = local()
|
||||
|
||||
def get_connection():
|
||||
if not hasattr(db_local, 'conn'):
|
||||
db_local.conn = sqlite3.connect('./data/user.db', check_same_thread=True)
|
||||
return db_local.conn
|
||||
|
||||
def close_connection():
|
||||
if hasattr(db_local, 'conn'):
|
||||
db_local.conn.close()
|
||||
del db_local.conn
|
||||
|
||||
if not os.path.exists("./data"):
|
||||
os.mkdir("./data")
|
||||
if not os.path.exists("./data/user.db"):
|
||||
user_db = sqlite3.connect("./data/user.db")
|
||||
db_cur = user_db.cursor()
|
||||
db_cur.execute('''CREATE TABLE USERDATA
|
||||
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
USERID TEXT NOT NULL,
|
||||
COUNT INT NOT NULL,
|
||||
DESCRIPTION TEXT NOT NULL
|
||||
);''')
|
||||
user_db.commit()
|
||||
db_cur.close()
|
||||
user_db.close()
|
||||
|
||||
def give_user(uid:str):
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT COUNT FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
res = db_cur.fetchone()
|
||||
if res is None:
|
||||
return -1
|
||||
db_cur.execute("UPDATE USERDATA SET COUNT = COUNT + 1 WHERE USERID = ?", (uid,))
|
||||
conn.commit()
|
||||
return 0
|
||||
|
||||
def get_user_count(uid:str) -> int:
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT COUNT FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
res = db_cur.fetchone()
|
||||
if res is None:
|
||||
return -1
|
||||
return res[0]
|
||||
|
||||
def get_all_users() -> list:
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT * FROM USERDATA")
|
||||
res = db_cur.fetchall()
|
||||
users = []
|
||||
for r in res:
|
||||
users.append(User(r[0], r[1], r[2], r[3]))
|
||||
return users
|
||||
|
||||
def add_user(uid:str, description:str, password:str) -> bool:
|
||||
if password != config_.KEY_ADMIN:
|
||||
return False
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("SELECT COUNT FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
res = db_cur.fetchone()
|
||||
if res is not None:
|
||||
return False
|
||||
db_cur.execute("INSERT INTO USERDATA (USERID, COUNT, DESCRIPTION) VALUES (?, ?, ?)", (uid, 0, description))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
def delete_user(uid:str, password:str):
|
||||
if password != config_.KEY_ADMIN:
|
||||
return False
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("DELETE FROM USERDATA WHERE USERID = ?", (uid,))
|
||||
conn.commit()
|
||||
return
|
||||
|
||||
def update_user(uid:str, name_new:str, count:int, description:str, password:str):
|
||||
if password != config_.KEY_ADMIN:
|
||||
return False
|
||||
conn = get_connection()
|
||||
db_cur = conn.cursor()
|
||||
db_cur.execute("UPDATE USERDATA SET USERID = ?, COUNT = ?, DESCRIPTION = ? WHERE USERID = ?", (name_new, count, description, uid))
|
||||
conn.commit()
|
||||
return
|
||||
@@ -0,0 +1,6 @@
|
||||
from package import *
|
||||
import app
|
||||
|
||||
from config_ import PORT
|
||||
|
||||
app.run("0.0.0.0", PORT)
|
||||
@@ -0,0 +1,5 @@
|
||||
import flask
|
||||
import json
|
||||
import sqlite3
|
||||
import os
|
||||
from threading import local
|
||||
Binary file not shown.
@@ -0,0 +1,310 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>管理员面板 - 用户管理</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
||||
padding: 20px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
header {
|
||||
text-align: center;
|
||||
margin-bottom: 30px;
|
||||
padding: 20px;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #2c3e50;
|
||||
font-size: 2.5rem;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #7f8c8d;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.panel {
|
||||
display: flex;
|
||||
gap: 30px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.form-section {
|
||||
flex: 1;
|
||||
background: white;
|
||||
padding: 25px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.users-section {
|
||||
flex: 2;
|
||||
background: white;
|
||||
padding: 25px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
h2 {
|
||||
color: #2c3e50;
|
||||
margin-bottom: 20px;
|
||||
padding-bottom: 10px;
|
||||
border-bottom: 2px solid #3498db;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
color: #2c3e50;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
input, select {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 1rem;
|
||||
transition: border-color 0.3s;
|
||||
}
|
||||
|
||||
input:focus, select:focus {
|
||||
outline: none;
|
||||
border-color: #3498db;
|
||||
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
|
||||
}
|
||||
|
||||
button {
|
||||
background: linear-gradient(135deg, #3498db, #2980b9);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 12px 20px;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
transition: all 0.3s ease;
|
||||
margin-right: 10px;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: linear-gradient(135deg, #2980b9, #3498db);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: linear-gradient(135deg, #e74c3c, #c0392b);
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background: linear-gradient(135deg, #c0392b, #e74c3c);
|
||||
}
|
||||
|
||||
.btn-success {
|
||||
background: linear-gradient(135deg, #2ecc71, #27ae60);
|
||||
}
|
||||
|
||||
.btn-success:hover {
|
||||
background: linear-gradient(135deg, #27ae60, #2ecc71);
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
th, td {
|
||||
padding: 15px;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #f8f9fa;
|
||||
color: #2c3e50;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
tr:hover {
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.edit-form {
|
||||
display: none;
|
||||
background: #f8f9fa;
|
||||
padding: 15px;
|
||||
border-radius: 6px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.edit-form.active {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.cancel-edit {
|
||||
background: linear-gradient(135deg, #95a5a6, #7f8c8d);
|
||||
}
|
||||
|
||||
.cancel-edit:hover {
|
||||
background: linear-gradient(135deg, #7f8c8d, #95a5a6);
|
||||
}
|
||||
|
||||
#backlogin {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.panel {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
table {
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
th, td {
|
||||
padding: 10px 5px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>管理员面板</h1>
|
||||
<p class="subtitle">用户管理系统</p>
|
||||
<a id="backlogin" type="submit" onclick="window.location.href='/login'">重新设置key</a>
|
||||
</header>
|
||||
|
||||
<div class="panel">
|
||||
<div class="form-section">
|
||||
<h2>添加新用户</h2>
|
||||
<form action="/add_user" method="post">
|
||||
<div class="form-group">
|
||||
<label for="name">昵称:</label>
|
||||
<input type="text" id="name" name="name" required>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="description">介绍:</label>
|
||||
<input type="text" id="description" name="description" required>
|
||||
</div>
|
||||
|
||||
<!-- <div class="form-group">
|
||||
<label for="role">角色:</label>
|
||||
<select id="role" name="role" required>
|
||||
<option value="普通用户">普通用户</option>
|
||||
</select>
|
||||
</div> -->
|
||||
|
||||
<button type="submit">添加用户</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="users-section">
|
||||
<h2>用户列表</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>姓名</th>
|
||||
<th>介绍</th>
|
||||
<th>计数</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for user in users %}
|
||||
<tr id="user-row-{{ user.id }}">
|
||||
<td>{{ user.id }}</td>
|
||||
<td id="name-{{ user.id }}">{{ user.name }}</td>
|
||||
<td id="description-{{ user.id }}">{{ user.description }}</td>
|
||||
<td class="actions">
|
||||
<button class="btn-success edit-btn" onclick="showEditForm({{ user.id }})">编辑</button>
|
||||
<form action="/delete_user/{{ user.name }}" method="post" style="display:inline;">
|
||||
<button type="submit" class="btn-danger" onclick="return confirm('确定要删除这个用户吗?')">删除</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="5">
|
||||
<div id="edit-form-{{ user.id }}" class="edit-form">
|
||||
<form action="/update_user/{{ user.name }}" method="post">
|
||||
<div class="form-group">
|
||||
<input type="text" name="name" value="{{ user.name }}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="text" name="description" value="{{ user.description }}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="text" name="count" value="{{ user.count }}" required>
|
||||
</div>
|
||||
<button type="submit">保存更改</button>
|
||||
<button type="button" class="cancel-edit" onclick="hideEditForm({{ user.id }})">取消</button>
|
||||
</form>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function showEditForm(userId) {
|
||||
// 隐藏所有编辑表单
|
||||
document.querySelectorAll('.edit-form').forEach(form => {
|
||||
form.classList.remove('active');
|
||||
});
|
||||
|
||||
// 显示当前用户的编辑表单
|
||||
document.getElementById(`edit-form-${userId}`).classList.add('active');
|
||||
}
|
||||
|
||||
function hideEditForm(userId) {
|
||||
document.getElementById(`edit-form-${userId}`).classList.remove('active');
|
||||
}
|
||||
|
||||
// 页面加载后隐藏所有编辑表单
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
document.querySelectorAll('.edit-form').forEach(form => {
|
||||
form.classList.remove('active');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,158 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>用户列表</title>
|
||||
<style>
|
||||
.action-btn {
|
||||
width: 100%;
|
||||
padding: 12px 20px;
|
||||
margin-top: 15px;
|
||||
background: linear-gradient(135deg, #3498db, #2980b9);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
|
||||
}
|
||||
|
||||
.action-btn:hover {
|
||||
background: linear-gradient(135deg, #2980b9, #3498db);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 6px 12px rgba(52, 152, 219, 0.4);
|
||||
}
|
||||
|
||||
.action-btn:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
color: #2c3e50;
|
||||
margin-bottom: 30px;
|
||||
font-size: 2.5rem;
|
||||
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.user-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.user-card {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
|
||||
padding: 25px;
|
||||
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.user-card:hover {
|
||||
transform: translateY(-5px);
|
||||
box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.user-card::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 5px;
|
||||
background: linear-gradient(90deg, #3498db, #8e44ad);
|
||||
}
|
||||
|
||||
.user-info {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.user-name {
|
||||
font-size: 1.5rem;
|
||||
color: #2c3e50;
|
||||
margin: 0 0 8px 0;
|
||||
}
|
||||
|
||||
.user-email {
|
||||
color: #7f8c8d;
|
||||
font-size: 1rem;
|
||||
margin: 0 0 15px 0;
|
||||
}
|
||||
|
||||
.highlight-data {
|
||||
background: linear-gradient(135deg, #3498db, #2c3e50);
|
||||
color: white;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
font-weight: bold;
|
||||
font-size: 1.1rem;
|
||||
text-align: center;
|
||||
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.user-id {
|
||||
position: absolute;
|
||||
top: 15px;
|
||||
right: 15px;
|
||||
background: #ecf0f1;
|
||||
color: #7f8c8d;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.user-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>用户列表</h1>
|
||||
<div class="user-grid">
|
||||
{% for user in users %}
|
||||
<div class="user-card">
|
||||
<div class="user-id">{{ user.id }}</div>
|
||||
<div class="user-info">
|
||||
<h2 class="user-name">{{ user.name }}</h2>
|
||||
<p class="user-description">{{ user.description }}</p>
|
||||
</div>
|
||||
<div class="highlight-data">
|
||||
艾草次数: {{ user.count }}
|
||||
</div>
|
||||
<button class="action-btn" data-user-id="{{ user.id }}" onclick="window.location.href='/api/getcao?uid={{ user.name }}'">香草</button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,4 @@
|
||||
<form action="/login/check" method="post" class="login-form">
|
||||
<input type="password" name="password" placeholder="password" required>
|
||||
<button type="submit">登录</button>
|
||||
</form>
|
||||
Reference in new issue
Block a user