Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

微信内容安全

Feature: wx-sec-check

📖 官方文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html

配置

[wx_sec_check]
app_id     = "wx1234567890"
app_secret = "your-secret"
# base_url = "https://api.weixin.qq.com"  # 可选,默认值

API

WxSecCheck

方法参数返回说明
msg_sec_check&MsgSecCheckRequestResult<MsgSecCheckResponse>文本内容安全检测
media_check_async&MediaCheckRequestResult<MediaCheckResponse>异步媒体内容安全检测(结果由微信推送)

MsgSecCheckRequest

#![allow(unused)]
fn main() {
// Builder 模式构造
let request = MsgSecCheckRequest::new(
    "待检测文本",       // content: &str
    Scene::Comment,     // scene: Scene
    "user_openid",      // openid: &str
)
.title("标题")         // 可选
.nickname("昵称")      // 可选
.signature("签名");    // 可选,仅 scene=Profile 有效
}
字段类型必填说明
contentString文本内容,上限 2500 字,UTF-8
versionu8固定值 2(自动设置)
sceneu8场景:1 资料 / 2 评论 / 3 论坛 / 4 社交日志
openidString用户 openid
titleOption<String>文本标题
nicknameOption<String>用户昵称
signatureOption<String>个性签名(仅 scene=1)

MsgSecCheckResponse

#![allow(unused)]
fn main() {
pub struct MsgSecCheckResponse {
    pub errcode: Option<i32>,
    pub errmsg: Option<String>,
    pub trace_id: Option<String>,
    pub result: Option<SecCheckResult>,     // 综合结果
    pub detail: Option<Vec<SecCheckDetail>>,// 详细检测结果
}
}

MediaCheckRequest

#![allow(unused)]
fn main() {
let request = MediaCheckRequest::new(
    "https://example.com/image.jpg",  // media_url
    MediaType::Image,                  // media_type: 1=音频, 2=图片
    Scene::Comment,                    // scene
    "user_openid",                     // openid
);
}

异步检测结果会在 30 分钟内推送到消息接收服务器,推送事件为 wxa_media_check。 文件大小限制 10MB。

场景枚举 (Scene)

名称说明
1Profile资料
2Comment评论
3Forum论坛
4Social社交日志

建议枚举 (Suggest)

说明
Pass通过
Review需人工审核
Risky拦截

标签枚举 (Label)

名称说明
100Normal正常
10001Ad广告
20001Politics时政
20002Porn色情
20003Abuse辱骂
20006Illegal违法犯罪
20008Fraud欺诈
20012Vulgar低俗
20013Copyright版权
21000Other其他

媒体类型 (MediaType)

名称支持格式
1Audiomp3, aac, ac3, wma, flac, vorbis, opus, wav
2Imagejpg, jpeg, png, bmp, gif (取首帧)

错误码

English中文
40701WeChat API business error微信 API 业务错误
40702Content is empty or exceeds 2500 characters内容为空或超过 2500 字
50701Content check URL build failed构建请求 URL 失败
50702Content check request failed发送请求失败
50703Content check response parse failed解析响应 JSON 失败
50704Content check response deserialize failed反序列化响应失败
50705Failed to get access_token获取 access_token 失败
50706Access_token response parse failedaccess_token 响应解析失败
50806Media detection notification parse error内容安全通知解析失败
50815Media detection callback not registered未注册媒体检测回调

使用示例

文本检测

#![allow(unused)]
fn main() {
use afaster::state::wx_sec_check::{MsgSecCheckRequest, Scene};

let request = MsgSecCheckRequest::new(
    "待检测的文本内容",
    Scene::Comment,
    "user_openid",
);

let response = state.wx_sec_check.msg_sec_check(&request).await?;

if let Some(result) = &response.result {
    match result.suggest.as_str() {
        "risky" => println!("拦截: label={}", result.label),
        "review" => println!("需审核: label={}", result.label),
        "pass" => println!("通过"),
        _ => {}
    }
}
}

媒体检测

#![allow(unused)]
fn main() {
use afaster::state::wx_sec_check::{MediaCheckRequest, MediaType, Scene};

let request = MediaCheckRequest::new(
    "https://example.com/image.jpg",
    MediaType::Image,
    Scene::Comment,
    "user_openid",
);

let response = state.wx_sec_check.media_check_async(&request).await?;
println!("trace_id: {:?}", response.trace_id);
}

注册媒体检测回调

统一通知模块:媒体检测结果由 wx-notify 模块统一接收和分发。 微信后台只需配置一个回调 URL(默认 https://your-domain/wx/notify), 后端通过 Event 字段自动路由到 wxa_media_check 回调。

详细配置参见 config.toml 中的 [wx_notify] 段。

#![allow(unused)]
fn main() {
use afaster::AFaster;

AFaster::new()
    .with_wx_notify(|w| {
        w.with_media_check_callback(|(state, notify)| {
            async move {
                // 处理异步检测结果
                if let Some(result) = &notify.result {
                    println!("检测结果: {:?}", result.suggest);
                }
                if let Some(detail) = &notify.detail {
                    for item in detail {
                        println!("策略: {}, 建议: {}, 标签: {}", item.strategy, item.suggest, item.label);
                    }
                }
                Ok(WxSecCheckNotifyResult::success())
            }
        })
    })
    .run()
    .await;
}

注意:未注册回调时,wx-notify 模块返回错误码 50815,微信会重试最多 15 次。