DataRecycleLog.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\admin\controller\security;
  3. use app\common\controller\Backend;
  4. use app\admin\model\DataRecycleLog as DataRecycleLogModel;
  5. use think\db\exception\PDOException;
  6. use Exception;
  7. use think\facade\Db;
  8. class DataRecycleLog extends Backend
  9. {
  10. protected $model = null;
  11. // 排除字段
  12. protected $preExcludeFields = [];
  13. protected $quickSearchField = 'recycle.name';
  14. protected $withJoinTable = ['recycle', 'admin'];
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. $this->model = new DataRecycleLogModel();
  19. }
  20. public function restore($ids = null)
  21. {
  22. $data = $this->model->where('id', 'in', $ids)->select();
  23. if (!$data) {
  24. $this->error(__('Record not found'));
  25. }
  26. $count = 0;
  27. Db::startTrans();
  28. try {
  29. foreach ($data as $row) {
  30. $recycleData = json_decode($row->data, true);
  31. if (is_array($recycleData) && Db::name($row->data_table)->insert($recycleData)) {
  32. $row->delete();
  33. $count++;
  34. }
  35. }
  36. Db::commit();
  37. } catch (PDOException|Exception $e) {
  38. Db::rollback();
  39. $this->error($e->getMessage());
  40. }
  41. if ($count) {
  42. $this->success();
  43. } else {
  44. $this->error(__('No rows were restore'));
  45. }
  46. }
  47. public function info($id = null)
  48. {
  49. $row = $this->model
  50. ->withJoin($this->withJoinTable, $this->withJoinType)
  51. ->where('data_recycle_log.id', $id)
  52. ->find();
  53. if (!$row) {
  54. $this->error(__('Record not found'));
  55. }
  56. $data = $this->jsonToArray($row->data);
  57. if (is_array($data)) {
  58. foreach ($data as $key => $item) {
  59. $data[$key] = $this->jsonToArray($item);
  60. }
  61. }
  62. $row->data = $data;
  63. $this->success('', [
  64. 'row' => $row
  65. ]);
  66. }
  67. protected function jsonToArray($value = '')
  68. {
  69. if (!is_string($value)) {
  70. return $value;
  71. }
  72. $data = json_decode($value, true);
  73. if (($data && is_object($data)) || (is_array($data) && !empty($data))) {
  74. return $data;
  75. }
  76. return $value;
  77. }
  78. }