WhiteList.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/2/9
  8. * Time: 14:10
  9. */
  10. namespace app\platform\modules\system\models;
  11. use app\common\models\BaseModel;
  12. /**
  13. * Class WhiteList
  14. * @package app\platform\modules\system\models
  15. * @property string ip
  16. * @property int is_open
  17. */
  18. class WhiteList extends BaseModel
  19. {
  20. public $table = 'yz_system_white_list';
  21. public $timestamps = true;
  22. protected $guarded = [''];
  23. /**
  24. * @param array $search
  25. * @return WhiteList
  26. */
  27. public static function getWhite($search = [])
  28. {
  29. $model = new self();
  30. if ($search['id']) {
  31. $model = $model->where('id',$search['id']);
  32. }
  33. if ($search['ip']) {
  34. $model = $model->where('ip',$search['ip']);
  35. }
  36. if (isset($search['is_open']) && is_numeric($search['is_open'])) {
  37. $model = $model->where('is_open',$search['is_open']);
  38. }
  39. return $model;
  40. }
  41. /***
  42. * @param $ip
  43. * @return mixed
  44. */
  45. public static function checkValidateIP($ip)
  46. {
  47. return filter_var($ip, FILTER_VALIDATE_IP);
  48. }
  49. public static function addIP($data = [])
  50. {
  51. if (!$data || !is_array($data)) {
  52. throw new \Exception('参数错误');
  53. }
  54. $insert = [];
  55. foreach ($data as $ip) {
  56. if(!self::checkValidateIP($ip)) {
  57. throw new \Exception('输入的ip地址不合法');
  58. }
  59. $insert[] = [
  60. 'ip' => $ip,
  61. 'is_open' => 1, //默认开启
  62. 'created_at' => time(),
  63. 'updated_at' => time(),
  64. ];
  65. }
  66. if ($insert) {
  67. $res = self::insert($insert);
  68. return $res;
  69. }
  70. return false;
  71. }
  72. public static function editIP($id,$data)
  73. {
  74. if (!$id) {
  75. throw new \Exception('参数错误');
  76. }
  77. $ip = self::getWhite(['id'=>$id])->first();
  78. if (!$ip) {
  79. throw new \Exception('未找到该白名单');
  80. }
  81. $fill = [];
  82. //编辑ip
  83. if ($data['ip'] && !self::checkValidateIP($data['ip'])) {
  84. throw new \Exception('输入的ip地址不合法');
  85. }
  86. $data['ip'] && $fill['ip'] = $data['ip'];
  87. //编辑状态
  88. if (isset($data['is_open'])) {
  89. $fill['is_open'] = $data['is_open'] ? 1 : 0;
  90. }
  91. if (!$fill) {
  92. throw new \Exception('参数错误');
  93. }
  94. $ip->fill($fill);
  95. return $ip->save();
  96. }
  97. public static function delIP($id)
  98. {
  99. if (!$id) {
  100. throw new \Exception('参数错误');
  101. }
  102. return self::where('id',$id)->delete();
  103. }
  104. /**
  105. * 是否白名单IP
  106. * @param $ip
  107. * @return bool
  108. */
  109. public static function isWhite($ip)
  110. {
  111. return self::getWhite(['ip' => $ip,'is_open' => 1])->count() ? true : false;
  112. }
  113. }