IpConfig.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $params['remark'] = request()->input('remark', '');
  37. if (empty($params['remark'])) $params['remark'] = '';
  38. $params['operator_id'] = $this->admin_id;
  39. IpConfigModel::updateOrCreate(['ip' => $params['ip']], $params);
  40. DB::commit();
  41. } catch (Exception $e) {
  42. DB::rollBack();
  43. return $this->error($e->getMessage());
  44. }
  45. return $this->success();
  46. }
  47. /**
  48. * @api {get} /ip/index IP查询
  49. */
  50. function index()
  51. {
  52. try {
  53. $params = $this->request->param();
  54. $page = $params['page'] ?? 1;
  55. $limit = $params['limit'] ?? 15;
  56. $query = IpConfigModel::join('kefu', 'kefu.id = ip_config.operator_id', 'left');
  57. if (!empty($params['type'])) {
  58. $query->where('ip_config.type', $params['type']);
  59. }
  60. if (!empty($params['status'])) {
  61. $query->where('ip_config.status', $params['status']);
  62. }
  63. if (!empty($params['operator'])) {
  64. $query->where('kefu.name', 'like', '%'.$params['operator'].'%');
  65. }
  66. $count = $query->count();
  67. $list = $query
  68. ->field(['ip_config.*', 'kefu.name as operator_name'])
  69. ->order('updated_at','desc')
  70. ->paginate(['list_rows'=>$limit,'page'=>$page]);
  71. } catch (Exception $e) {
  72. return $this->error($e->getMessage());
  73. }
  74. return $this->success(['count' => $count, 'list' => $list]);
  75. }
  76. }