98 lines
2.6 KiB
PHP
98 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\BadWord;
|
|
|
|
class ImMessageService
|
|
{
|
|
/**
|
|
* 类型
|
|
* @var
|
|
*/
|
|
public $type;
|
|
|
|
/**
|
|
* 发送内容
|
|
* @var
|
|
*/
|
|
public $content;
|
|
|
|
/**
|
|
* 用户
|
|
* @var
|
|
*/
|
|
public $user;
|
|
|
|
/**
|
|
* 发送入口
|
|
* @return array
|
|
* @throws \Exception
|
|
*/
|
|
public function send(): array
|
|
{
|
|
$type = $this->type;
|
|
if (!in_array($type, ['text', 'picture'])) {
|
|
throw new \Exception('未知类型');
|
|
}
|
|
return $this->$type();
|
|
}
|
|
|
|
/**
|
|
* 发送文本
|
|
* @return array[]|string[]|\string[][]
|
|
* @throws \Exception
|
|
*/
|
|
private function text(): array
|
|
{
|
|
$content = $this->filterContent($this->content);
|
|
$check = BadWord::whereRaw("instr('$content',content) > 0")->first();
|
|
if($check){
|
|
throw new \Exception('检测到有敏感词,发送失败');
|
|
}
|
|
// $words = BadWord::pluck('content');
|
|
// foreach ($words as $word) {
|
|
// if (strpos($content, $word) !== false) {
|
|
// throw new \Exception('检测到有敏感词,发送失败');
|
|
// }
|
|
// }
|
|
return ['content' => $content];
|
|
}
|
|
|
|
/**
|
|
* 发送图片
|
|
* @return array
|
|
*/
|
|
private function picture(): array
|
|
{
|
|
return ['content' => $this->content];
|
|
}
|
|
|
|
/**
|
|
* 过滤文字
|
|
* @param $content
|
|
* @return array|string|string[]
|
|
*/
|
|
private function filterContent($content)
|
|
{
|
|
$preg_wechat = '/[1-9]([0-9]{5,11})|0?(13|14|15|18)[0-9]{9}|[a-zA-Z]{6}|(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{5}|[\x80-\xff]+[a-z-A-Z]{5}|((0|[1-9]\d*)(\.\d+)?){5}|(零|一|二|三|四|五|六|七|八|九|十)(百|十|零)?(一|二|三|四|五|六|七|八|九)?(百|十|零)?(一|二|三|四|五|六|七|八|九){2}|(①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩)?(①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩)?(①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩)|(壹|贰|叁|肆|伍|陆|柒|捌|玖|零|拾)|(❶|❷|❸|❹|❺|❻|❼|❽|❾|❿)+/';
|
|
$filter = (bool)preg_match($preg_wechat, preg_quote($content));
|
|
|
|
//已经违规
|
|
if ($filter) {
|
|
//替换正则数据
|
|
$content = preg_replace_callback($preg_wechat, function ($arr) {
|
|
if (!empty($arr[0])) {
|
|
$str = '';
|
|
for ($i = 0; $i < mb_strlen($arr[0]); $i++) {
|
|
$str .= '*';
|
|
}
|
|
$arr[0] = $str;
|
|
}
|
|
return $arr[0];
|
|
}, $content);
|
|
}
|
|
|
|
return str_replace('\\', '', $content);
|
|
}
|
|
} |