123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- /**
- * IpBanModel.php UTF-8
- * IP封禁表
- *
- * @date : 2018/9/7 14:24
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @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);
- }
- }
|