SystemConfig.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use think\facade\Route as Url;
  4. use app\admin\controller\AuthController;
  5. use app\admin\model\system\{
  6. SystemConfig as ConfigModel, SystemConfigTab as ConfigTabModel
  7. };
  8. use crmeb\services\{
  9. CacheService,
  10. FormBuilder as Form,
  11. UtilService as Util,
  12. JsonService as Json
  13. };
  14. use crmeb\services\upload\Upload;
  15. /**
  16. * 配置列表控制器
  17. * Class SystemConfig
  18. * @package app\admin\controller\system
  19. */
  20. class SystemConfig extends AuthController
  21. {
  22. /**
  23. * 基础配置
  24. * */
  25. public function index()
  26. {
  27. [$type, $tab_id, $children_tab_id] = Util::getMore([
  28. ['type', 0],//配置类型
  29. ['tab_id', 1],//当前分类ID
  30. ['children_tab_id', null],//当前子集分类ID
  31. ], null, true);
  32. $config_tab = null;//顶级分类
  33. $children_config_tab = null;//二级分类
  34. if ($type == 3) {//其它分类
  35. $config_tab = null;
  36. } else {
  37. $config_tab = ConfigModel::getConfigTabAll($type);//获取一级tab
  38. }
  39. $children_config_tab = ConfigModel::getConfigChildrenTabAll($tab_id);//获取二级tab
  40. if (!$children_tab_id && $children_config_tab) {
  41. $children_tab_id = $children_config_tab[0]['id'];
  42. }
  43. if ($children_tab_id) {
  44. $tid = $children_tab_id;
  45. } else {
  46. $tid = $tab_id;
  47. }
  48. //获取分类配置参数
  49. $list = ConfigModel::getAll($tid);
  50. $formbuider = ConfigModel::builder_config_from_data($list);//生产表单json
  51. $form = Form::make_post_form('编辑配置', $formbuider, Url::buildUrl('save_basics'));
  52. $this->assign('tab_id', $tab_id);
  53. $this->assign('children_tab_id', $children_tab_id);
  54. $this->assign('config_tab', $config_tab);
  55. $this->assign('children_config_tab', $children_config_tab);
  56. $this->assign(compact('form'));
  57. $this->assign('list', $list);
  58. return $this->fetch();
  59. }
  60. /**
  61. * 基础配置 单个
  62. * @return mixed|void
  63. */
  64. public function index_alone()
  65. {
  66. $tab_id = input('tab_id');
  67. if (!$tab_id) return $this->failed('参数错误,请重新打开');
  68. $this->assign('tab_id', $tab_id);
  69. $list = ConfigModel::getAll($tab_id);
  70. foreach ($list as $k => $v) {
  71. if (!is_null(json_decode($v['value'])))
  72. $list[$k]['value'] = json_decode($v['value'], true);
  73. if ($v['type'] == 'upload' && !empty($v['value'])) {
  74. if ($v['upload_type'] == 1 || $v['upload_type'] == 3) $list[$k]['value'] = explode(',', $v['value']);
  75. }
  76. }
  77. $this->assign('list', $list);
  78. return $this->fetch();
  79. }
  80. /**
  81. * 添加字段
  82. * @return string
  83. * @throws \FormBuilder\exception\FormBuilderException
  84. */
  85. public function create()
  86. {
  87. $data = Util::getMore(['type',]);//接收参数
  88. $tab_id = !empty(request()->param('tab_id')) ? request()->param('tab_id') : 1;
  89. //前面通用字段
  90. $formbuiderheader = array();
  91. $formbuiderheader[] = Form::select('config_tab_id', '分类', 0)->setOptions(function () {
  92. $menuList = ConfigTabModel::field(['id', 'pid', 'title'])->select()->toArray();
  93. $list = sort_list_tier($menuList, '顶级', 'pid', 'id');
  94. $options = [['value' => 0, 'label' => '顶级按钮']];
  95. foreach ($list as $option) {
  96. $options[] = ['value' => $option['id'], 'label' => $option['html'] . $option['title']];
  97. }
  98. return $options;
  99. })->filterable(1);
  100. $formbuiderheader[] = Form::select('input_type', '类型')->setOptions(ConfigModel::texttype());
  101. $formbuiderheader[] = Form::input('info', '配置名称')->autofocus(1);
  102. $formbuiderheader[] = Form::input('menu_name', '字段变量')->placeholder('例如:site_url');
  103. $formbuiderheader[] = Form::input('desc', '配置简介');
  104. //不同类型不同字段
  105. $formbuider = array();
  106. switch ($data['type']) {
  107. case 0://文本框
  108. $formbuider = ConfigModel::createInputRule($tab_id);
  109. break;
  110. case 1://多行文本框
  111. $formbuider = ConfigModel::createTextAreaRule($tab_id);
  112. break;
  113. case 2://单选框
  114. $formbuider = ConfigModel::createRadioRule($tab_id);
  115. break;
  116. case 3://文件上传
  117. $formbuider = ConfigModel::createUploadRule($tab_id);
  118. break;
  119. case 4://多选框
  120. $formbuider = ConfigModel::createCheckboxRule($tab_id);
  121. break;
  122. case 5://下拉框
  123. $formbuider = ConfigModel::createSelectRule($tab_id);
  124. break;
  125. }
  126. //后面通用字段
  127. $formbuiderfoot = array();
  128. $formbuiderfoot[] = Form::number('sort', '排序');
  129. $formbuiderfoot[] = Form::radio('status', '状态', 1)->options(ConfigModel::formstatus());
  130. $formbuiders = array_merge($formbuiderheader, $formbuider, $formbuiderfoot);
  131. $form = Form::make_post_form('添加字段', $formbuiders, Url::buildUrl('save'));
  132. $this->assign(compact('form'));
  133. $this->assign('get', request()->param());
  134. return $this->fetch();
  135. }
  136. /**
  137. * 保存字段
  138. * */
  139. public function save()
  140. {
  141. $data = Util::postMore([
  142. 'menu_name',
  143. 'type',
  144. 'input_type',
  145. 'config_tab_id',
  146. 'parameter',
  147. 'upload_type',
  148. 'required',
  149. 'width',
  150. 'high',
  151. 'value',
  152. 'info',
  153. 'desc',
  154. 'sort',
  155. 'status',]);
  156. if (!$data['info']) return Json::fail('请输入配置名称');
  157. if (!$data['menu_name']) return Json::fail('请输入字段名称');
  158. if ($data['menu_name']) {
  159. $oneConfig = ConfigModel::getOneConfig('menu_name', $data['menu_name']);
  160. if (!empty($oneConfig)) return Json::fail('请重新输入字段名称,之前的已经使用过了');
  161. }
  162. if (!$data['desc']) return Json::fail('请输入配置简介');
  163. if ($data['sort'] < 0) {
  164. $data['sort'] = 0;
  165. }
  166. if ($data['type'] == 'text') {
  167. if (!ConfigModel::valiDateTextRole($data)) return Json::fail(ConfigModel::getErrorInfo());
  168. }
  169. if ($data['type'] == 'textarea') {
  170. if (!ConfigModel::valiDateTextareaRole($data)) return Json::fail(ConfigModel::getErrorInfo());
  171. }
  172. $data['parameter'] = htmlspecialchars_decode($data['parameter']);
  173. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  174. if (!$data['parameter']) return Json::fail('请输入配置参数');
  175. if (!ConfigModel::valiDateRadioAndCheckbox($data)) return Json::fail(ConfigModel::getErrorInfo());
  176. $data['value'] = json_encode($data['value']);
  177. }
  178. ConfigModel::create($data);
  179. CacheService::clear();
  180. return Json::successful('添加菜单成功!');
  181. }
  182. /**
  183. * @param $id
  184. */
  185. public function update_config($id)
  186. {
  187. $type = request()->post('type');
  188. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  189. $value = request()->post('value');
  190. } else {
  191. $value = request()->post('value/a');
  192. }
  193. $data = Util::postMore(['status', 'info', 'desc', 'sort', 'config_tab_id', 'required', 'parameter', ['value', $value], 'upload_type', 'input_type']);
  194. $data['value'] = htmlspecialchars_decode(json_encode($data['value']));
  195. $data['parameter'] = htmlspecialchars_decode($data['parameter']);
  196. if (!ConfigModel::get($id)) return Json::fail('编辑的记录不存在!');
  197. ConfigModel::edit($data, $id);
  198. return Json::successful('修改成功!');
  199. }
  200. /**
  201. * 修改是否显示子子段
  202. * @param $id
  203. * @return mixed
  204. */
  205. public function edit_config($id)
  206. {
  207. $menu = ConfigModel::get($id)->getData();
  208. if (!$menu) return Json::fail('数据不存在!');
  209. $formbuider = array();
  210. $formbuider[] = Form::input('menu_name', '字段变量', $menu['menu_name'])->disabled(1);
  211. $formbuider[] = Form::hidden('type', $menu['type']);
  212. // $formbuider[] = Form::select('config_tab_id', '分类', (string)$menu['config_tab_id'])->setOptions(ConfigModel::getConfigTabAll(-1));
  213. $formbuider[] = Form::select('config_tab_id', '分类', (string)$menu['config_tab_id'])->setOptions(function () {
  214. $menuList = ConfigTabModel::field(['id', 'pid', 'title'])->select()->toArray();
  215. $list = sort_list_tier($menuList, '顶级', 'pid', 'id');
  216. $options = [['value' => 0, 'label' => '顶级按钮']];
  217. foreach ($list as $option) {
  218. $options[] = ['value' => $option['id'], 'label' => $option['html'] . $option['title']];
  219. }
  220. return $options;
  221. })->filterable(1);
  222. $formbuider[] = Form::input('info', '配置名称', $menu['info'])->autofocus(1);
  223. $formbuider[] = Form::input('desc', '配置简介', $menu['desc']);
  224. switch ($menu['type']) {
  225. case 'text':
  226. $menu['value'] = json_decode($menu['value'], true);
  227. $formbuider[] = Form::select('input_type', '类型', $menu['input_type'])->setOptions(ConfigModel::texttype());
  228. //输入框验证规则
  229. $formbuider[] = Form::input('value', '默认值', $menu['value']);
  230. if (!empty($menu['required'])) {
  231. $formbuider[] = Form::number('width', '文本框宽(%)', $menu['width']);
  232. $formbuider[] = Form::input('required', '验证规则', $menu['required'])->placeholder('多个请用,隔开例如:required:true,url:true');
  233. }
  234. break;
  235. case 'textarea':
  236. $menu['value'] = json_decode($menu['value'], true);
  237. //多行文本
  238. if (!empty($menu['high'])) {
  239. $formbuider[] = Form::textarea('value', '默认值', $menu['value'])->rows(5);
  240. $formbuider[] = Form::number('width', '文本框宽(%)', $menu['width']);
  241. $formbuider[] = Form::number('high', '多行文本框高(%)', $menu['high']);
  242. } else {
  243. $formbuider[] = Form::input('value', '默认值', $menu['value']);
  244. }
  245. break;
  246. case 'radio':
  247. $menu['value'] = json_decode($menu['value'], true);
  248. $parameter = explode("\n", htmlspecialchars_decode($menu['parameter']));
  249. $options = [];
  250. if ($parameter) {
  251. foreach ($parameter as $v) {
  252. $data = explode("=>", $v);
  253. $options[] = ['label' => $data[1], 'value' => $data[0]];
  254. }
  255. $formbuider[] = Form::radio('value', '默认值', $menu['value'])->options($options);
  256. }
  257. //单选和多选参数配置
  258. if (!empty($menu['parameter'])) {
  259. $formbuider[] = Form::textarea('parameter', '配置参数', $menu['parameter'])->placeholder("参数方式例如:\n1=白色\n2=红色\n3=黑色");
  260. }
  261. break;
  262. case 'checkbox':
  263. $menu['value'] = json_decode($menu['value'], true) ?: [];
  264. $parameter = explode("\n", htmlspecialchars_decode($menu['parameter']));
  265. $options = [];
  266. if ($parameter) {
  267. foreach ($parameter as $v) {
  268. $data = explode("=>", $v);
  269. $options[] = ['label' => $data[1], 'value' => $data[0]];
  270. }
  271. $formbuider[] = Form::checkbox('value', '默认值', $menu['value'])->options($options);
  272. }
  273. //单选和多选参数配置
  274. if (!empty($menu['parameter'])) {
  275. $formbuider[] = Form::textarea('parameter', '配置参数', $menu['parameter'])->placeholder("参数方式例如:\n1=白色\n2=红色\n3=黑色");
  276. }
  277. break;
  278. case 'upload':
  279. if ($menu['upload_type'] == 1) {
  280. $menu['value'] = json_decode($menu['value'], true);
  281. $formbuider[] = Form::frameImageOne('value', '图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'value')), (string)$menu['value'])->icon('image')->width('100%')->height('550px');
  282. } elseif ($menu['upload_type'] == 2) {
  283. $menu['value'] = json_decode($menu['value'], true) ?: [];
  284. $formbuider[] = Form::frameImages('value', '多图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'value')), $menu['value'])->maxLength(5)->icon('images')->width('100%')->height('550px')->spin(0);
  285. } else {
  286. $menu['value'] = json_decode($menu['value'], true);
  287. $formbuider[] = Form::uploadFileOne('value', '文件', Url::buildUrl('file_upload'), $menu['value'])->name('file');
  288. }
  289. //上传类型选择
  290. if (!empty($menu['upload_type'])) {
  291. $formbuider[] = Form::radio('upload_type', '上传类型', $menu['upload_type'])->options([['value' => 1, 'label' => '单图'], ['value' => 2, 'label' => '多图'], ['value' => 3, 'label' => '文件']]);
  292. }
  293. break;
  294. }
  295. $formbuider[] = Form::number('sort', '排序', $menu['sort']);
  296. $formbuider[] = Form::radio('status', '状态', $menu['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  297. $form = Form::make_post_form('编辑字段', $formbuider, Url::buildUrl('update_config', array('id' => $id)));
  298. $this->assign(compact('form'));
  299. return $this->fetch('public/form-builder');
  300. }
  301. /**
  302. * 删除子字段
  303. * @return \think\response\Json
  304. */
  305. public function delete_config()
  306. {
  307. $id = input('id');
  308. if (!ConfigModel::del($id)) {
  309. return Json::fail(ConfigModel::getErrorInfo('删除失败,请稍候再试!'));
  310. } else {
  311. CacheService::clear();
  312. return Json::successful('删除成功!');
  313. }
  314. }
  315. /**
  316. * 保存数据 true
  317. * */
  318. public function save_basics()
  319. {
  320. $request = app('request');
  321. if ($request->isPost()) {
  322. $post = $request->post();
  323. foreach ($post as $k => $v) {
  324. if (is_array($v)) {
  325. $res = ConfigModel::where('menu_name', $k)->column('upload_type', 'type');
  326. foreach ($res as $kk => $vv) {
  327. if ($kk == 'upload') {
  328. if ($vv == 1 || $vv == 3) {
  329. $post[$k] = $v[0];
  330. }
  331. }
  332. }
  333. }
  334. }
  335. foreach ($post as $k => $v) {
  336. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  337. }
  338. CacheService::clear();
  339. return $this->successful('修改成功');
  340. }
  341. }
  342. /**
  343. * 文件上传
  344. * */
  345. public function file_upload()
  346. {
  347. $upload = new Upload('local');
  348. $res = $upload->to('config/file')->move($this->request->param('file', 'file'));
  349. if ($res === false) return Json::fail($upload->getError());
  350. return Json::successful('上传成功!', ['filePath' => $res->filePath]);
  351. }
  352. }