Config.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Config as cateModel;
  11. use app\common\model\ConfigTab as tabModel;
  12. use think\Db;
  13. class Config extends Permissions
  14. {
  15. public function index()
  16. {
  17. if ($this->request->isAjax()) {
  18. $post = $this->request->param();
  19. $where = [
  20. 'hide' => cateModel::STATUS_SHOW,
  21. ];
  22. if (isset($post['tab_id']) && !empty($post['tab_id'])) {
  23. $where['tab_id'] = $post['tab_id'];
  24. }
  25. if (isset($post['keywords']) and !empty($post['keywords'])) {
  26. $where['name'] = ['like', '%' . $post['keywords'] . '%'];
  27. }
  28. $model = new cateModel();
  29. $count = $model->where($where)->count();
  30. $data = $model->where($where)->page($post['page']??0, $post['limit']??0)->order('sort desc')->select();
  31. foreach ($data as $k => $v) {
  32. $data[$k]['tab_text'] = $v->tab_text;
  33. $data[$k]['type_text'] = $v->type_text;
  34. }
  35. return array('code' => 0, 'count' => $count, 'data' => $data);
  36. } else {
  37. $grouptabs = (new tabModel())->order('sort desc')->select();
  38. $this->assign('tabs', $grouptabs);
  39. return $this->fetch();
  40. }
  41. }
  42. public function index2()
  43. {
  44. if ($this->request->isAjax()) {
  45. $post = $this->request->param();
  46. $where = [
  47. 'hide' => cateModel::STATUS_SHOW,
  48. 'status' => cateModel::STATUS_OPEN
  49. ];
  50. if (isset($post['tab_id']) && !empty($post['tab_id'])) {
  51. $where['tab_id'] = $post['tab_id'];
  52. }
  53. if (isset($post['keywords']) and !empty($post['keywords'])) {
  54. $where['name'] = ['like', '%' . $post['keywords'] . '%'];
  55. }
  56. $model = new cateModel();
  57. $count = $model->where($where)->count();
  58. $data = $model->where($where)->page($post['page']??0, $post['limit']??0)->order('sort desc')->select();
  59. foreach ($data as $k => $v) {
  60. $data[$k]['value_text'] = $v->value_text;
  61. }
  62. return array('code' => 0, 'count' => $count, 'data' => $data);
  63. } else {
  64. $grouptabs = (new tabModel())->order('sort desc')->select();
  65. $this->assign('tabs', $grouptabs);
  66. return $this->fetch();
  67. }
  68. }
  69. public function publish()
  70. {
  71. $id = $this->request->param('id', 0, 'intval');
  72. $model = new cateModel();
  73. $post = $this->request->post();
  74. if ($this->request->isPost()) {
  75. $validate = new \think\Validate([
  76. ['name', 'require', '名称不能为空'],
  77. ]);
  78. if (!$validate->check($post)) {
  79. $this->error('提交失败:' . $validate->getError());
  80. }
  81. } else {
  82. $grouptabs = (new tabModel())->order('sort desc')->select();
  83. $this->assign('tabs', $grouptabs);
  84. $this->assign('types', $model::TYPES);
  85. }
  86. if ($id > 0) {
  87. $cate = $model->where('id', $id)->find();
  88. if (empty($cate)) {
  89. $this->error('id不正确');
  90. }
  91. if ($this->request->isPost()) {
  92. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  93. $this->error('修改失败');
  94. } else {
  95. $this->success('修改成功', 'index');
  96. }
  97. } else {
  98. $this->assign('cate', $cate);
  99. $this->assign('type', $cate->type);
  100. return $this->fetch();
  101. }
  102. } else {
  103. if ($this->request->isPost()) {
  104. if (false == $model->allowField(true)->save($post)) {
  105. $this->error('添加失败');
  106. } else {
  107. $this->success('添加成功', 'index');
  108. }
  109. } else {
  110. $this->assign('type', 0);
  111. return $this->fetch();
  112. }
  113. }
  114. }
  115. public function delete()
  116. {
  117. if ($this->request->isAjax()) {
  118. $id = $this->request->param('id', 0, 'intval');
  119. if (Db::name('config_option')->where('pid', $id)->select() == null) {
  120. if (false == Db::name('config')->where('id', $id)->delete()) {
  121. $this->error('删除失败');
  122. } else {
  123. $this->success('删除成功', 'index');
  124. }
  125. } else {
  126. $this->error('请先删除子配置');
  127. }
  128. }
  129. }
  130. public function sort()
  131. {
  132. if ($this->request->isPost() && $this->request->has('ids')) {
  133. $post = $this->request->post();
  134. $i = 0;
  135. foreach ($post['ids'] as $k => $id) {
  136. $sort = Db::name('config')->where('id', $id)->value('sort');
  137. $newsort = $post['sorts'][$k]??$sort;
  138. if ($sort != $newsort) {
  139. if (false == Db::name('config')->where('id', $id)->update(['sort' => $newsort])) {
  140. $this->error('更新失败');
  141. } else {
  142. $i++;
  143. }
  144. }
  145. }
  146. $this->success('成功更新' . $i . '个数据', 'index');
  147. } else {
  148. $this->error('无数据更新', 'index');
  149. }
  150. }
  151. public function status()
  152. {
  153. if ($this->request->isPost()) {
  154. $post = $this->request->post();
  155. if (false == Db::name('config')->where('id', $post['id'])->update(['status' => $post['status']])) {
  156. $this->error('设置失败');
  157. } else {
  158. $this->success('设置成功', 'index');
  159. }
  160. }
  161. }
  162. }