* @version : Beibao 1.0 */ namespace ban\model; use ban\BanConst; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use think\Cache; class IpBanModel extends CommonModel { protected $name = 'ip_ban'; private $cache_prefix = CacheConst::CACHE_IP_BAN_PREFIX; /** * 加入数据库 * * @param $data * * @return bool|mixed */ public function addData($data) { $_data = $data; $_data['ip'] = get_val($data, 'ip', ''); if (empty($_data['ip'])) { return false; } $_data['reg_ban'] = get_val($data, 'reg_ban', 2); $_data['login_ban'] = get_val($data, 'login_ban', 2); $_data['access_ban'] = get_val($data, 'access_ban', 2); $_data['create_time'] = time(); $_data['update_time'] = $_data['create_time']; if ($_obj = self::create($_data, true)) { $_bdata = ['is_ban' => BanConst::IS_BAN]; $_ip = $_data['ip']; $_cache_key = $this->cache_prefix.md5($_ip); Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS); return $_obj->id; } else { return false; } } /** * 更新数据 * * @param array $data * @param int $ip * * @return bool|mixed */ public function updateData($data, $ip) { $_map['ip'] = $ip; $_data = $data; $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } else { $_bdata = ['is_ban' => BanConst::IS_BAN]; $_cache_key = $this->cache_prefix.md5($ip); Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS); return true; } } /** * 获取数据详情 * * @param int $ip * * @return array|false */ public function getDetail($ip) { $_map['ip'] = $ip; $_data = $this->where($_map)->find(); if (false == $_data) { return false; } if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { $_bdata = ['is_ban' => BanConst::IS_BAN]; $_cache_key = $this->cache_prefix.md5($ip); Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS); } return $_data; } /***** * 删除数据 * * @param $ip * * @return int */ public function deleteData($ip, $is_complete = false) { $_map['ip'] = $ip; $_rs = self::where($_map)->delete(); if (false != $_rs) { $_bdata = ['is_ban' => BanConst::NO_BAN]; $_cache_key = $this->cache_prefix.md5($ip); Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS); } return $_rs; } /** * 判断是否封禁 * * @param $ip * * @return bool */ public function isBan($ip) { $_cache_key = $this->cache_prefix.md5($ip); $_cdata = Cache::get($_cache_key); if (empty($_cdata)) { $_data = $this->getDetail($ip); if (!empty($_data)) { return true; } return false; } if (BanConst::IS_BAN == $_cdata['is_ban']) { return true; } return false; } /** * 获取缓存KEY * * @param string $ip IP地址 * * @return string */ public function getCacheKeyByIp($ip) { $_ip = str_replace(".", "_", $ip); return $this->cache_prefix.'ip_'.$_ip; } /** * 根据IP获取id * * @param $ip * * @return mixed */ public function getIdByIp($ip) { $_cache_key = $this->getCacheKeyByIp($ip); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } $_map = ['ip' => $ip]; $_id = $this->where($_map)->value('id'); if (!empty($_id)) { Cache::set($_cache_key, $_id); } return $_id; } /** * 根据IP获取数据 * * @param string $ip * * @return array */ public function getInfoByIp($ip) { $_id = $this->getIdByIp($ip); if (empty($_id)) { return []; } return $this->getInfoById($_id); } /** * 查询切换状态 * * @param string $ip IP地址 * * @return int */ public function getPaySwitch($ip) { $_data = $this->getInfoByIp($ip); return get_val($_data, 'pay_switch', 0); } }