12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Service;
- use App\Model\Employee;
- use App\Model\Settings;
- use Illuminate\Support\Facades\Cache;
- class LoginService extends Service
- {
- const ALL = 'all';
- const LOGIN_ERROR = "LoginError";
- 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($account){
- $cacheKey = self::LOGIN_ERROR . $account;
- if(Cache::has($cacheKey)){
- $num = Cache::get($cacheKey);
- $num++;
- Cache::put($cacheKey,$num,1800);
- if($num >= 3){
- Employee::where('account',$account)->update([
- 'state' => Employee::NOT_USE
- ]);
- Cache::forget($cacheKey);
- return '账号密码输入错误3次,账号停用!';
- }else{
- return '账号密码输入错误第'. $num .'次!';
- }
- }else{
- Cache::add($cacheKey,1,1800);
- return '密码输入错误!';
- }
- }
- //判断是否限制登录
- public static function isLoginlimitation($cacheKey){
- if(Cache::has($cacheKey)){
- $num = Cache::get($cacheKey);
- if($num >= 3) return true;
- }
- return false;
- }
- }
|