1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Service;
- use App\Model\Settings;
- use Illuminate\Support\Facades\Cache;
- class LoginService extends Service
- {
- const ALL = 'all';
- public function loginRule($data){
- // 获取用户的IP地址
- $userIP = $_SERVER['REMOTE_ADDR'];
- // 获取设置的IP地址
- $allowedIPs = $this->allowedIPs();
- // 校验用户IP是否在允许的范围内
- $isValidIP = false;
- if(in_array(self::ALL,$allowedIPs)) {
- $isValidIP = true;
- }else{
- foreach ($allowedIPs as $allowedIP) {
- if (strpos($allowedIP, '/') !== false) {
- // IP段表示法校验
- list($subnet, $mask) = explode('/', $allowedIP);
- if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
- $isValidIP = true;
- break;
- }
- } else {
- // 单个IP地址校验
- if ($allowedIP === $userIP) {
- $isValidIP = true;
- break;
- }
- }
- }
- }
- return $isValidIP;
- }
- public function allowedIPs(){
- $allowedIPs = Settings::where('name','allowedIPs')->first();
- if(empty($allowedIPs) || empty($allowedIPs->value)) return [self::ALL];
- return explode(',',$allowedIPs->value);
- }
- //设置登录错误次数(超过三次)
- public static function errorSetLogin($cacheKey){
- if(Cache::has($cacheKey)){
- $num = Cache::get($cacheKey);
- $num++;
- Cache::put($cacheKey,$num,30);
- if($num >= 3){
- return ['账号密码输入错误3次,30分钟内限制登录!'];
- }else{
- return ['账号密码输入错误第'. $num .'次!'];
- }
- }else{
- Cache::add($cacheKey,1,30);
- return ['密码输入错误!'];
- }
- }
- //判断是否限制登录
- public static function isLoginlimitation($cacheKey){
- if(Cache::has($cacheKey)){
- $num = Cache::get($cacheKey);
- if($num >= 3) return true;
- }
- return false;
- }
- }
|