| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2022/2/9
- * Time: 14:10
- */
- namespace app\platform\modules\system\models;
- use app\common\models\BaseModel;
- /**
- * Class WhiteList
- * @package app\platform\modules\system\models
- * @property string ip
- * @property int is_open
- */
- class WhiteList extends BaseModel
- {
- public $table = 'yz_system_white_list';
- public $timestamps = true;
- protected $guarded = [''];
- /**
- * @param array $search
- * @return WhiteList
- */
- public static function getWhite($search = [])
- {
- $model = new self();
- if ($search['id']) {
- $model = $model->where('id',$search['id']);
- }
- if ($search['ip']) {
- $model = $model->where('ip',$search['ip']);
- }
- if (isset($search['is_open']) && is_numeric($search['is_open'])) {
- $model = $model->where('is_open',$search['is_open']);
- }
- return $model;
- }
- /***
- * @param $ip
- * @return mixed
- */
- public static function checkValidateIP($ip)
- {
- return filter_var($ip, FILTER_VALIDATE_IP);
- }
- public static function addIP($data = [])
- {
- if (!$data || !is_array($data)) {
- throw new \Exception('参数错误');
- }
- $insert = [];
- foreach ($data as $ip) {
- if(!self::checkValidateIP($ip)) {
- throw new \Exception('输入的ip地址不合法');
- }
- $insert[] = [
- 'ip' => $ip,
- 'is_open' => 1, //默认开启
- 'created_at' => time(),
- 'updated_at' => time(),
- ];
- }
- if ($insert) {
- $res = self::insert($insert);
- return $res;
- }
- return false;
- }
- public static function editIP($id,$data)
- {
- if (!$id) {
- throw new \Exception('参数错误');
- }
- $ip = self::getWhite(['id'=>$id])->first();
- if (!$ip) {
- throw new \Exception('未找到该白名单');
- }
- $fill = [];
- //编辑ip
- if ($data['ip'] && !self::checkValidateIP($data['ip'])) {
- throw new \Exception('输入的ip地址不合法');
- }
- $data['ip'] && $fill['ip'] = $data['ip'];
- //编辑状态
- if (isset($data['is_open'])) {
- $fill['is_open'] = $data['is_open'] ? 1 : 0;
- }
- if (!$fill) {
- throw new \Exception('参数错误');
- }
- $ip->fill($fill);
- return $ip->save();
- }
- public static function delIP($id)
- {
- if (!$id) {
- throw new \Exception('参数错误');
- }
- return self::where('id',$id)->delete();
- }
- /**
- * 是否白名单IP
- * @param $ip
- * @return bool
- */
- public static function isWhite($ip)
- {
- return self::getWhite(['ip' => $ip,'is_open' => 1])->count() ? true : false;
- }
- }
|