ConfigTab.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\ConfigTab as tabModel;
  11. use think\Db;
  12. class ConfigTab extends Permissions
  13. {
  14. public function index()
  15. {
  16. if ($this->request->isAjax()) {
  17. $post = $this->request->param();
  18. $model = new tabModel();
  19. $data = $model->page($post['page']??0, $post['limit']??15)->order('sort desc')->select();
  20. return array('code' => 0, 'count' => count($data), 'data' => $data);
  21. } else {
  22. return $this->fetch();
  23. }
  24. }
  25. public function publish()
  26. {
  27. $id = $this->request->param('id', 0, 'intval');
  28. $model = new tabModel();
  29. $post = $this->request->post();
  30. if ($this->request->isPost()) {
  31. $validate = new \think\Validate([
  32. ['name', 'require', '分类名称不能为空'],
  33. ]);
  34. if (!$validate->check($post)) {
  35. $this->error('提交失败:' . $validate->getError());
  36. }
  37. }
  38. if ($id > 0) {
  39. $cate = $model->where('id', $id)->find();
  40. if (empty($cate)) {
  41. $this->error('id不正确');
  42. }
  43. if ($this->request->isPost()) {
  44. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  45. $this->error('修改失败');
  46. } else {
  47. $this->success('修改成功', 'index');
  48. }
  49. } else {
  50. $this->assign('cate', $cate);
  51. return $this->fetch();
  52. }
  53. } else {
  54. if ($this->request->isPost()) {
  55. if (false == $model->allowField(true)->save($post)) {
  56. $this->error('添加失败');
  57. } else {
  58. $this->success('添加成功', 'index');
  59. }
  60. } else {
  61. return $this->fetch();
  62. }
  63. }
  64. }
  65. public function delete()
  66. {
  67. if ($this->request->isAjax()) {
  68. $id = $this->request->param('id', 0, 'intval');
  69. if (Db::name('config')->where('tab_id', $id)->select() == null) {
  70. if (false == Db::name('config_tab')->where('id', $id)->delete()) {
  71. $this->error('删除失败');
  72. } else {
  73. $this->success('删除成功', 'index');
  74. }
  75. } else {
  76. $this->error('该标签已经绑定了链接组,请先删除关联');
  77. }
  78. }
  79. }
  80. public function sort()
  81. {
  82. if ($this->request->isPost() && $this->request->has('ids')) {
  83. $post = $this->request->post();
  84. $i = 0;
  85. foreach ($post['ids'] as $k => $id) {
  86. $sort = Db::name('config_tab')->where('id', $id)->value('sort');
  87. $newsort = $post['sorts'][$k]??$sort;
  88. if ($sort != $newsort) {
  89. if (false == Db::name('config_tab')->where('id', $id)->update(['sort' => $newsort])) {
  90. $this->error('更新失败');
  91. } else {
  92. $i++;
  93. }
  94. }
  95. }
  96. $this->success('成功更新' . $i . '个数据', 'index');
  97. } else {
  98. $this->error('无数据更新', 'index');
  99. }
  100. }
  101. public function status()
  102. {
  103. if ($this->request->isPost()) {
  104. $post = $this->request->post();
  105. if (false == Db::name('config_tab')->where('id', $post['id'])->update(['status' => $post['status']])) {
  106. $this->error('设置失败');
  107. } else {
  108. $this->success('设置成功', 'index');
  109. }
  110. }
  111. }
  112. }