| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php
- namespace app\common\service;
- use think\Db;
- use think\Cache;
- use think\Exception;
- class RiskControlService
- {
- // 缓存前缀
- const CACHE_PREFIX = 'risk_control:';
- // 缓存时间(秒)
- const CACHE_TIME = 3600;
-
- /**
- * @var RuleCheckService
- */
- private $ruleCheckService;
-
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->ruleCheckService = new RuleCheckService();
- }
-
- /**
- * 处理风控事件
- * @param array $eventData 事件数据
- * @return array 处理结果
- */
- public function processEvent(array $eventData)
- {
- try {
- // 1. 数据验证
- $this->validateEventData($eventData);
- // 2. 检查黑白名单
- if ($this->checkBlackList($eventData)) {
- return ['is_blocked' => true, 'msg' => '账号已被封禁'];
- }
-
- // 3. 执行策略检查
- $strategyResults = $this->executeStrategies($eventData);
- // 4. 处理异常数据
- if (!empty($strategyResults['exceptions'])) {
- $this->handleExceptions($strategyResults['exceptions'], $eventData);
- }
-
- // 5. 清理过期数据
- $this->cleanExpiredData();
-
- // 6. 检查异常程度
- $riskLevel = $this->checkRiskLevel($eventData);
-
- return [
- 'is_blocked' => $riskLevel['is_blocked'],
- 'is_risky' => $riskLevel['is_risky'],
- 'risk_level' => $riskLevel['level'],
- 'msg' => $riskLevel['msg']
- ];
-
- } catch (Exception $e) {
- // 记录错误日志
- \think\Log::error('风控处理异常:' . $e->getMessage());
- return ['is_blocked' => false, 'is_risky' => false, 'msg' => '系统异常'];
- }
- }
-
- /**
- * 验证事件数据
- * @param array $eventData
- * @throws Exception
- */
- private function validateEventData(array $eventData)
- {
- $requiredFields = ['event_type', 'member_id', 'ip', 'imei', 'timestamp'];
- foreach ($requiredFields as $field) {
- if (!isset($eventData[$field]) || empty($eventData[$field])) {
- throw new Exception("缺少必要字段:{$field}");
- }
- }
- }
-
- /**
- * 检查黑名单
- * @param array $eventData
- * @return bool
- */
- private function checkBlackList(array $eventData)
- {
- $cacheKey = self::CACHE_PREFIX . 'blacklist:';
-
- // 检查IP
- if ($this->checkInList($eventData['ip'], 'ip', 'black', $cacheKey)) {
- return true;
- }
-
- // 检查IMEI
- if ($this->checkInList($eventData['imei'], 'imei', 'black', $cacheKey)) {
- return true;
- }
-
- // 检查用户ID
- if ($this->checkInList($eventData['member_id'], 'member_id', 'black', $cacheKey)) {
- return true;
- }
-
- return false;
- }
-
- /**
- * 检查是否在名单中
- * @param string $value
- * @param string $type
- * @param string $listType
- * @param string $cacheKey
- * @return bool
- */
- private function checkInList($value, $type, $listType, $cacheKey)
- {
- $key = $cacheKey . $listType . ':' . $type . ':' . $value;
-
- // 先查缓存
- if (Cache::get($key)) {
- return true;
- }
-
- // 查数据库
- $exists = Db::name('fk_roster_lists')
- ->where([
- 'object_type' => $type,
- 'object_value' => $value,
- 'list_type' => $listType,
- 'status' => 1
- ])
- ->where(function ($query) {
- $query->where('expire_time', '>', time())
- ->whereOr('expire_time', 0);
- })
- ->find();
-
- if ($exists) {
- Cache::set($key, 1, self::CACHE_TIME);
- return true;
- }
-
- return false;
- }
-
- /**
- * 执行策略检查
- * @param array $eventData
- * @return array
- */
- private function executeStrategies(array $eventData)
- {
- $exceptions = [];
-
- // 获取相关策略
- $strategies = Db::name('fk_strategies')
- ->where('status', 1)
- ->where('event_type', $eventData['event_type'])
- ->order('weight', 'desc')
- ->select();
-
- foreach ($strategies as $strategy) {
- // 获取策略详情
- $details = Db::name('fk_strategy_details')
- ->alias('d')
- ->join('fk_rules r', 'd.rule_id = r.id')
- ->where('d.strategy_id', $strategy['id'])
- ->where('r.status', 1)
- ->select();
- dump($details->toArray());
- foreach ($details as $detail) {
- if ($this->checkRule($detail, $eventData)) {
- $exceptions[] = [
- 'strategy_id' => $strategy['id'],
- 'rule_id' => $detail['rule_id'],
- 'object_type' => $detail['object_type'],
- 'object_value' => $eventData[$detail['object_type']],
- 'exception_level' => $detail['exception_level'],
- 'expire_time' => time() + $detail['expire_time']
- ];
- }
- }
- }
-
- return ['exceptions' => $exceptions];
- }
-
- /**
- * 检查规则
- * @param array $detail
- * @param array $eventData
- * @return bool
- */
- private function checkRule(array $detail, array $eventData)
- {
- // 根据规则类型执行不同的检查逻辑
- switch ($detail['rule_type']) {
- case 'frequency':
- return $this->ruleCheckService->checkFrequencyRule($detail, $eventData);
- case 'statistics':
- return $this->ruleCheckService->checkStatisticsRule($detail, $eventData);
- default:
- return false;
- }
- }
-
- /**
- * 处理异常数据
- * @param array $exceptions
- * @param array $eventData
- */
- private function handleExceptions(array $exceptions, array $eventData)
- {
- Db::startTrans();
- try {
- foreach ($exceptions as $exception) {
- // 记录异常记录
- Db::name('fk_exception_records')->insert([
- 'strategy_id' => $exception['strategy_id'],
- 'rule_id' => $exception['rule_id'],
- 'object_type' => $exception['object_type'],
- 'object_value' => $exception['object_value'],
- 'exception_level' => $exception['exception_level'],
- 'expire_time' => $exception['expire_time'],
- 'create_time' => time()
- ]);
-
- // 更新异常对象
- $this->updateExceptionObject($exception);
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- throw $e;
- }
- }
-
- /**
- * 更新异常对象
- * @param array $exception
- */
- private function updateExceptionObject(array $exception)
- {
- $object = Db::name('fk_exception_objects')
- ->where([
- 'object_type' => $exception['object_type'],
- 'object_value' => $exception['object_value']
- ])
- ->find();
-
- if ($object) {
- // 更新异常程度
- $newLevel = min(100, $object['exception_level'] + $exception['exception_level']);
- Db::name('fk_exception_objects')
- ->where('id', $object['id'])
- ->update([
- 'exception_level' => $newLevel,
- 'update_time' => time()
- ]);
- } else {
- // 创建新异常对象
- Db::name('fk_exception_objects')->insert([
- 'object_type' => $exception['object_type'],
- 'object_value' => $exception['object_value'],
- 'exception_level' => $exception['exception_level'],
- 'create_time' => time(),
- 'update_time' => time()
- ]);
- }
- }
-
- /**
- * 清理过期数据
- */
- private function cleanExpiredData()
- {
- $now = time();
-
- // 清理过期异常记录
- Db::name('fk_exception_records')
- ->where('expire_time', '<', $now)
- ->where('expire_time', '>', 0)
- ->delete();
-
- // 更新异常对象异常程度
- $expiredRecords = Db::name('fk_exception_records')
- ->where('expire_time', '<', $now)
- ->where('expire_time', '>', 0)
- ->select();
-
- foreach ($expiredRecords as $record) {
- $object = Db::name('fk_exception_objects')
- ->where([
- 'object_type' => $record['object_type'],
- 'object_value' => $record['object_value']
- ])
- ->find();
-
- if ($object) {
- $newLevel = max(0, $object['exception_level'] - $record['exception_level']);
- Db::name('fk_exception_objects')
- ->where('id', $object['id'])
- ->update([
- 'exception_level' => $newLevel,
- 'update_time' => $now
- ]);
- }
- }
- }
-
- /**
- * 检查风险等级
- * @param array $eventData
- * @return array
- */
- private function checkRiskLevel(array $eventData)
- {
- $riskLevel = 0;
- $isBlocked = false;
- $isRisky = false;
- $msg = '';
-
- // 检查各个维度的异常程度
- $dimensions = ['ip', 'imei', 'member_id'];
- if (isset($eventData['id_card'])) {
- $dimensions[] = 'id_card';
- }
-
- foreach ($dimensions as $dimension) {
- $object = Db::name('fk_exception_objects')
- ->where([
- 'object_type' => $dimension,
- 'object_value' => $eventData[$dimension]
- ])
- ->find();
-
- if ($object) {
- $riskLevel = max($riskLevel, $object['exception_level']);
-
- // 如果异常程度超过80,加入黑名单
- if ($object['exception_level'] >= 80) {
- $this->addToBlackList($dimension, $eventData[$dimension]);
- $isBlocked = true;
- $msg = '账号已被封禁';
- }
- // 如果异常程度超过50,标记为风险
- elseif ($object['exception_level'] >= 50) {
- $isRisky = true;
- $msg = '需要验证';
- }
- }
- }
-
- return [
- 'is_blocked' => $isBlocked,
- 'is_risky' => $isRisky,
- 'level' => $riskLevel,
- 'msg' => $msg
- ];
- }
-
- /**
- * 添加到黑名单
- * @param string $type
- * @param string $value
- */
- private function addToBlackList($type, $value)
- {
- // 检查是否已在黑名单
- $exists = Db::name('fk_roster_lists')
- ->where([
- 'object_type' => $type,
- 'object_value' => $value,
- 'list_type' => 'black'
- ])
- ->find();
-
- if (!$exists) {
- Db::name('fk_roster_lists')->insert([
- 'object_type' => $type,
- 'object_value' => $value,
- 'list_type' => 'black',
- 'create_time' => time(),
- 'status' => 1
- ]);
-
- // 清除缓存
- Cache::rm(self::CACHE_PREFIX . 'blacklist:black:' . $type . ':' . $value);
- }
- }
- }
|