
微信小程序内容安全检测(敏感词、敏感图)
来源:
浏览:118
时间:2023-08-04
1. 前言
下载 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 = pplogicWeChat::checkText($content);$bool === false && fault('系统检测到文本内容中包含非法内容');halt('文本内容合法');抛出错误
{ "code": 201, "msg": "系统检测到文本内容中包含非法内容"}3. 图片内容安全检测
测试表单
上传接口
$name = 'img'; // 文件上传字段域名称try { $file = request()->file($name); if (!$file) throw new Exception('没有文件上传'); // 敏感图检测 // checkImage() 参数要求必须是绝对路径地址的图片,可以使用图片的临时存放路径 $bool = pplogicWeChat::checkImage($file->getRealPath()); $bool === false && fault('系统检测到图片中包含非法内容'); // 上传操作 ...} catch (Exception $e) { fault($e->getMessage());}4. 代码示例(基础类库层、逻辑层、函数)
基础类库层
'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;// 内容安全 } }}逻辑层
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 =


