fix(cookieManager): 修复新增页重置按钮无效与网址校验过严

1) 重置按钮无响应:4 个页面容器是 <div class="layui-form"> 而非 <form>,
   type="reset" 没有可重置的表单。改为 <form class="layui-form">...</form>
   (与标准用户管理页一致)。
2) 保存报错:lay-verify="required|url" 强制要求带协议头,
   输入 www.baidu.com 时前台校验直接拦截(提示“链接格式不正确”)、/save 未发出。
   改为自定义 siteurl 规则,允许省略协议;后端新增 _normalize_url 自动补 https://。
已 Playwright 验证:重置可清空全部字段;无协议网址保存成功且落库为 https:// 前缀。
This commit is contained in:
bwstudio
2026-09-09 22:34:23 +08:00
parent 63cb897f8c
commit 1193f7f56a
6 changed files with 78 additions and 16 deletions
+13 -2
View File
@@ -4,6 +4,7 @@ Cookie管理 - 登录网站视图
URL: /system/cookie-site
"""
import os
import re
from datetime import datetime
from flask import Blueprint, render_template, request
from flask_login import current_user
@@ -23,6 +24,16 @@ bp = Blueprint('cookie_site', __name__,
template_folder=os.path.join(dir_path, '..', 'templates'))
def _normalize_url(raw):
"""网址补齐协议头:用户常省略 http(s)://,此处自动补 https://。"""
if not raw or not str(raw).strip():
return raw
u = str(raw).strip()
if re.match(r'^[a-zA-Z][a-zA-Z0-9+.\-]*://', u):
return u
return 'https://' + u
def _parse_expiry(raw):
"""过期时间:空值转 None;非空字符串按常见格式解析为 datetime;解析失败返回 None。"""
if not raw or not str(raw).strip():
@@ -82,7 +93,7 @@ def save():
try:
obj = CookieSite(
name=name,
url=url,
url=_normalize_url(url),
cookie_value=cookie_value,
domain=str_escape(req_json.get('domain')),
path=str_escape(req_json.get('path')) or '/',
@@ -125,7 +136,7 @@ def update():
return fail_api(msg="名称/网址/Cookie值不得为空")
try:
obj.name = name
obj.url = url
obj.url = _normalize_url(url)
obj.cookie_value = cookie_value
obj.domain = str_escape(req_json.get('domain'))
obj.path = str_escape(req_json.get('path')) or '/'
+13 -2
View File
@@ -4,6 +4,7 @@ Cookie管理 - 登录用户视图
URL: /system/cookie-user
"""
import os
import re
from datetime import datetime
from flask import Blueprint, render_template, request
from flask_login import current_user
@@ -23,6 +24,16 @@ bp = Blueprint('cookie_user', __name__,
template_folder=os.path.join(dir_path, '..', 'templates'))
def _normalize_url(raw):
"""网址补齐协议头:用户常省略 http(s)://,此处自动补 https://。"""
if not raw or not str(raw).strip():
return raw
u = str(raw).strip()
if re.match(r'^[a-zA-Z][a-zA-Z0-9+.\-]*://', u):
return u
return 'https://' + u
def _parse_expiry(raw):
"""过期时间:空值转 None;非空字符串按常见格式解析为 datetime;解析失败返回 None。"""
if not raw or not str(raw).strip():
@@ -83,7 +94,7 @@ def save():
obj = CookieUser(
username=username,
nickname=str_escape(req_json.get('nickname')),
website_url=website_url,
website_url=_normalize_url(website_url),
website_name=str_escape(req_json.get('website_name')),
cookie_value=cookie_value,
domain=str_escape(req_json.get('domain')),
@@ -125,7 +136,7 @@ def update():
try:
obj.username = username
obj.nickname = str_escape(req_json.get('nickname'))
obj.website_url = website_url
obj.website_url = _normalize_url(website_url)
obj.website_name = str_escape(req_json.get('website_name'))
obj.cookie_value = cookie_value
obj.domain = str_escape(req_json.get('domain'))