Page.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller\shopro\data;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. /**
  6. * 前端路由
  7. */
  8. class Page extends Common
  9. {
  10. protected $noNeedRight = ['select'];
  11. /**
  12. * Page模型对象
  13. * @var \app\admin\model\shopro\data\Page
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\shopro\data\Page;
  20. }
  21. /**
  22. * 查看
  23. *
  24. * @return string|Json
  25. * @throws \think\Exception
  26. * @throws DbException
  27. */
  28. public function index()
  29. {
  30. if (!$this->request->isAjax()) {
  31. return $this->view->fetch();
  32. }
  33. $list = $this->model->sheepFilter()->paginate($this->request->request('list_rows', 10));
  34. $this->success('', null, $list);
  35. }
  36. public function select()
  37. {
  38. if (!$this->request->isAjax()) {
  39. return $this->view->fetch();
  40. }
  41. $list = $this->model->group('group')->with(['children'])->field('group')->select();
  42. $this->success('', null, $list);
  43. }
  44. /**
  45. * 添加
  46. */
  47. public function add()
  48. {
  49. if (!$this->request->isAjax()) {
  50. return $this->view->fetch();
  51. }
  52. $params = $this->request->only(['name', 'path', 'group']);
  53. $this->svalidate($params, '.add');
  54. $this->model->save($params);
  55. $this->success('保存成功', null, $this->model);
  56. }
  57. /**
  58. * 详情
  59. *
  60. * @param $id
  61. * @return \think\Response
  62. */
  63. public function detail($id)
  64. {
  65. $detail = $this->model->where('id', $id)->find();
  66. if (!$detail) {
  67. $this->error(__('No Results were found'));
  68. }
  69. $this->success('获取成功', null, $detail);
  70. }
  71. /**
  72. * 编辑(支持批量)
  73. */
  74. public function edit($id = null)
  75. {
  76. if (!$this->request->isAjax()) {
  77. return $this->view->fetch('add');
  78. }
  79. $params = $this->request->only(['name', 'path', 'group']);
  80. $list = $this->model->where('id', 'in', $id)->select();
  81. $result = Db::transaction(function () use ($list, $params) {
  82. $count = 0;
  83. foreach ($list as $item) {
  84. $params['id'] = $item->id;
  85. $this->svalidate($params);
  86. $count += $item->save($params);
  87. }
  88. return $count;
  89. });
  90. if ($result) {
  91. $this->success('更新成功', null, $result);
  92. } else {
  93. $this->error('更新失败,未改变任何记录');
  94. }
  95. }
  96. /**
  97. * 删除(支持批量)
  98. *
  99. * @param $id
  100. * @return \think\Response
  101. */
  102. public function delete($id)
  103. {
  104. if (empty($id)) {
  105. $this->error(__('Parameter %s can not be empty', 'id'));
  106. }
  107. $list = $this->model->where('id', 'in', $id)->select();
  108. $result = Db::transaction(function () use ($list) {
  109. $count = 0;
  110. foreach ($list as $item) {
  111. $count += $item->delete();
  112. }
  113. return $count;
  114. });
  115. if ($result) {
  116. $this->success('删除成功', null, $result);
  117. } else {
  118. $this->error(__('No rows were deleted'));
  119. }
  120. }
  121. }