'用户黑名单', self::TYPE_IP => 'IP黑名单', ]; // 检查用户是否在黑名单 public static function checkUser($user_id = null, $phone = null) { $blackList = self::query()->where('type',self::TYPE_USER); if ($user_id) { $blackList = $blackList->where('user_id', $user_id); } if ($phone) { if($user_id){ $blackList = $blackList->orWhere('phone', $phone); }else{ $blackList = $blackList->where('phone', $phone); } } return $blackList->exists(); } // 检查IP是否在黑名单 public static function checkIp($ip = null) { // 先从缓存中查找黑名单 $blacklist = Cache::get('ip_blacklist'); // 如果缓存不存在,则从数据库获取并更新缓存 if (!$blacklist) { $blacklist = self::updateCache(); } // 检查IP是否在黑名单中 return in_array($ip, $blacklist); } // ip加入黑名单 public static function addIp($ip = null) { if (!$ip) return false; // 更新数据库 $info = self::query()->where('type', self::TYPE_IP)->where('ip', $ip)->first(); if (!$info){ self::create([ 'type' => self::TYPE_IP, 'ip' => $ip, ]); } // 更新缓存 self::updateCache(); return true; } // 更新缓存 public static function updateCache() { $blacklist = self::query()->where('type', self::TYPE_IP)->pluck('ip')->toArray(); Cache::put('ip_blacklist', $blacklist, now()->addMinutes(60)); return $blacklist; } }