Config.php 6.9 KB

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