123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wanggb
- * Date: 2018/11/22
- * Time: 16:25
- */
- namespace app\admin\controller\domain;
- use app\common\constants\DomainBlackList;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\common\service\BlacklistService;
- class Blacklist extends Backend
- {
- /**
- * @var \Redis
- */
- protected $redis = null;
- /**
- * @var \think\model\
- */
- protected $model = null;
- /**
- * @var BlacklistService
- */
- protected $blcklistservice = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->redis = Redis::instance();
- $this->model = model('Blacklist');
- $this->blcklistservice = BlacklistService::instance();
- $this->view->assign('typelist', $this->model->getTypeList());
- $this->view->assign('statuslist', $this->model->getStatusList());
- }
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where('status','active')
- ->count();
- $list = $this->model
- ->where($where)
- ->where('status','active')
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 新增
- * @return string
- * @throws \think\Exception
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- $params['domain'] = $this->blcklistservice->formatDomain($params['domain']);
- $type = $params['type'];
- if ($params) {
- try {
- if ($type == DomainBlackList::LOCKALLDOMAIN) {
- // 整个链接
- unset($params['param']);
- unset($params['path']);
- } elseif ($type == DomainBlackList::LOCKDOMAINBYPARAM) {
- unset($params['path']);
- } elseif ($type == DomainBlackList::LOCKDOMAINBYPATH) {
- unset($params['param']);
- }
- $result = $this->model->save($params);
- if ($result) {
- $this->saveRedis($params);
- $this->success();
- }
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- } else {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- }
- return $this->view->fetch();
- }
- /**
- * 保存Redis
- * @param $data
- */
- private function saveRedis($data)
- {
- $type = $data['type'];
- $status = $data['status']??'active';
- $redisKey = DomainBlackList::REDISPREFIX . $data['domain'];
- if ($status == DomainBlackList::STATUSACTIVE) {
- if ($type == DomainBlackList::LOCKALLDOMAIN) {
- $this->redis->hset($redisKey, $data['domain'], $type);
- } elseif ($type == DomainBlackList::LOCKDOMAINBYPARAM) {
- $this->redis->hset($redisKey, $data['param'], $type);
- } elseif ($type == DomainBlackList::LOCKDOMAINBYPATH) {
- $this->redis->hset($redisKey, $data['path'], $type);
- }
- }
- }
- /**
- * 删除
- */
- public function del($ids = NULL)
- {
- if ($ids) {
- $params = $this->model->get($ids);
- $params['domain'] = $this->blcklistservice->formatDomain($params['domain']);
- $redis_key = DomainBlackList::REDISPREFIX . $params['domain'];
- if ($params['type'] == DomainBlackList::LOCKALLDOMAIN) {
- $this->redis->hDel($redis_key, $params['domain']);
- } elseif ($params['type'] == DomainBlackList::LOCKDOMAINBYPARAM) {
- $this->redis->hDel($redis_key, $params['param']);
- } elseif ($params['type'] == DomainBlackList::LOCKDOMAINBYPATH) {
- $this->redis->hDel($redis_key, $params['path']);
- }
- try {
- $result = $this->model->save(['status' => 'inactive'], ['id' => $ids]);
- if ($result !== false) {
- $this->success();
- } else {
- $this->error('删除出错了!');
- }
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 刷新Redis 缓存
- * @return false|string
- */
- public function refreshredis()
- {
- $key = DomainBlackList::REDISPREFIX . "*";
- try {
- $redis = Redis::instance();
- $redis::delScan($key);
- $res = $this->model->where('status', '=', 'active')
- ->select();
- foreach ($res as $val) {
- if ($val['type'] == DomainBlackList::LOCKALLDOMAIN) {
- $redis->hset(DomainBlackList::REDISPREFIX . $val['domain'], $val['domain'], $val['type']);
- } elseif ($val['type'] == DomainBlackList::LOCKDOMAINBYPARAM) {
- $redis->hset(DomainBlackList::REDISPREFIX . $val['domain'], $val['param'], $val['type']);
- } elseif ($val['type'] == DomainBlackList::LOCKDOMAINBYPATH) {
- $redis->hset(DomainBlackList::REDISPREFIX . $val['domain'], $val['path'], $val['type']);
- }
- }
- } catch (\Exception $e) {
- Log::error('刷新域名黑名单缓存失败');
- }
- $arr = ['err' => 1];
- return json_encode($arr);
- }
- }
|