Blacklist.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wanggb
  5. * Date: 2018/11/22
  6. * Time: 16:25
  7. */
  8. namespace app\admin\controller\domain;
  9. use app\common\constants\DomainBlackList;
  10. use app\common\controller\Backend;
  11. use app\common\library\Redis;
  12. use app\common\service\BlacklistService;
  13. class Blacklist extends Backend
  14. {
  15. /**
  16. * @var \Redis
  17. */
  18. protected $redis = null;
  19. /**
  20. * @var \think\model\
  21. */
  22. protected $model = null;
  23. /**
  24. * @var BlacklistService
  25. */
  26. protected $blcklistservice = null;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->redis = Redis::instance();
  31. $this->model = model('Blacklist');
  32. $this->blcklistservice = BlacklistService::instance();
  33. $this->view->assign('typelist', $this->model->getTypeList());
  34. $this->view->assign('statuslist', $this->model->getStatusList());
  35. }
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags']);
  40. if ($this->request->isAjax()) {
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->where($where)
  44. ->where('status','active')
  45. ->count();
  46. $list = $this->model
  47. ->where($where)
  48. ->where('status','active')
  49. ->limit($offset, $limit)
  50. ->select();
  51. $result = array("total" => $total, "rows" => $list);
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 新增
  58. * @return string
  59. * @throws \think\Exception
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. $params['domain'] = $this->blcklistservice->formatDomain($params['domain']);
  66. $type = $params['type'];
  67. if ($params) {
  68. try {
  69. if ($type == DomainBlackList::LOCKALLDOMAIN) {
  70. // 整个链接
  71. unset($params['param']);
  72. unset($params['path']);
  73. } elseif ($type == DomainBlackList::LOCKDOMAINBYPARAM) {
  74. unset($params['path']);
  75. } elseif ($type == DomainBlackList::LOCKDOMAINBYPATH) {
  76. unset($params['param']);
  77. }
  78. $result = $this->model->save($params);
  79. if ($result) {
  80. $this->saveRedis($params);
  81. $this->success();
  82. }
  83. } catch (\think\exception\PDOException $e) {
  84. $this->error($e->getMessage());
  85. }
  86. } else {
  87. $this->error(__('Parameter %s can not be empty', ''));
  88. }
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 保存Redis
  94. * @param $data
  95. */
  96. private function saveRedis($data)
  97. {
  98. $type = $data['type'];
  99. $status = $data['status']??'active';
  100. $redisKey = DomainBlackList::REDISPREFIX . $data['domain'];
  101. if ($status == DomainBlackList::STATUSACTIVE) {
  102. if ($type == DomainBlackList::LOCKALLDOMAIN) {
  103. $this->redis->hset($redisKey, $data['domain'], $type);
  104. } elseif ($type == DomainBlackList::LOCKDOMAINBYPARAM) {
  105. $this->redis->hset($redisKey, $data['param'], $type);
  106. } elseif ($type == DomainBlackList::LOCKDOMAINBYPATH) {
  107. $this->redis->hset($redisKey, $data['path'], $type);
  108. }
  109. }
  110. }
  111. /**
  112. * 删除
  113. */
  114. public function del($ids = NULL)
  115. {
  116. if ($ids) {
  117. $params = $this->model->get($ids);
  118. $params['domain'] = $this->blcklistservice->formatDomain($params['domain']);
  119. $redis_key = DomainBlackList::REDISPREFIX . $params['domain'];
  120. if ($params['type'] == DomainBlackList::LOCKALLDOMAIN) {
  121. $this->redis->hDel($redis_key, $params['domain']);
  122. } elseif ($params['type'] == DomainBlackList::LOCKDOMAINBYPARAM) {
  123. $this->redis->hDel($redis_key, $params['param']);
  124. } elseif ($params['type'] == DomainBlackList::LOCKDOMAINBYPATH) {
  125. $this->redis->hDel($redis_key, $params['path']);
  126. }
  127. try {
  128. $result = $this->model->save(['status' => 'inactive'], ['id' => $ids]);
  129. if ($result !== false) {
  130. $this->success();
  131. } else {
  132. $this->error('删除出错了!');
  133. }
  134. } catch (\think\exception\PDOException $e) {
  135. $this->error($e->getMessage());
  136. }
  137. }
  138. $this->error(__('Parameter %s can not be empty', 'ids'));
  139. }
  140. /**
  141. * 刷新Redis 缓存
  142. * @return false|string
  143. */
  144. public function refreshredis()
  145. {
  146. $key = DomainBlackList::REDISPREFIX . "*";
  147. try {
  148. $redis = Redis::instance();
  149. $redis::delScan($key);
  150. $res = $this->model->where('status', '=', 'active')
  151. ->select();
  152. foreach ($res as $val) {
  153. if ($val['type'] == DomainBlackList::LOCKALLDOMAIN) {
  154. $redis->hset(DomainBlackList::REDISPREFIX . $val['domain'], $val['domain'], $val['type']);
  155. } elseif ($val['type'] == DomainBlackList::LOCKDOMAINBYPARAM) {
  156. $redis->hset(DomainBlackList::REDISPREFIX . $val['domain'], $val['param'], $val['type']);
  157. } elseif ($val['type'] == DomainBlackList::LOCKDOMAINBYPATH) {
  158. $redis->hset(DomainBlackList::REDISPREFIX . $val['domain'], $val['path'], $val['type']);
  159. }
  160. }
  161. } catch (\Exception $e) {
  162. Log::error('刷新域名黑名单缓存失败');
  163. }
  164. $arr = ['err' => 1];
  165. return json_encode($arr);
  166. }
  167. }