SystemConfigTab.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use think\facade\Route as Url;
  4. use app\admin\controller\AuthController;
  5. use crmeb\services\{
  6. FormBuilder as Form, UtilService as Util, JsonService as Json
  7. };
  8. use app\admin\model\system\{
  9. SystemConfig as ConfigModel, SystemConfigTab as ConfigTabModel
  10. };
  11. /**
  12. * 配置分类控制器
  13. * Class SystemConfigTab
  14. * @package app\admin\controller\system
  15. */
  16. class SystemConfigTab extends AuthController
  17. {
  18. /** 定义配置分类,需要添加分类可以手动添加
  19. * @return array
  20. */
  21. public function getConfigType()
  22. {
  23. return [
  24. ['value' => 0, 'label' => '系统']
  25. , ['value' => 1, 'label' => '应用']
  26. , ['value' => 2, 'label' => '支付']
  27. , ['value' => 3, 'label' => '其它']
  28. ];
  29. }
  30. /**
  31. * 子子段
  32. * @return mixed|\think\response\Json
  33. */
  34. public function sonconfigtab()
  35. {
  36. $tab_id = input('tab_id');
  37. if (!$tab_id) return Json::fail('参数错误');
  38. $this->assign('tab_id', $tab_id);
  39. $list = ConfigModel::getAll($tab_id, 2);
  40. foreach ($list as $k => $v) {
  41. $list[$k]['value'] = json_decode($v['value'], true) ?: '';
  42. if ($v['type'] == 'radio' || $v['type'] == 'checkbox') {
  43. $list[$k]['value'] = ConfigTabModel::getRadioOrCheckboxValueInfo($v['menu_name'], $v['value']);
  44. }
  45. if ($v['type'] == 'upload' && !empty($v['value'])) {
  46. if ($v['upload_type'] == 1 || $v['upload_type'] == 3) $list[$k]['value'] = explode(',', $v['value']);
  47. }
  48. }
  49. $this->assign('list', $list);
  50. return $this->fetch();
  51. }
  52. /**
  53. * 基础配置
  54. * @return mixed
  55. */
  56. public function index()
  57. {
  58. $where = Util::getMore([
  59. ['status', ''],
  60. ['title', ''],
  61. ['pid', 0],
  62. ], $this->request);
  63. $this->assign('where', $where);
  64. $this->assign(ConfigTabModel::getSystemConfigTabPage($where));
  65. return $this->fetch();
  66. }
  67. /**
  68. * 添加配置分类
  69. * @return mixed
  70. */
  71. public function create()
  72. {
  73. $field = [];
  74. $field[] = Form::select('pid', '父级分类', 0)->setOptions(function () {
  75. $menuList = ConfigTabModel::field(['id', 'pid', 'title'])->select()->toArray();//var_dump($menuList);
  76. $list = sort_list_tier($menuList, '顶级', 'pid', 'id');//var_dump($list);
  77. $menus = [['value' => 0, 'label' => '顶级按钮']];
  78. foreach ($list as $menu) {
  79. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  80. }
  81. return $menus;
  82. })->filterable(1);
  83. $field[] = Form::input('title', '分类名称');
  84. $field[] = Form::input('eng_title', '分类字段英文');
  85. $field[] = Form::frameInputOne('icon', '图标', Url::buildUrl('admin/widget.widgets/icon', array('fodder' => 'icon')))->icon('ionic')->height('500px');
  86. $field[] = Form::radio('type', '类型', 0)->options(self::getConfigType());
  87. $field[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  88. $field[] = Form::radio('type', '类型', 0)->options(self::getConfigType());
  89. $field[] = Form::number('sort', '排序', 0);
  90. $form = Form::make_post_form('添加分类配置', $field, Url::buildUrl('save'), 3);
  91. $form->setMethod('post')->setTitle('添加分类配置');
  92. $this->assign(compact('form'));
  93. return $this->fetch('public/form-builder');
  94. }
  95. /**
  96. * 保存分类名称
  97. */
  98. public function save()
  99. {
  100. $data = Util::postMore([
  101. 'eng_title',
  102. 'status',
  103. 'title',
  104. 'pid',
  105. 'icon',
  106. 'type',
  107. ['sort', 0]
  108. ]);
  109. if (!$data['title']) return Json::fail('请输入按钮名称');
  110. ConfigTabModel::create($data);
  111. return Json::successful('添加菜单成功!');
  112. }
  113. /**
  114. * 修改分类
  115. * @param $id
  116. * @return string|void
  117. * @throws \FormBuilder\exception\FormBuilderException
  118. */
  119. public function edit($id)
  120. {
  121. $menu = ConfigTabModel::get($id)->getData();
  122. if (!$menu) return Json::fail('数据不存在!');
  123. $form = Form::create(Url::buildUrl('update', array('id' => $id)), [
  124. Form::select('pid', '父级分类', (string)$menu['pid'])->setOptions(function () {
  125. $menuList = ConfigTabModel::field(['id', 'pid', 'title'])->select()->toArray();
  126. $list = sort_list_tier($menuList, '顶级', 'pid', 'id');
  127. $options = [['value' => 0, 'label' => '顶级按钮']];
  128. foreach ($list as $option) {
  129. $options[] = ['value' => $option['id'], 'label' => $option['html'] . $option['title']];
  130. }
  131. return $options;
  132. })->filterable(1),
  133. Form::input('title', '分类名称', $menu['title']),
  134. Form::input('eng_title', '分类字段英文', $menu['eng_title']),
  135. Form::frameInputOne('icon', '图标', Url::buildUrl('admin/widget.widgets/icon', array('fodder' => 'icon')), $menu['icon'])->icon('ionic')->height('500px'),
  136. Form::radio('type', '类型', $menu['type'])->options(self::getConfigType()),
  137. Form::radio('status', '状态', $menu['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]),
  138. Form::number('sort', '排序', $menu['sort'] ?? 0),
  139. ]);
  140. $form->setMethod('post')->setTitle('添加分类配置');
  141. $this->assign(compact('form'));
  142. return $this->fetch('public/form-builder');
  143. }
  144. /**
  145. * @param $id
  146. */
  147. public function update($id)
  148. {
  149. $data = Util::postMore(['title', 'pid', 'status', 'eng_title', 'icon', 'type','sort']);
  150. if (!$data['title']) return Json::fail('请输入分类昵称');
  151. if (!$data['eng_title']) return Json::fail('请输入分类字段');
  152. if (!ConfigTabModel::get($id)) return Json::fail('编辑的记录不存在!');
  153. ConfigTabModel::edit($data, $id);
  154. return Json::successful('修改成功!');
  155. }
  156. /**
  157. * @param $id
  158. */
  159. public function delete($id)
  160. {
  161. if (!$id) return Json::fail('参数有误!');
  162. if (ConfigTabModel::be(['pid' => $id])) return Json::fail('有子分类,不能直接删除');
  163. if (!ConfigTabModel::del($id))
  164. return Json::fail(ConfigTabModel::getErrorInfo('删除失败,请稍候再试!'));
  165. else
  166. return Json::successful('删除成功!');
  167. }
  168. }