环境: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('发布失败!');
}
}
}