From dcd53b2691bd9c4b4198b08592dbfedd15eb665e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Thu, 18 Jun 2026 11:52:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20web=20=E7=AB=AF=20GUI=20=E9=A3=8E?= =?UTF-8?q?=E6=A0=BC=E7=95=8C=E9=9D=A2=20+=20SVG=20=E6=94=AF=E6=8C=81=20+?= =?UTF-8?q?=20=E9=A2=84=E8=A7=88=E6=B8=85=E6=99=B0=E5=BA=A6=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - index.html: 完整复刻 GUI 三栏布局(模式/预览/设置) 7 种编码模式切换,实时防抖预览,下载 PNG/SVG/复制 - main.rs: API 新增 fmt=svg 参数,预览用 PNG size=8 避免拉伸模糊 - Dockerfile: 多阶段构建(alpine),部署就绪 Co-Authored-By: Claude --- web/src/main.rs | 20 ++- web/src/templates/index.html | 335 +++++++++++++++++++++++++---------- 2 files changed, 253 insertions(+), 102 deletions(-) diff --git a/web/src/main.rs b/web/src/main.rs index 6331355..57ad21b 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -19,6 +19,9 @@ struct QrParams { margin: u8, #[serde(default = "default_size")] size: u8, + /// fmt=svg 返回 SVG,否则返回 PNG + #[serde(default)] + fmt: String, } fn default_level() -> String { "M".into() } @@ -40,7 +43,7 @@ async fn index() -> Html<&'static str> { Html(include_str!("templates/index.html")) } -/// QR 码生成 API → PNG 图片 +/// QR 码生成 API → PNG 或 SVG async fn generate_qr(Query(params): Query) -> impl IntoResponse { let level = match parse_level(¶ms.level) { Ok(l) => l, @@ -58,12 +61,15 @@ async fn generate_qr(Query(params): Query) -> impl IntoResponse { Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(), }; - let png = match qr.to_png_bytes(params.size) { - Ok(b) => b, - Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), - }; - - ([(header::CONTENT_TYPE, "image/png")], png).into_response() + if params.fmt == "svg" { + let svg = qr.to_svg(); + ([(header::CONTENT_TYPE, "image/svg+xml")], svg).into_response() + } else { + match qr.to_png_bytes(params.size) { + Ok(b) => ([(header::CONTENT_TYPE, "image/png")], b).into_response(), + Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), + } + } } #[tokio::main] diff --git a/web/src/templates/index.html b/web/src/templates/index.html index a002824..4684a35 100644 --- a/web/src/templates/index.html +++ b/web/src/templates/index.html @@ -6,117 +6,262 @@ QRGen Web -
-

QRGen Web

- -
- - - - + + +
QRGen
+ + +
+ + -
- - - 8px + + +
+
+ 输入内容生成 QR 码 + + +
+
-
- 输入内容生成 QR 码 -
-
-
- - + + +
+ +
+