diff --git a/.gitignore b/.gitignore index b7faf40..d2debf2 100644 --- a/.gitignore +++ b/.gitignore @@ -205,3 +205,5 @@ cython_debug/ marimo/_static/ marimo/_lsp/ __marimo__/ + +/data \ No newline at end of file diff --git a/README.md b/README.md index 7c63705..a3dd5ed 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # get_cao_Chart 艾草榜 +## 部署步骤 +1. 安装依赖 项目根目录执行`pip install -r requirements.txt` +2. 修改`config_.py`中的`KEY_ADMIN`(管理员密码)和`PORT`(监听端口) +3. 启动 `python main.py` \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..30b2cc8 --- /dev/null +++ b/app.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/', 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/', 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, 返回管理页面") + 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 \ No newline at end of file diff --git a/config_.py b/config_.py new file mode 100644 index 0000000..c77e247 --- /dev/null +++ b/config_.py @@ -0,0 +1,2 @@ +KEY_ADMIN = "adminkey" +PORT = 8080 \ No newline at end of file diff --git a/data_io.py b/data_io.py new file mode 100644 index 0000000..f4234cc --- /dev/null +++ b/data_io.py @@ -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 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..f802efa --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +from package import * +import app + +from config_ import PORT + +app.run("0.0.0.0", PORT) \ No newline at end of file diff --git a/package.py b/package.py new file mode 100644 index 0000000..e9455f0 --- /dev/null +++ b/package.py @@ -0,0 +1,5 @@ +import flask +import json +import sqlite3 +import os +from threading import local \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5d5cda6 Binary files /dev/null and b/requirements.txt differ diff --git a/templates/admin.html b/templates/admin.html new file mode 100644 index 0000000..397ebed --- /dev/null +++ b/templates/admin.html @@ -0,0 +1,310 @@ + + + + + + 管理员面板 - 用户管理 + + + +
+
+

管理员面板

+

用户管理系统

+ 重新设置key +
+ +
+
+

添加新用户

+
+
+ + +
+ +
+ + +
+ + + + +
+
+ +
+

用户列表

+ + + + + + + + + + + + {% for user in users %} + + + + + + + + + + {% endfor %} + +
ID姓名介绍计数操作
{{ user.id }}{{ user.name }}{{ user.description }} + +
+ +
+
+
+
+
+ +
+
+ +
+
+ +
+ + +
+
+
+
+
+
+ + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..1b105d5 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,158 @@ + + + + + + 用户列表 + + + +
+

用户列表

+
+ {% for user in users %} +
+
{{ user.id }}
+ +
+ 艾草次数: {{ user.count }} +
+ +
+ {% endfor %} +
+
+ + \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..bd96bed --- /dev/null +++ b/templates/login.html @@ -0,0 +1,4 @@ + \ No newline at end of file