前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序内容安全检测(敏感词、敏感图)

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

作者头像
很酷的站长
发布2023-01-02 11:43:13
4.8K0
发布2023-01-02 11:43:13
举报
微信小程序内容安全检测(敏感词、敏感图)
微信小程序内容安全检测(敏感词、敏感图)
1. 前言

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

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

下载 TP6.0 最新正式版

代码语言:javascript
复制
composer create-project topthink/think=6.0.* tp6

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

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

代码语言:javascript
复制
cd tp6 && composer require overtrue/wechat:~4.0
2. 文本内容安全检测

使用示例

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

抛出错误

代码语言:javascript
复制
{  "code": 201,  "msg": "系统检测到文本内容中包含非法内容"}
3. 图片内容安全检测

测试表单

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

上传接口

代码语言:javascript
复制
$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. 代码示例(基础类库层、逻辑层、函数)

基础类库层

代码语言:javascript
复制
<?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;// 内容安全        }    }}

逻辑层

代码语言:javascript
复制
<?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);    }}

自定义函数

代码语言:javascript
复制
/** * 抛出异常 * * @param string  $msg * @param integer $code */function fault(string $msg = "", int $code = <span class="hljs
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 前言
  • 2. 文本内容安全检测
  • 3. 图片内容安全检测
  • 4. 代码示例(基础类库层、逻辑层、函数)
相关产品与服务
应用安全开发
应用安全开发(Application Security Development,下文中也叫 Xcheck)为您提供优质的代码分析服务。Xcheck 凭借优秀的算法和工程实现,能在极低的误报率和漏报率前提下,以极快的速度发现代码中存在的安全漏洞。Xcheck 采用私有化部署的模式,所以产品使用的整个生命周期,源码都不会流出公司网络,杜绝源码泄露风险。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档