SensitiveDataLog.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\admin\controller\security;
  3. use app\common\controller\Backend;
  4. use app\admin\model\SensitiveDataLog as SensitiveDataLogModel;
  5. use think\db\exception\PDOException;
  6. use think\facade\Db;
  7. use Exception;
  8. class SensitiveDataLog extends Backend
  9. {
  10. protected $model = null;
  11. // 排除字段
  12. protected $preExcludeFields = [];
  13. protected $quickSearchField = 'sensitive.name';
  14. protected $withJoinTable = ['sensitive', 'admin'];
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. $this->model = new SensitiveDataLogModel();
  19. }
  20. /**
  21. * 查看
  22. */
  23. public function index()
  24. {
  25. $this->request->filter(['strip_tags', 'trim']);
  26. if ($this->request->param('select')) {
  27. $this->select();
  28. }
  29. list($where, $alias, $limit, $order) = $this->queryBuilder();
  30. $res = $this->model
  31. ->withJoin($this->withJoinTable, $this->withJoinType)
  32. ->alias($alias)
  33. ->where($where)
  34. ->order($order)
  35. ->paginate($limit);
  36. foreach ($res->items() as $item) {
  37. $item->id_value = $item['primary_key'] . '=' . $item->id_value;
  38. }
  39. $this->success('', [
  40. 'list' => $res->items(),
  41. 'total' => $res->total(),
  42. 'remark' => get_route_remark(),
  43. ]);
  44. }
  45. public function info($id = null)
  46. {
  47. $row = $this->model
  48. ->withJoin($this->withJoinTable, $this->withJoinType)
  49. ->where('sensitive_data_log.id', $id)
  50. ->find();
  51. if (!$row) {
  52. $this->error(__('Record not found'));
  53. }
  54. $this->success('', [
  55. 'row' => $row
  56. ]);
  57. }
  58. public function rollback($ids = null)
  59. {
  60. $data = $this->model->where('id', 'in', $ids)->select();
  61. if (!$data) {
  62. $this->error(__('Record not found'));
  63. }
  64. $count = 0;
  65. Db::startTrans();
  66. try {
  67. foreach ($data as $row) {
  68. if (Db::name($row->data_table)->where($row->primary_key, $row->id_value)->update([
  69. $row->data_field => $row->before
  70. ])) {
  71. $row->delete();
  72. $count++;
  73. }
  74. }
  75. Db::commit();
  76. } catch (PDOException|Exception $e) {
  77. Db::rollback();
  78. $this->error($e->getMessage());
  79. }
  80. if ($count) {
  81. $this->success();
  82. } else {
  83. $this->error(__('No rows were rollback'));
  84. }
  85. }
  86. }