IpConfig.php 2.9 KB

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