Config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | tp5-form-builder[请基于ThinkPHP5使用]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016-2019 http://www.lwwan.com
  6. // +----------------------------------------------------------------------
  7. // | Author 似水星辰 [ 2630481389@qq.com ]
  8. // +----------------------------------------------------------------------
  9. // | 星辰工作室 QQ群331378225
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use think\Controller;
  13. use app\admin\model\Config as ConfigModel;
  14. /**
  15. * 控制器
  16. * @package app\admin\controller
  17. */
  18. class Config extends Controller
  19. {
  20. /**
  21. * 新增表单项
  22. * @author 似水星辰 [ 2630481389@qq.com ]
  23. * @return mixed
  24. */
  25. public function add()
  26. {
  27. $fields = [
  28. [
  29. 'type' => 'radio',
  30. 'name' => 'group',
  31. 'title' => '配置分组',
  32. 'extra' => config('config_group'),
  33. 'value' => $group,
  34. ],
  35. [
  36. 'type' => 'select',
  37. 'name' => 'type',
  38. 'title' => '配置类型',
  39. 'tips' => '',
  40. 'extra' => config('form_item_type')
  41. ],
  42. [
  43. 'type' => 'text',
  44. 'name' => 'title',
  45. 'title' => '配置标题',
  46. 'attr' => 'data-rule="required;" data-msg-required="标题不能为空,可以使用中文或者英文"'
  47. ],
  48. [
  49. 'type' => 'text',
  50. 'name' => 'name',
  51. 'title' => '配置标识',
  52. 'attr' => 'data-rule="required;name;" data-rule-name="[/^[a-zA-Z][a-zA-Z0-9_]*$/, \'请输入正确的配置标识,只能使用英文和下划线,必须以英文字母开头\']" data-msg-required="配置标识不能为空"'
  53. ],
  54. ['type' => 'textarea', 'name' => 'value', 'title' => '配置值'],
  55. ['type' => 'textarea', 'name' => 'extra', 'title' => '配置项', 'tips' => '用于单选、多选、下拉、联动等类型'],
  56. ['type' => 'textarea', 'name' => 'tips', 'title' => '配置说明'],
  57. ['type' => 'radio', 'name' => 'status', 'title' => '状态', '', 'extra' => ['禁用', '启用'], 'value' => 1]
  58. ];
  59. $this->assign('page_title', '新增管理员');
  60. $this->assign('form_items', $fields);
  61. return $this->fetch('public/add');
  62. }
  63. /**
  64. * 编辑表单项
  65. * @param int $id
  66. * @author 似水星辰 [ 2630481389@qq.com ]
  67. * @return mixed
  68. */
  69. public function edit($id = 0)
  70. {
  71. if ($id === 0) $this->error('参数错误');
  72. // 获取数据
  73. $info = ConfigModel::get($id);
  74. $fields = [
  75. ['type' => 'hidden', 'name' => 'id',],
  76. ['type' => 'radio', 'name' => 'group', 'title' => '配置分组', 'extra' => config('config_group'), 'value' => $group,],
  77. ['type' => 'select', 'name' => 'type', 'title' => '配置类型', 'extra' => config('form_item_type')],
  78. [
  79. 'type' => 'text',
  80. 'name' => 'title',
  81. 'title' => '配置标题',
  82. 'attr' => 'data-rule="required;" data-msg-required="标题不能为空,可以使用中文或者英文"'],
  83. [
  84. 'type' => 'text',
  85. 'name' => 'name',
  86. 'title' => '配置标识',
  87. 'attr' => 'data-rule="required;name;" data-rule-name="[/^[a-zA-Z][a-zA-Z0-9_]*$/, \'请输入正确的配置标识,只能使用英文和下划线,必须以英文字母开头\']" data-msg-required="配置标识不能为空"'
  88. ],
  89. ['type' => 'textarea', 'name' => 'value', 'title' => '配置值'],
  90. ['type' => 'textarea', 'name' => 'extra', 'title' => '配置项', 'tips' => '用于单选、多选、下拉、联动等类型'],
  91. ['type' => 'textarea', 'name' => 'tips', 'title' => '配置说明'],
  92. ['type' => 'radio', 'name' => 'status', 'title' => '状态', '', 'extra' => ['禁用', '启用'], 'value' => 1]
  93. ];
  94. $this->assign('page_title', '编辑配置');
  95. $this->assign('form_items', $this->setData($fields, $info));
  96. return $this->fetch('public/edit');
  97. }
  98. /**
  99. * 设置表单数据
  100. * @param array $fields 字段组
  101. * @param array $info 字段值组
  102. * @author 似水星辰 [ 2630481389@qq.com ]
  103. * @return mixed
  104. */
  105. public function setData($fields = [], $info =[]){
  106. if(is_array($fields)){
  107. foreach($fields as &$v){
  108. if($v['type'] != 'sort'){
  109. if($v['type'] == 'password'){
  110. $v['value'] = '';
  111. }else if($v['type'] == 'attr'){
  112. $v['value'] = htmlspecialchars_decode($info[$v['name']]);
  113. }else{
  114. $v['value'] = $info[$v['name']];
  115. }
  116. }
  117. }
  118. }
  119. return $fields;
  120. }
  121. }