環境: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('發布失敗!');
}
}
}