<返回更多

微信小程序文本内容安全识别接口

2022-12-04  今日头条  世界上最好的语言
加入收藏

环境:TP5+EasyWechat4

小程序后台配置违禁关键词

小程序端配置违禁关键词

② 手动修改下easywechat的checkText方法

 

php代码

	public function _initialize()
    {
        parent::_initialize();
        $config = config('site');
        $this->App = Factory::miniProgram([
            'app_id' => $config['xcx_appid'],
            'secret' => $config['xcx_secret'],
        ]);
    }    
	public function add_chem(){
        if(request()->isPost()){
            $data = input('post.'); 
            $content = $data['content'];
            $userInfo = $this->auth->getUserInfo();

            //违禁词处理
            $third =  Db::name('third')->where(['user_id'=>$userInfo['user_id']])->field('id,openid')->find();
            $params = [
                'openid'=> $third['openid'], //用户需2小时内访问过小程序
                'content' => $content,
                'version' => 2,
                'scene' =>1, //场景枚举值(1 资料;2 评论;3 论坛;4 社交日志
            ];
            $wei = $this->app->content_security->checkText($params);
            if($wei['errcode']==0 && $wei['result']['suggest'] !== 'pass'){
                $keywords = [];
                foreach ($wei['detail'] as $k => $v) {
                    if($v['strategy'] == "keyword" && isset($v['keyword'])){
                        $keywords[]=$v['keyword'];
                    }
                }
                if(count($keywords) > 0){
                    $keywords = implode('/', $keywords);
                    $this->error('您提交的信息包含违禁词【'.$keywords.'】,请重新编辑后提交!');
                }else{
                    $this->error('您提交的信息包含违禁内容,请重新编辑后提交!');
                }
            }
						//设置内容标签
            $tags = $this->setTags($data['content']);
            //隐藏内容中的手机号与电话
            $content = preg_replace("/(([0-9]{3,4}-)?[0-9]{7,8})/","************", preg_replace("/(d{3})d{4}(d{4})/", "***********", $content));
            $addData = [
                'user_id'=>$this->auth->getUserInfo()['user_id'],
                'type'=>$data['type'],
                'content'=>$content,
                'tags'=>$tags,
                'images'=>$data['images'],
                'name'=>$data['name'],
                'mobile'=>$data['mobile'],
                'user_name'=>$userInfo['nickname'],
                'user_avatar'=>$userInfo['avatar'],
                'status'=>0,
                'updatetime'=>time(),
                'createtime'=>time()
            ];

            $chemId = Db::name('chem')->insertGetId($addData);
            if($chemId){
                $this->success('发布成功,请等待管理员审核!',['chem_id'=>$chemId]);
            }else{
                $this->error('发布失败!');
            }
        }
    }
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>