Index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @version:
  5. * @Author: xiaoyu5062
  6. * @QQ: 170515071
  7. * @Email: xiaoyu5062@qq.com
  8. * @Date: 2020-07-25 10:01:48
  9. * @LastEditors: xiaoyu5062
  10. * @LastEditTime: 2025-02-19 17:02:04
  11. */
  12. namespace app\admin\controller\apilog;
  13. use app\common\controller\Backend;
  14. use think\Cache;
  15. use think\Db;
  16. class Index extends Backend
  17. {
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \addons\apilog\model\Apilog;
  23. $this->view->assign("methodList", $this->model->getMethodList());
  24. }
  25. public function index()
  26. {
  27. $this->request->filter(['strip_tags']);
  28. if ($this->request->isAjax()) {
  29. if ($this->request->request('keyField')) {
  30. return $this->selectpage();
  31. }
  32. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  33. $total = $this->model
  34. ->where($where)
  35. ->order($sort, $order)
  36. ->count();
  37. $list = $this->model
  38. ->where($where)
  39. ->order($sort, $order)
  40. ->limit($offset, $limit)
  41. ->select();
  42. foreach ($list as $k => $v) {
  43. $v['banip'] = Cache::has('banip:' . $v['ip']);
  44. }
  45. $list = collection($list)->toArray();
  46. $result = array("total" => $total, "rows" => $list);
  47. return json($result);
  48. }
  49. return $this->view->fetch();
  50. }
  51. public function detail($ids)
  52. {
  53. $row = $this->model->get(['id' => $ids]);
  54. if (!$row)
  55. $this->error(__('No Results were found'));
  56. $this->view->assign("row", $row->toArray());
  57. return $this->view->fetch();
  58. }
  59. public function banip($status, $ip, $time = 0)
  60. {
  61. if ($status == 0) {
  62. Cache::set('banip:' . $ip, 1, $time * 60);
  63. } else {
  64. Cache::rm('banip:' . $ip);
  65. }
  66. $this->success('succ', null, Cache::has('banip:' . $ip));
  67. }
  68. public function clear()
  69. {
  70. $tableName = $this->model->getTable();
  71. try {
  72. // 尝试使用 TRUNCATE 语句清空数据表
  73. Db::execute("TRUNCATE TABLE {$tableName}");
  74. $this->success('清空成功');
  75. } catch (\Exception $e) {
  76. // TRUNCATE 失败,捕获异常并尝试使用 DELETE 语句
  77. try {
  78. // 开启事务
  79. Db::startTrans();
  80. // 执行 DELETE 操作清空表数据
  81. Db::name($tableName)->delete(true);
  82. // 提交事务
  83. Db::commit();
  84. $this->success('清空成功');
  85. } catch (\Exception $deleteException) {
  86. // DELETE 也失败,回滚事务并返回错误信息
  87. Db::rollback();
  88. $this->error('清空失败:'+ $deleteException->getMessage());
  89. }
  90. }
  91. }
  92. }