//! WASM 测试插件 — 验证插件端 API use serde_json::json; wit_bindgen::generate!({ path: "../erp-plugin-prototype/wit/plugin.wit", world: "plugin-world", }); use crate::erp::plugin::host_api; use crate::exports::erp::plugin::plugin_api::Guest; struct TestPlugin; impl Guest for TestPlugin { fn init() -> Result<(), String> { host_api::log_write("info", "测试插件初始化成功"); let data = json!({"sku": "TEST-001", "name": "测试商品", "quantity": 100}).to_string(); let result = host_api::db_insert("inventory_item", data.as_bytes()) .map_err(|e| format!("db_insert 失败: {}", e))?; let record: serde_json::Value = serde_json::from_slice(&result).map_err(|e| format!("解析结果失败: {}", e))?; host_api::log_write( "info", &format!( "插入成功: id={}, tenant_id={}", record["id"].as_str().unwrap_or("?"), record["tenant_id"].as_str().unwrap_or("?") ), ); Ok(()) } fn on_tenant_created(tenant_id: String) -> Result<(), String> { host_api::log_write("info", &format!("租户创建: {}", tenant_id)); let data = json!({"name": "默认分类", "tenant_id": tenant_id}).to_string(); host_api::db_insert("inventory_category", data.as_bytes()) .map_err(|e| format!("创建默认分类失败: {}", e))?; Ok(()) } fn handle_event(event_type: String, payload: Vec) -> Result<(), String> { host_api::log_write("info", &format!("处理事件: {}", event_type)); let data: serde_json::Value = serde_json::from_slice(&payload).map_err(|e| format!("解析失败: {}", e))?; if event_type == "workflow.task.completed" { let order_id = data["order_id"].as_str().unwrap_or("unknown"); let update = json!({"status": "approved"}).to_string(); host_api::db_update("purchase_order", order_id, update.as_bytes(), 1) .map_err(|e| format!("更新失败: {}", e))?; let evt = json!({"order_id": order_id, "status": "approved"}).to_string(); host_api::event_publish("purchase_order.approved", evt.as_bytes()) .map_err(|e| format!("发布事件失败: {}", e))?; host_api::log_write("info", &format!("采购单 {} 审批完成", order_id)); } Ok(()) } } export!(TestPlugin);