zl程序教程

您现在的位置是:首页 >  APP

当前栏目

微信小程序内容安全检测(敏感词、敏感图)

2023-02-19 12:20:08 时间

1. 前言


微信小程序官方文档 - 内容安全检测

推荐使用 EasyWechat: https://www.easywechat.com/docs/4.x/basic-services/content_security

下载 TP6.0 最新正式版

composer create-project topthink/think=6.0.* tp6

进入框架根目录,安装 easywechat 4.x 版本扩展包

easywechat 4.x 要求PHP7.2+,tp6.0 要求PHP7.2.5+, 这个版本最适合在TP6.0中使用

cd tp6 && composer require overtrue/wechat:~4.0

2. 文本内容安全检测


使用示例

$content = '某某某';$bool = \app\logic\WeChat::checkText($content);$bool === false && fault('系统检测到文本内容中包含非法内容');halt('文本内容合法');

抛出错误

{  "code": 201,  "msg": "系统检测到文本内容中包含非法内容"}

3. 图片内容安全检测


测试表单

<form action="{:url('upload')}" method="post" enctype="multipart/form-data">    <input type="file" name="img">    <button>提交</button></form>

上传接口

$name = 'img'; // 文件上传字段域名称try {    $file = request()->file($name);    if (!$file) throw new \Exception('没有文件上传');    // 敏感图检测    // checkImage() 参数要求必须是绝对路径地址的图片,可以使用图片的临时存放路径    $bool = \app\logic\WeChat::checkImage($file->getRealPath());    $bool === false && fault('系统检测到图片中包含非法内容');    // 上传操作 ...} catch (\Exception $e) {    fault($e->getMessage());}

4. 代码示例(基础类库层、逻辑层、函数)


基础类库层

<?phpnamespace app\lib;use EasyWeChat\Factory;/** * 小程序 */class MiniProgram{    private $app;    /**     * 初始化配置     */    public function __construct()    {        $config = [            'app_id' => 'wx4837bd88b6xxxxx',            'secret' => 'c8fe4278064b22d722682xxxxx',            // 下面为可选项            // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名            'response_type' => 'array',        ];        $this->app = Factory::miniProgram($config);    }    /**     * 文本安全内容检测(敏感词检测)     *     * @param string $content 文本内容     */    public function checkText(string $content)    {        $result = $this->app->content_security->checkText($content);        if (isset($result['errcode']) && $result['errcode'] == 87014) {            return false;//含有非法内容        } else {            return true;// 内容安全        }    }    /**     * 图片内容安全检测(敏感图检测)     *     * @param string $image 绝对路径图片地址     */    public function checkImage(string $image)    {        $result = $this->app->content_security->checkImage($image);        if (isset($result['errcode']) && $result['errcode'] == 87014) {            return false;//含有非法内容        } else {            return true;// 内容安全        }    }}

逻辑层

<?phpnamespace app\logic;use app\lib\MiniProgram;class WeChat{    // +------------------------------------------------------------    // | 小程序    // +------------------------------------------------------------    /**     * 敏感词检测     *     * @param  string  $content     * @return boolean true 内容安全     */    public static function checkText(string $content)    {        return app(MiniProgram::class)->checkText($content);    }    /**     * 敏感图检测     *     * @param  string  $image     */    public static function checkImage(string $image)    {        return app(MiniProgram::class)->checkImage($image);    }}

自定义函数

/** * 抛出异常 * * @param string  $msg * @param integer $code */function fault(string $msg = "", int $code = <span class="hljs