IpConfig.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\IpConfig as IpConfigModel;
  5. use app\admin\validate\IpConfigValidate;
  6. use Exception;
  7. use think\facade\Db;
  8. use think\facade\Cache;
  9. class IpConfig extends BaseController
  10. {
  11. /**
  12. * @api {post} /ip/delete IP黑名单 删除
  13. *
  14. */
  15. function delete()
  16. {
  17. Db::startTrans();
  18. try {
  19. $params = (new IpConfigValidate())->goCheck('id');
  20. $count = IpConfigModel::where('id', $params['id'])->delete();
  21. if ($count < 1) throw new Exception('操作失败');
  22. Db::commit();
  23. } catch (Exception $e) {
  24. Db::rollBack();
  25. return $this->error($e->getMessage());
  26. }
  27. //删除缓存
  28. Cache::delete('ip_blacklist');
  29. Cache::delete('ip_whitelist');
  30. return $this->success();
  31. }
  32. /**
  33. * @api {post} /ip/update IP 更新
  34. */
  35. function update()
  36. {
  37. Db::startTrans();
  38. try {
  39. $params = (new IpConfigValidate())->post()->goCheck('edit');
  40. $id = $this->request->param('id',0);
  41. $params['operation_id'] = $this->admin_id;
  42. if (!$id) {
  43. $ip = IpConfigModel::where('ip', $params['ip'])->find();
  44. if ($ip) {
  45. return $this->error('IP已存在');
  46. }
  47. IpConfigModel::create($params);
  48. } else {
  49. $ip = IpConfigModel::where('id', $id)->find();
  50. if (!$ip) {
  51. return $this->error('IP不存在');
  52. }
  53. IpConfigModel::where('id', $id)->update($params);
  54. }
  55. Db::commit();
  56. } catch (Exception $e) {
  57. Db::rollBack();
  58. return $this->error($e->getMessage());
  59. }
  60. //删除缓存
  61. Cache::delete('ip_blacklist');
  62. Cache::delete('ip_whitelist');
  63. return $this->success();
  64. }
  65. /**
  66. * @api {get} /ip/list IP查询
  67. */
  68. function list()
  69. {
  70. try {
  71. $params = $this->request->param();
  72. $page = $params['page'] ?? 1;
  73. $limit = $params['limit'] ?? 15;
  74. $query = IpConfigModel::alias('ip_config')->join('admin', 'admin.id = ip_config.operation_id', 'left');
  75. if (!empty($params['keyword'])) {
  76. $query = $query->where('ip_config.ip', 'like', '%'.$params['keyword'].'%');
  77. }
  78. if (!empty($params['status'])) {
  79. $query = $query->where('ip_config.status', $params['status']);
  80. }
  81. if (!empty($params['type'])) {
  82. $query = $query->where('ip_config.type', $params['type']);
  83. }
  84. $count = $query->count();
  85. $list = $query
  86. ->field(['ip_config.*', 'admin.nickname as operator_name'])
  87. ->order('updated_at','desc')
  88. ->limit($limit)
  89. ->page($page)
  90. ->select();
  91. } catch (Exception $e) {
  92. return $this->error($e->getMessage());
  93. }
  94. return $this->success(['count' => $count, 'list' => $list]);
  95. }
  96. }