Category.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\controller\admin\files;
  3. use laytp\controller\Backend;
  4. use laytp\library\Tree;
  5. use think\facade\Config;
  6. /**
  7. * 附件分类管理
  8. */
  9. class Category extends Backend
  10. {
  11. /**
  12. * files_category模型对象
  13. * @var \app\model\files\Category
  14. */
  15. protected $model;
  16. public $hasSoftDel = 1;//是否拥有软删除功能
  17. public $orderRule = ['sort' => 'DESC', 'id' => 'ASC'];
  18. public function initialize()
  19. {
  20. parent::initialize();
  21. $this->model = new \app\model\files\Category();
  22. }
  23. //查看
  24. public function index()
  25. {
  26. global $_W;
  27. $where = $this->buildSearchParams();
  28. $where[] = ['uniacid','=',$_W['uniacid']];
  29. $order = $this->buildOrder();
  30. $sourceData = $this->model->order($order)->where($where);
  31. $isTree = $this->request->param('is_tree');
  32. if ($isTree) {
  33. $menuTreeObj = Tree::instance();
  34. $menuTreeObj->init($sourceData->select()->toArray());
  35. $data = $menuTreeObj->getRootTrees();
  36. } else {
  37. $paging = $this->request->param('paging', false);
  38. if ($paging) {
  39. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  40. $data = $sourceData->paginate($limit)->toArray();
  41. $data['data'] = $this->getSelectedData($data['data']);
  42. } else {
  43. $data = $sourceData->select()->toArray();
  44. }
  45. }
  46. return $this->success('获取成功', $data);
  47. }
  48. //删除
  49. public function del()
  50. {
  51. $ids = $this->request->post('ids');
  52. if (!$ids) {
  53. return $this->error('参数ids不能为空');
  54. }
  55. $sourceData = $this->model->select()->toArray();
  56. $treeLib = Tree::instance();
  57. $treeLib->init($sourceData);
  58. $childIds = $treeLib->getChildIds($ids);
  59. if ($this->model->destroy($childIds)) {
  60. return $this->success('数据删除成功');
  61. } else {
  62. return $this->error('数据删除失败');
  63. }
  64. }
  65. //回收站
  66. public function recycle()
  67. {
  68. global $_W;
  69. $where = $this->buildSearchParams();
  70. $where[] = ['uniacid','=',$_W['uniacid']];
  71. $order = $this->buildOrder();
  72. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  73. $data = $this->model->onlyTrashed()
  74. ->with(['parent'])
  75. ->order($order)->where($where)->paginate($limit)->toArray();
  76. return $this->success('回收站数据获取成功', $data);
  77. }
  78. //设置排序
  79. public function setSort()
  80. {
  81. $id = $this->request->post('id');
  82. $fieldVal = $this->request->post('field_val');
  83. $isRecycle = $this->request->post('is_recycle');
  84. $update['sort'] = $fieldVal;
  85. try {
  86. if ($isRecycle) {
  87. $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
  88. } else {
  89. $updateRes = $this->model->where('id', '=', $id)->update($update);
  90. }
  91. if ($updateRes) {
  92. return $this->success('操作成功');
  93. } else if ($updateRes === 0) {
  94. return $this->success('未作修改');
  95. } else {
  96. return $this->error('操作失败');
  97. }
  98. } catch (\Exception $e) {
  99. return $this->error('数据库异常,操作失败');
  100. }
  101. }
  102. }