IpBanModel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * IpBanModel.php UTF-8
  4. * IP封禁表
  5. *
  6. * @date : 2018/9/7 14:24
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : Beibao 1.0
  11. */
  12. namespace ban\model;
  13. use ban\BanConst;
  14. use huo\model\common\CommonModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use think\Cache;
  18. class IpBanModel extends CommonModel {
  19. protected $name = 'ip_ban';
  20. private $cache_prefix = CacheConst::CACHE_IP_BAN_PREFIX;
  21. /**
  22. * 加入数据库
  23. *
  24. * @param $data
  25. *
  26. * @return bool|mixed
  27. */
  28. public function addData($data) {
  29. $_data = $data;
  30. $_data['ip'] = get_val($data, 'ip', '');
  31. if (empty($_data['ip'])) {
  32. return false;
  33. }
  34. $_data['reg_ban'] = get_val($data, 'reg_ban', 2);
  35. $_data['login_ban'] = get_val($data, 'login_ban', 2);
  36. $_data['access_ban'] = get_val($data, 'access_ban', 2);
  37. $_data['create_time'] = time();
  38. $_data['update_time'] = $_data['create_time'];
  39. if ($_obj = self::create($_data, true)) {
  40. $_bdata = ['is_ban' => BanConst::IS_BAN];
  41. $_ip = $_data['ip'];
  42. $_cache_key = $this->cache_prefix.md5($_ip);
  43. Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS);
  44. return $_obj->id;
  45. } else {
  46. return false;
  47. }
  48. }
  49. /**
  50. * 更新数据
  51. *
  52. * @param array $data
  53. * @param int $ip
  54. *
  55. * @return bool|mixed
  56. */
  57. public function updateData($data, $ip) {
  58. $_map['ip'] = $ip;
  59. $_data = $data;
  60. $_rs = self::update($_data, $_map, true);
  61. if (false == $_rs) {
  62. return false;
  63. } else {
  64. $_bdata = ['is_ban' => BanConst::IS_BAN];
  65. $_cache_key = $this->cache_prefix.md5($ip);
  66. Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS);
  67. return true;
  68. }
  69. }
  70. /**
  71. * 获取数据详情
  72. *
  73. * @param int $ip
  74. *
  75. * @return array|false
  76. */
  77. public function getDetail($ip) {
  78. $_map['ip'] = $ip;
  79. $_data = $this->where($_map)->find();
  80. if (false == $_data) {
  81. return false;
  82. }
  83. if (is_object($_data)) {
  84. $_data = $_data->toArray();
  85. }
  86. if (!empty($_data)) {
  87. $_bdata = ['is_ban' => BanConst::IS_BAN];
  88. $_cache_key = $this->cache_prefix.md5($ip);
  89. Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS);
  90. }
  91. return $_data;
  92. }
  93. /*****
  94. * 删除数据
  95. *
  96. * @param $ip
  97. *
  98. * @return int
  99. */
  100. public function deleteData($ip, $is_complete = false) {
  101. $_map['ip'] = $ip;
  102. $_rs = self::where($_map)->delete();
  103. if (false != $_rs) {
  104. $_bdata = ['is_ban' => BanConst::NO_BAN];
  105. $_cache_key = $this->cache_prefix.md5($ip);
  106. Cache::set($_cache_key, $_bdata, CommonConst::CONST_DAY_SECONDS);
  107. }
  108. return $_rs;
  109. }
  110. /**
  111. * 判断是否封禁
  112. *
  113. * @param $ip
  114. *
  115. * @return bool
  116. */
  117. public function isBan($ip) {
  118. $_cache_key = $this->cache_prefix.md5($ip);
  119. $_cdata = Cache::get($_cache_key);
  120. if (empty($_cdata)) {
  121. $_data = $this->getDetail($ip);
  122. if (!empty($_data)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. if (BanConst::IS_BAN == $_cdata['is_ban']) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * 获取缓存KEY
  134. *
  135. * @param string $ip IP地址
  136. *
  137. * @return string
  138. */
  139. public function getCacheKeyByIp($ip) {
  140. $_ip = str_replace(".", "_", $ip);
  141. return $this->cache_prefix.'ip_'.$_ip;
  142. }
  143. /**
  144. * 根据IP获取id
  145. *
  146. * @param $ip
  147. *
  148. * @return mixed
  149. */
  150. public function getIdByIp($ip) {
  151. $_cache_key = $this->getCacheKeyByIp($ip);
  152. $_id = Cache::get($_cache_key);
  153. if (!empty($_id)) {
  154. return $_id;
  155. }
  156. $_map = ['ip' => $ip];
  157. $_id = $this->where($_map)->value('id');
  158. if (!empty($_id)) {
  159. Cache::set($_cache_key, $_id);
  160. }
  161. return $_id;
  162. }
  163. /**
  164. * 根据IP获取数据
  165. *
  166. * @param string $ip
  167. *
  168. * @return array
  169. */
  170. public function getInfoByIp($ip) {
  171. $_id = $this->getIdByIp($ip);
  172. if (empty($_id)) {
  173. return [];
  174. }
  175. return $this->getInfoById($_id);
  176. }
  177. /**
  178. * 查询切换状态
  179. *
  180. * @param string $ip IP地址
  181. *
  182. * @return int
  183. */
  184. public function getPaySwitch($ip) {
  185. $_data = $this->getInfoByIp($ip);
  186. return get_val($_data, 'pay_switch', 0);
  187. }
  188. }