diff --git a/applications/models/admin_cookie.py b/applications/models/admin_cookie.py index 95276a1..0e7ff23 100644 --- a/applications/models/admin_cookie.py +++ b/applications/models/admin_cookie.py @@ -4,7 +4,7 @@ from applications.extensions import db class CookieUser(db.Model): - """登录用户 - 保存已登录用户的Cookie信息""" + """登录用户 - 记录在各网站上的登录账号""" __tablename__ = 'admin_cookie_user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) @@ -12,8 +12,6 @@ class CookieUser(db.Model): nickname = db.Column(db.String(100), default='', comment='昵称') website_url = db.Column(db.String(255), nullable=False, comment='网站URL') website_name = db.Column(db.String(100), default='', comment='网站名称') - cookie_value = db.Column(db.Text, nullable=False, comment='Cookie值') - domain = db.Column(db.String(255), default='', comment='Cookie域名') expiry = db.Column(db.DateTime, comment='过期时间') enable = db.Column(db.Integer, default=1, comment='状态(1启用,0禁用)') create_by = db.Column(db.String(50), default='admin', comment='创建者') @@ -27,8 +25,6 @@ class CookieUser(db.Model): 'nickname': self.nickname, 'website_url': self.website_url, 'website_name': self.website_name, - 'cookie_value': self.cookie_value, - 'domain': self.domain, 'expiry': self.expiry.strftime('%Y-%m-%d %H:%M:%S') if self.expiry else '', 'enable': self.enable, 'create_by': self.create_by, @@ -42,6 +38,8 @@ class CookieSite(db.Model): __tablename__ = 'admin_cookie_site' id = db.Column(db.Integer, primary_key=True, autoincrement=True) + # 关联的登录用户(admin_cookie_user.id),用于把网站归入某个登录账号 + cookie_user_id = db.Column(db.Integer, nullable=True, comment='关联登录用户ID') name = db.Column(db.String(100), nullable=False, comment='网站名称') url = db.Column(db.String(255), nullable=False, comment='网站URL') cookie_value = db.Column(db.Text, nullable=False, comment='Cookie值') @@ -58,6 +56,7 @@ class CookieSite(db.Model): def to_dict(self): return { 'id': self.id, + 'cookie_user_id': self.cookie_user_id, 'name': self.name, 'url': self.url, 'cookie_value': self.cookie_value, diff --git a/applications/schemas/admin_cookie.py b/applications/schemas/admin_cookie.py index f4237f9..3f82940 100644 --- a/applications/schemas/admin_cookie.py +++ b/applications/schemas/admin_cookie.py @@ -1,5 +1,6 @@ """Cookie管理Schema""" from flask_marshmallow.sqla import SQLAlchemyAutoSchema +from marshmallow import fields from applications.extensions import ma from applications.models import CookieUser, CookieSite @@ -15,7 +16,13 @@ class CookieUserManageSchema(SQLAlchemyAutoSchema): class CookieSiteManageSchema(SQLAlchemyAutoSchema): """登录网站管理用:序列化为表格数据 / 表单回填""" + # 关联登录用户的显示名(视图层预先挂到对象上的 _cookie_username) + cookie_username = fields.Method('get_cookie_username') + class Meta: model = CookieSite include_fk = True load_instance = True + + def get_cookie_username(self, obj): + return getattr(obj, '_cookie_username', '') or '' diff --git a/migrations/versions/e5f6a7b8c9d0_cookie_user_drop_cookie_fields.py b/migrations/versions/e5f6a7b8c9d0_cookie_user_drop_cookie_fields.py new file mode 100644 index 0000000..720401c --- /dev/null +++ b/migrations/versions/e5f6a7b8c9d0_cookie_user_drop_cookie_fields.py @@ -0,0 +1,71 @@ +"""登录用户去掉 Cookie 明细字段;登录网站增加关联登录用户 + +1. admin_cookie_user:删除 cookie_value、domain 两列 + 登录用户只登记「账号归属」(用户名/昵称/网站/有效期), + Cookie 明细由「登录网站」承载,故这两列不再需要。 + +2. admin_cookie_site:新增 cookie_user_id(关联 admin_cookie_user.id) + 用于把网站挂到某个登录用户下,列表/新增/编辑用下拉框选择。 + +SQLite 不支持直接 DROP COLUMN,统一用 batch_alter_table 重建表; +每一步都先判断列是否存在,便于重复执行与跨环境差异。 + +Revision ID: e5f6a7b8c9d0 +Revises: d4e5f6a7b8c9 +""" +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = 'e5f6a7b8c9d0' +down_revision = 'd4e5f6a7b8c9' +branch_labels = None +depends_on = None + + +def _columns(conn, table): + insp = sa.inspect(conn) + return {c['name'] for c in insp.get_columns(table)} + + +def upgrade(): + conn = op.get_bind() + + user_cols = _columns(conn, 'admin_cookie_user') + if 'cookie_value' in user_cols: + with op.batch_alter_table('admin_cookie_user', schema=None) as batch_op: + batch_op.drop_column('cookie_value') + if 'domain' in user_cols: + with op.batch_alter_table('admin_cookie_user', schema=None) as batch_op: + batch_op.drop_column('domain') + + site_cols = _columns(conn, 'admin_cookie_site') + if 'cookie_user_id' not in site_cols: + with op.batch_alter_table('admin_cookie_site', schema=None) as batch_op: + batch_op.add_column( + sa.Column('cookie_user_id', sa.Integer(), nullable=True, + comment='关联登录用户ID') + ) + + +def downgrade(): + conn = op.get_bind() + + site_cols = _columns(conn, 'admin_cookie_site') + if 'cookie_user_id' in site_cols: + with op.batch_alter_table('admin_cookie_site', schema=None) as batch_op: + batch_op.drop_column('cookie_user_id') + + user_cols = _columns(conn, 'admin_cookie_user') + if 'domain' not in user_cols: + with op.batch_alter_table('admin_cookie_user', schema=None) as batch_op: + batch_op.add_column( + sa.Column('domain', sa.String(length=255), nullable=True, + server_default='', comment='Cookie域名') + ) + if 'cookie_value' not in user_cols: + with op.batch_alter_table('admin_cookie_user', schema=None) as batch_op: + batch_op.add_column( + sa.Column('cookie_value', sa.Text(), nullable=True, + comment='Cookie值') + ) diff --git a/plugins/cookieManager/templates/system/cookie_site/add.html b/plugins/cookieManager/templates/system/cookie_site/add.html index e9cd611..ca0c3fe 100644 --- a/plugins/cookieManager/templates/system/cookie_site/add.html +++ b/plugins/cookieManager/templates/system/cookie_site/add.html @@ -7,6 +7,17 @@
+
+ +
+ +
+
@@ -107,6 +118,7 @@ layui.use(['form', 'layer', 'laydate'], function(){ type: 'POST', contentType: 'application/json', data: JSON.stringify({ + cookie_user_id: data.cookie_user_id, name: data.name, url: data.url, cookie_value: data.cookie_value, diff --git a/plugins/cookieManager/templates/system/cookie_site/edit.html b/plugins/cookieManager/templates/system/cookie_site/edit.html index 083d98a..a087fdc 100644 --- a/plugins/cookieManager/templates/system/cookie_site/edit.html +++ b/plugins/cookieManager/templates/system/cookie_site/edit.html @@ -8,6 +8,17 @@ +
+ +
+ +
+
@@ -110,6 +121,7 @@ layui.use(['form', 'layer', 'laydate'], function(){ contentType: 'application/json', data: JSON.stringify({ cookieSiteId: data.cookieSiteId, + cookie_user_id: data.cookie_user_id, name: data.name, url: data.url, cookie_value: data.cookie_value, diff --git a/plugins/cookieManager/templates/system/cookie_site/main.html b/plugins/cookieManager/templates/system/cookie_site/main.html index dcf50ea..2a77d86 100644 --- a/plugins/cookieManager/templates/system/cookie_site/main.html +++ b/plugins/cookieManager/templates/system/cookie_site/main.html @@ -10,6 +10,15 @@
+ +
+ +
@@ -97,6 +106,9 @@ let cols = [ [ {title: '编号', field: 'id', align: 'center', width: 80}, + {title: '关联用户', field: 'cookie_username', align: 'center', templet: function (d) { + return d.cookie_username ? d.cookie_username : '未关联'; + }}, {title: '网站名称', field: 'name', align: 'center'}, {title: '网站URL', field: 'url', align: 'center', minWidth: 200}, {title: 'Cookie值', field: 'cookie_value', align: 'center', minWidth: 200, templet: '#col-cookie-site-value'}, diff --git a/plugins/cookieManager/templates/system/cookie_user/add.html b/plugins/cookieManager/templates/system/cookie_user/add.html index 1f96925..be867f8 100644 --- a/plugins/cookieManager/templates/system/cookie_user/add.html +++ b/plugins/cookieManager/templates/system/cookie_user/add.html @@ -35,20 +35,6 @@ autocomplete="off" class="layui-input">
-
- -
- -
-
-
- -
- -
-
@@ -105,8 +91,6 @@ layui.use(['form', 'layer', 'laydate'], function(){ nickname: data.nickname, website_name: data.website_name, website_url: data.website_url, - cookie_value: data.cookie_value, - domain: data.domain, expiry: data.expiry, enable: data.enable }), diff --git a/plugins/cookieManager/templates/system/cookie_user/edit.html b/plugins/cookieManager/templates/system/cookie_user/edit.html index 02feed3..02533c7 100644 --- a/plugins/cookieManager/templates/system/cookie_user/edit.html +++ b/plugins/cookieManager/templates/system/cookie_user/edit.html @@ -36,20 +36,6 @@ autocomplete="off" class="layui-input">
-
- -
- -
-
-
- -
- -
-
@@ -107,8 +93,6 @@ layui.use(['form', 'layer', 'laydate'], function(){ nickname: data.nickname, website_name: data.website_name, website_url: data.website_url, - cookie_value: data.cookie_value, - domain: data.domain, expiry: data.expiry, enable: data.enable }), diff --git a/plugins/cookieManager/templates/system/cookie_user/main.html b/plugins/cookieManager/templates/system/cookie_user/main.html index 4298d42..01940b2 100644 --- a/plugins/cookieManager/templates/system/cookie_user/main.html +++ b/plugins/cookieManager/templates/system/cookie_user/main.html @@ -74,15 +74,6 @@ - - {% endraw %}