love_php/app/Http/Controllers/Admin/BadWordController.php
2026-04-02 09:20:51 +08:00

200 lines
7.2 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\BadWordType;
use App\Models\BadWord;
use App\Models\Moment;
use App\Models\User;
use App\Models\VerifyLog;
class BadWordController extends Controller
{
/** 添加 敏感词类型 */
public function createBadwordType(Request $request){
$name = $request->input('name');
if(empty($name)){
return $this->failure('请输入敏感词类型名字');
}
$badWordType = new BadWordType();
$res = $badWordType->where('name',$name)->first();
if($res){
return $this->failure('不能添加重复类型');
}
$badWordType->name = $name;
$badWordType->operator = auth()->id();
$badWordType->save();
return $this->success('ok');
}
/** 修改 敏感词类型 名字 */
public function updateBadwordType(Request $request){
$id = $request->input('id');
$badWordType = new BadWordType();
$badWordTypeData = $badWordType->where('id',$id)->first();
if (empty($badWordTypeData)) return $this->failure('敏感词类型不存在');
if (empty($request->name)) return $this->failure('请输入敏感词');
if ($request->name && $request->name != $badWordTypeData->name) {
$badWordTypeData->name = $request->name;
}
$badWordTypeData->operator = auth()->id();
$badWordTypeData->save();
return $this->success('ok',$badWordTypeData);
}
/** 删除 敏感词类型 */
public function deleteBadwordType(Request $request){
$id = $request->input('id',0);
$status = $request->input('status',1);
$badWordType = BadWordType::find($id);
if (empty($badWordType)) return $this->failure('敏感词类型不存在');
$badWordType->delete();//删除类型 也删除类型对应的敏感词
BadWord::where('bad_word_type_id',$id)->delete();
return $this->success('ok');
}
/** 列表 敏感词类型 */
public function pagingBadwordType(Request $request){
$keyword = $request->input('keyword');
$size = $request->input('size',15);
$badWordType = new BadWordType();
$badWordType_list = $badWordType::with('user:id,nickname,app_avatar,circle_avatar')
->whereHas('user')->orderBy('id','desc');
if ($keyword) {
$keyword = trim($keyword);
$badWordType_list = $badWordType_list->whereHas('user',function($sql) use($keyword){
$sql->where('id',$keyword)->orWhere('nickname','like','%'.$keyword.'%');
});
}
$badWordType_list = $badWordType_list->paginate($size);
return $this->success('ok',$badWordType_list);
}
/** 开始校验 */
public function startVerifyAll(Request $request){
$t1=microtime(true);
$momentList = Moment::get();
$illegality_keyword = BadWord::get();
$violation_moment_list = [];
$violation_userinfo_list = [];
VerifyLog::where('id','>=','1')->delete();
foreach ($momentList as $m_i){
$res = \CommonUtilsService::verifyTextV2([$m_i->content],$illegality_keyword,1);
if($res['code'] != 1){
$violation_moment_list[] = $this->verifyStatisticsArray('moment',$m_i->id,$res['detail_context'],$res['code'],$res['type'],$res['context'],'content');
}
}
$userCount = User::where('type','signle')->count();
for($uc=0;$uc<=$userCount;$uc+=10000){
$thisUserCount = $uc;
$userList = User::with('profileCourtship')
->whereHas('profileCourtship', function ($sql){
$sql->where('introduction','<>','');
$sql->where('ideal_mate','<>','');
})
->where('type','single')
->limit(10000)
->offset($thisUserCount)
->get();
foreach ($userList as $u){
$introduction = $u->profileCourtship->introduction;
$ideal_mate = $u->profileCourtship->ideal_mate;
$res = \CommonUtilsService::verifyTextV2([$introduction],$illegality_keyword,1);
if($res['code'] != 1){
$violation_userinfo_list[] = $this->verifyStatisticsArray('user',$u->id,$res['detail_context'],$res['code'],$res['type'],$res['context'],'introduction');
}
$res = \CommonUtilsService::verifyTextV2([$ideal_mate],$illegality_keyword,1);
if($res['code'] != 1){
$violation_userinfo_list[] = $this->verifyStatisticsArray('user',$u->id,$res['detail_context'],$res['code'],$res['type'],$res['context'],'ideal_mate');
}
}
}
$data['violation_moment_list'] = $violation_moment_list;
$data['violation_userinfo_list'] = $violation_userinfo_list;
$r = '耗时:'.(microtime(true)-$t1);
return $this->success('完成',[$r]);
}
/**敏感词 校验记录 */
public function verifyLogList(Request $request){
$source_type = $request->input('source_type','user');
if ($source_type == 'user') {
$verify_logsList = VerifyLog::with('user')->where('source_type',$source_type)->paginate()->toArray();
}else{
$verify_logsList = VerifyLog::where('source_type',$source_type)->paginate()->toArray();
}
$verify_logs = $verify_logsList['data'];
for($v = 0;$v<count($verify_logs);$v++){
$v_item = $verify_logs[$v];
$field = $v_item['source_context_field'];
$source_type = $v_item['source_type'];
$error_code = $v_item['error_code'];
$show_source_type = '';
$show_field = '';
$show_error_msg = '包含敏感词';
if($source_type == 'moment'){
$show_source_type = '动态';
if($field == 'content'){
$show_field = '动态内容';
}
}else if($source_type == 'user'){
$show_source_type = '用户信息';
if($field == 'introduction'){
$show_field = '个人介绍';
}else if($field == 'ideal_mate'){
$show_field = '择偶标准';
}
}
if($error_code == '4001'){
$show_error_msg = "包含手机号";
}
$verify_logs[$v]['show_source_type'] = $show_source_type;
$verify_logs[$v]['show_field'] = $show_field;
$verify_logs[$v]['show_error_msg'] = $show_error_msg;
}
$verify_logsList['data'] = $verify_logs;
return $this->success('',$verify_logsList);
}
/***/
protected function verifyStatisticsArray($sourceType,$sourceId,$context,$errorCode,$errorType,$errorContext,$source_context_field){
VerifyLog::create([
'source_type'=>$sourceType,
'source_id'=>$sourceId,
'source_context'=>$context,
'source_context_field'=>$source_context_field,
'error_code'=>$errorCode,
'error_type'=>$errorType,
'error_context'=>$errorContext
]);
}
}