fix(desktop): DeerFlow UI — ChatArea refactor + ai-elements + dead CSS cleanup
ChatArea retry button uses setInput instead of direct sendToGateway, fix bootstrap spinner stuck for non-logged-in users, remove dead CSS (aurora-title/sidebar-open/quick-action-chips), add ai components (ReasoningBlock/StreamingText/ChatMode/ModelSelector/TaskProgress), add ClassroomPlayer + ResizableChatLayout + artifact panel Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,6 @@ use axum::{
|
||||
extract::{Extension, Form, Path, Query, State},
|
||||
Json,
|
||||
};
|
||||
use axum::response::Html;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::auth::types::AuthContext;
|
||||
@@ -90,9 +89,8 @@ pub async fn increment_usage_dimension(
|
||||
));
|
||||
}
|
||||
|
||||
for _ in 0..req.count {
|
||||
service::increment_dimension(&state.db, &ctx.account_id, &req.dimension).await?;
|
||||
}
|
||||
// 单次原子更新,避免循环 N 次数据库查询
|
||||
service::increment_dimension_by(&state.db, &ctx.account_id, &req.dimension, req.count).await?;
|
||||
|
||||
// 返回更新后的用量
|
||||
let usage = service::get_or_create_usage(&state.db, &ctx.account_id).await?;
|
||||
@@ -109,10 +107,12 @@ pub async fn create_payment(
|
||||
Extension(ctx): Extension<AuthContext>,
|
||||
Json(req): Json<CreatePaymentRequest>,
|
||||
) -> SaasResult<Json<PaymentResult>> {
|
||||
let config = state.config.read().await;
|
||||
let result = super::payment::create_payment(
|
||||
&state.db,
|
||||
&ctx.account_id,
|
||||
&req,
|
||||
&config.payment,
|
||||
).await?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
@@ -139,22 +139,28 @@ pub async fn payment_callback(
|
||||
) -> SaasResult<String> {
|
||||
tracing::info!("Payment callback received: method={}, body_len={}", method, body.len());
|
||||
|
||||
// 解析回调参数
|
||||
let body_str = String::from_utf8_lossy(&body);
|
||||
let config = state.config.read().await;
|
||||
|
||||
// 支付宝回调:form-urlencoded 格式
|
||||
// 微信回调:JSON 格式
|
||||
let (trade_no, status) = if method == "alipay" {
|
||||
parse_alipay_callback(&body_str)
|
||||
let (trade_no, status, callback_amount) = if method == "alipay" {
|
||||
parse_alipay_callback(&body_str, &config.payment)?
|
||||
} else if method == "wechat" {
|
||||
parse_wechat_callback(&body_str)
|
||||
parse_wechat_callback(&body_str, &config.payment)?
|
||||
} else {
|
||||
tracing::warn!("Unknown payment callback method: {}", method);
|
||||
return Ok("fail".into());
|
||||
};
|
||||
|
||||
if let Some(trade_no) = trade_no {
|
||||
super::payment::handle_payment_callback(&state.db, &trade_no, &status).await?;
|
||||
// trade_no 是必填字段,缺失说明回调格式异常
|
||||
let trade_no = trade_no.ok_or_else(|| {
|
||||
tracing::warn!("Payment callback missing out_trade_no: method={}", method);
|
||||
SaasError::InvalidInput("回调缺少交易号".into())
|
||||
})?;
|
||||
|
||||
if let Err(e) = super::payment::handle_payment_callback(&state.db, &trade_no, &status, callback_amount).await {
|
||||
// 对外返回通用错误,不泄露内部细节
|
||||
tracing::error!("Payment callback processing failed: method={}, error={}", method, e);
|
||||
return Ok("fail".into());
|
||||
}
|
||||
|
||||
// 支付宝期望 "success",微信期望 JSON
|
||||
@@ -178,6 +184,11 @@ pub struct MockPayQuery {
|
||||
pub async fn mock_pay_page(
|
||||
Query(params): Query<MockPayQuery>,
|
||||
) -> axum::response::Html<String> {
|
||||
// HTML 转义防止 XSS
|
||||
let safe_subject = html_escape(¶ms.subject);
|
||||
let safe_trade_no = html_escape(¶ms.trade_no);
|
||||
let amount_yuan = params.amount as f64 / 100.0;
|
||||
|
||||
axum::response::Html(format!(r#"
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
@@ -194,23 +205,19 @@ body {{ font-family: system-ui; max-width: 480px; margin: 40px auto; padding: 20
|
||||
</style></head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<div class="subject">{subject}</div>
|
||||
<div class="subject">{safe_subject}</div>
|
||||
<div class="amount">¥{amount_yuan}</div>
|
||||
<div style="text-align:center;color:#999;font-size:12px;margin-bottom:16px;">
|
||||
订单号: {trade_no}
|
||||
订单号: {safe_trade_no}
|
||||
</div>
|
||||
<form action="/api/v1/billing/mock-pay/confirm" method="POST">
|
||||
<input type="hidden" name="trade_no" value="{trade_no}" />
|
||||
<input type="hidden" name="trade_no" value="{safe_trade_no}" />
|
||||
<button type="submit" name="action" value="success" class="btn btn-pay">确认支付 ¥{amount_yuan}</button>
|
||||
<button type="submit" name="action" value="fail" class="btn btn-fail">模拟失败</button>
|
||||
</form>
|
||||
</div>
|
||||
</body></html>
|
||||
"#,
|
||||
subject = params.subject,
|
||||
trade_no = params.trade_no,
|
||||
amount_yuan = params.amount as f64 / 100.0,
|
||||
))
|
||||
"#))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -226,7 +233,7 @@ pub async fn mock_pay_confirm(
|
||||
) -> SaasResult<axum::response::Html<String>> {
|
||||
let status = if form.action == "success" { "success" } else { "failed" };
|
||||
|
||||
if let Err(e) = super::payment::handle_payment_callback(&state.db, &form.trade_no, status).await {
|
||||
if let Err(e) = super::payment::handle_payment_callback(&state.db, &form.trade_no, status, None).await {
|
||||
tracing::error!("Mock payment callback failed: {}", e);
|
||||
}
|
||||
|
||||
@@ -249,31 +256,140 @@ body {{ font-family: system-ui; max-width: 480px; margin: 40px auto; padding: 20
|
||||
"#)))
|
||||
}
|
||||
|
||||
// === 辅助函数 ===
|
||||
// === 回调解析 ===
|
||||
|
||||
fn parse_alipay_callback(body: &str) -> (Option<String>, String) {
|
||||
// 简化解析:支付宝回调是 form-urlencoded
|
||||
/// 解析支付宝回调并验签,返回 (trade_no, status, callback_amount_cents)
|
||||
fn parse_alipay_callback(
|
||||
body: &str,
|
||||
config: &crate::config::PaymentConfig,
|
||||
) -> SaasResult<(Option<String>, String, Option<i32>)> {
|
||||
// form-urlencoded → key=value 对
|
||||
let mut params: Vec<(String, String)> = Vec::new();
|
||||
for pair in body.split('&') {
|
||||
if let Some((k, v)) = pair.split_once('=') {
|
||||
if k == "out_trade_no" {
|
||||
return (Some(urlencoding::decode(v).unwrap_or_default().to_string()), "TRADE_SUCCESS".into());
|
||||
}
|
||||
params.push((
|
||||
k.to_string(),
|
||||
urlencoding::decode(v).unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
(None, "unknown".into())
|
||||
|
||||
let mut trade_no = None;
|
||||
let mut callback_amount: Option<i32> = None;
|
||||
|
||||
// 验签:生产环境强制,开发环境允许跳过
|
||||
let is_dev = std::env::var("ZCLAW_SAAS_DEV")
|
||||
.map(|v| v == "true" || v == "1")
|
||||
.unwrap_or(false);
|
||||
|
||||
if let Some(ref public_key) = config.alipay_public_key {
|
||||
match super::payment::verify_alipay_callback(¶ms, public_key) {
|
||||
Ok(true) => {}
|
||||
Ok(false) => {
|
||||
tracing::warn!("Alipay callback signature verification FAILED");
|
||||
return Err(SaasError::InvalidInput("支付宝回调验签失败".into()));
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Alipay callback verification error: {}", e);
|
||||
return Err(SaasError::InvalidInput("支付宝回调验签异常".into()));
|
||||
}
|
||||
}
|
||||
} else if !is_dev {
|
||||
tracing::error!("Alipay public key not configured in production — rejecting callback");
|
||||
return Err(SaasError::InvalidInput("支付宝公钥未配置,无法验签".into()));
|
||||
} else {
|
||||
tracing::warn!("Alipay public key not configured (dev mode), skipping signature verification");
|
||||
}
|
||||
|
||||
// 提取 trade_no、trade_status 和 total_amount
|
||||
let mut trade_status = "unknown".to_string();
|
||||
for (k, v) in ¶ms {
|
||||
match k.as_str() {
|
||||
"out_trade_no" => trade_no = Some(v.clone()),
|
||||
"trade_status" => trade_status = v.clone(),
|
||||
"total_amount" => {
|
||||
// 支付宝金额为元(字符串),转为分(整数)
|
||||
if let Ok(yuan) = v.parse::<f64>() {
|
||||
callback_amount = Some((yuan * 100.0).round() as i32);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// 支付宝成功状态映射
|
||||
let status = if trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED" {
|
||||
"TRADE_SUCCESS"
|
||||
} else {
|
||||
&trade_status
|
||||
};
|
||||
|
||||
Ok((trade_no, status.to_string(), callback_amount))
|
||||
}
|
||||
|
||||
fn parse_wechat_callback(body: &str) -> (Option<String>, String) {
|
||||
// 微信回调是 JSON
|
||||
if let Ok(v) = serde_json::from_str::<serde_json::Value>(body) {
|
||||
if let Some(event_type) = v.get("event_type").and_then(|t| t.as_str()) {
|
||||
if event_type == "TRANSACTION.SUCCESS" {
|
||||
let trade_no = v.pointer("/resource/out_trade_no")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string());
|
||||
return (trade_no, "SUCCESS".into());
|
||||
}
|
||||
}
|
||||
/// 解析微信支付回调,解密 resource 字段,返回 (trade_no, status, callback_amount_cents)
|
||||
fn parse_wechat_callback(
|
||||
body: &str,
|
||||
config: &crate::config::PaymentConfig,
|
||||
) -> SaasResult<(Option<String>, String, Option<i32>)> {
|
||||
let v: serde_json::Value = serde_json::from_str(body)
|
||||
.map_err(|e| SaasError::InvalidInput(format!("微信回调 JSON 解析失败: {}", e)))?;
|
||||
|
||||
let event_type = v.get("event_type")
|
||||
.and_then(|t| t.as_str())
|
||||
.unwrap_or("");
|
||||
|
||||
if event_type != "TRANSACTION.SUCCESS" {
|
||||
// 非支付成功事件(如退款等),忽略
|
||||
return Ok((None, event_type.to_string(), None));
|
||||
}
|
||||
(None, "unknown".into())
|
||||
|
||||
// 解密 resource 字段
|
||||
let resource = v.get("resource")
|
||||
.ok_or_else(|| SaasError::InvalidInput("微信回调缺少 resource 字段".into()))?;
|
||||
|
||||
let ciphertext = resource.get("ciphertext")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| SaasError::InvalidInput("微信回调 resource 缺少 ciphertext".into()))?;
|
||||
let nonce = resource.get("nonce")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| SaasError::InvalidInput("微信回调 resource 缺少 nonce".into()))?;
|
||||
let associated_data = resource.get("associated_data")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
|
||||
let api_v3_key = config.wechat_api_v3_key.as_deref()
|
||||
.ok_or_else(|| SaasError::InvalidInput("微信 API v3 密钥未配置,无法解密回调".into()))?;
|
||||
|
||||
let plaintext = super::payment::decrypt_wechat_resource(
|
||||
ciphertext, nonce, associated_data, api_v3_key,
|
||||
)?;
|
||||
|
||||
let decrypted: serde_json::Value = serde_json::from_str(&plaintext)
|
||||
.map_err(|e| SaasError::Internal(format!("微信回调解密内容 JSON 解析失败: {}", e)))?;
|
||||
|
||||
let trade_no = decrypted.get("out_trade_no")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string());
|
||||
|
||||
let trade_state = decrypted.get("trade_state")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("UNKNOWN");
|
||||
|
||||
// 微信金额已为分(整数)
|
||||
let callback_amount = decrypted.get("amount")
|
||||
.and_then(|a| a.get("total"))
|
||||
.and_then(|v| v.as_i64())
|
||||
.map(|v| v as i32);
|
||||
|
||||
Ok((trade_no, trade_state.to_string(), callback_amount))
|
||||
}
|
||||
|
||||
/// HTML 转义,防止 XSS 注入
|
||||
fn html_escape(s: &str) -> String {
|
||||
s.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
.replace('\'', "'")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user