RecycleBinController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\RecycleBinModel;
  13. use app\admin\model\RouteModel;
  14. use cmf\controller\AdminBaseController;
  15. use think\facade\Db;
  16. use think\Exception;
  17. use think\exception\PDOException;
  18. class RecycleBinController extends AdminBaseController
  19. {
  20. /**
  21. * 回收站
  22. * @adminMenu(
  23. * 'name' => '回收站',
  24. * 'parent' => '',
  25. * 'display'=> false,
  26. * 'hasView'=> true,
  27. * 'order' => 10000,
  28. * 'icon' => '',
  29. * 'remark' => '回收站',
  30. * 'param' => ''
  31. * )
  32. */
  33. public function index()
  34. {
  35. $content = hook_one('admin_recycle_bin_index_view');
  36. if (!empty($content)) {
  37. return $content;
  38. }
  39. $recycleBinModel = new RecycleBinModel();
  40. $list = $recycleBinModel->order('create_time desc')->paginate(10);
  41. // 获取分页显示
  42. $page = $list->render();
  43. $this->assign('page', $page);
  44. $this->assign('list', $list);
  45. return $this->fetch();
  46. }
  47. /**
  48. * 回收站还原
  49. * @adminMenu(
  50. * 'name' => '回收站还原',
  51. * 'parent' => 'index',
  52. * 'display'=> false,
  53. * 'hasView'=> false,
  54. * 'order' => 10000,
  55. * 'icon' => '',
  56. * 'remark' => '回收站还原',
  57. * 'param' => ''
  58. * )
  59. */
  60. public function restore()
  61. {
  62. if ($this->request->isPost()) {
  63. $ids = $this->request->param('ids');
  64. if (empty($ids)) {
  65. $ids = $this->request->param('id');
  66. }
  67. $this->operate($ids, false);
  68. $this->success('还原成功');
  69. }
  70. }
  71. /**
  72. * 回收站彻底删除
  73. * @adminMenu(
  74. * 'name' => '回收站彻底删除',
  75. * 'parent' => 'index',
  76. * 'display'=> false,
  77. * 'hasView'=> false,
  78. * 'order' => 10000,
  79. * 'icon' => '',
  80. * 'remark' => '回收站彻底删除',
  81. * 'param' => ''
  82. * )
  83. */
  84. public function delete()
  85. {
  86. if ($this->request->isPost()) {
  87. $ids = $this->request->param('ids');
  88. if (empty($ids)) {
  89. $ids = $this->request->param('id');
  90. }
  91. $this->operate($ids);
  92. $this->success('删除成功');
  93. }
  94. }
  95. /**
  96. * 清空回收站
  97. * @adminMenu(
  98. * 'name' => '清空回收站',
  99. * 'parent' => 'index',
  100. * 'display'=> false,
  101. * 'hasView'=> false,
  102. * 'order' => 10000,
  103. * 'icon' => '',
  104. * 'remark' => '一键清空回收站',
  105. * 'param' => ''
  106. * )
  107. */
  108. public function clear()
  109. {
  110. if ($this->request->isPost()) {
  111. $this->operate(null);
  112. $this->success('回收站已清空');
  113. }
  114. }
  115. /**
  116. * 统一处理删除、还原
  117. * @param bool $isDelete 是否是删除操作
  118. * @param array $ids 处理的资源id集
  119. */
  120. private function operate($ids, $isDelete = true)
  121. {
  122. if (!empty($ids) && !is_array($ids)) {
  123. $ids = [$ids];
  124. }
  125. $records = RecycleBinModel::select($ids);
  126. if ($records) {
  127. try {
  128. Db::startTrans();
  129. $desIds = [];
  130. foreach ($records as $record) {
  131. $desIds[] = $record['id'];
  132. if ($isDelete) {
  133. // 删除资源
  134. if ($record['table_name'] === 'portal_post#page') {
  135. // 页面没有单独的表,需要单独处理
  136. Db::name('portal_post')->delete($record['object_id']);
  137. // 消除路由
  138. $routeModel = new RouteModel();
  139. $routeModel->setRoute('', 'portal/Page/index', ['id' => $record['object_id']], 2, 5000);
  140. $routeModel->getRoutes(true);
  141. } else {
  142. Db::name($record['table_name'])->delete($record['object_id']);
  143. }
  144. // 如果是文章表,删除相关数据
  145. if ($record['table_name'] === 'portal_post') {
  146. Db::name('portal_category_post')->where('post_id', '=', $record['object_id'])->delete();
  147. Db::name('portal_tag_post')->where('post_id', '=', $record['object_id'])->delete();
  148. }
  149. } else {
  150. // 还原资源
  151. $tableNameArr = explode('#', $record['table_name']);
  152. $tableName = $tableNameArr[0];
  153. $result = Db::name($tableName)->where('id', '=', $record['object_id'])->update(['delete_time' => '0']);
  154. if ($result) {
  155. if ($tableName === 'portal_post') {
  156. Db::name('portal_category_post')->where('post_id', '=', $record['object_id'])->update(['status' => 1]);
  157. Db::name('portal_tag_post')->where('post_id', '=', $record['object_id'])->update(['status' => 1]);
  158. }
  159. }
  160. }
  161. }
  162. // 删除回收站数据
  163. RecycleBinModel::destroy($desIds);
  164. Db::commit();
  165. } catch (PDOException $e) {
  166. Db::rollback();
  167. $this->error('数据库错误', $e->getMessage());
  168. } catch (Exception $e) {
  169. Db::rollback();
  170. $this->error($isDelete ? '删除' : '还原' . '失败', $e->getMessage());
  171. }
  172. }
  173. }
  174. }