System.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\controller;
  10. use app\common\builder\ZBuilder;
  11. use app\admin\model\Config as ConfigModel;
  12. use app\admin\model\Module as ModuleModel;
  13. /**
  14. * 系统模块控制器
  15. * @package app\admin\controller
  16. */
  17. class System extends Admin
  18. {
  19. /**
  20. * 系统设置
  21. * @param string $group 分组
  22. * @author 蔡伟明 <314013107@qq.com>
  23. * @return mixed
  24. * @throws \think\Exception
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index($group = 'base')
  28. {
  29. // 保存数据
  30. if ($this->request->isPost()) {
  31. $data = $this->request->post();
  32. if (isset(config('config_group')[$group])) {
  33. // 查询该分组下所有的配置项名和类型
  34. $items = ConfigModel::where('group', $group)->where('status', 1)->column('name,type');
  35. foreach ($items as $name => $type) {
  36. if (!isset($data[$name])) {
  37. switch ($type) {
  38. // 开关
  39. case 'switch':
  40. $data[$name] = 0;
  41. break;
  42. case 'checkbox':
  43. $data[$name] = '';
  44. break;
  45. }
  46. } else {
  47. // 如果值是数组则转换成字符串,适用于复选框等类型
  48. if (is_array($data[$name])) {
  49. $data[$name] = implode(',', $data[$name]);
  50. }
  51. switch ($type) {
  52. // 开关
  53. case 'switch':
  54. $data[$name] = 1;
  55. break;
  56. // 日期时间
  57. case 'date':
  58. case 'time':
  59. case 'datetime':
  60. $data[$name] = strtotime($data[$name]);
  61. break;
  62. }
  63. }
  64. ConfigModel::where('name', $name)->update(['value' => $data[$name]]);
  65. }
  66. } else {
  67. // 保存模块配置
  68. if (false === ModuleModel::where('name', $group)->update(['config' => json_encode($data)])) {
  69. $this->error('更新失败');
  70. }
  71. // 非开发模式,缓存数据
  72. if (config('develop_mode') == 0) {
  73. cache('module_config_'.$group, $data);
  74. }
  75. }
  76. cache('system_config', null);
  77. // 记录行为
  78. action_log('system_config_update', 'admin_config', 0, UID, "分组($group)");
  79. $this->success('更新成功', url('index', ['group' => $group]));
  80. } else {
  81. // 配置分组信息
  82. $list_group = config('config_group');
  83. // 读取模型配置
  84. $modules = ModuleModel::where('config', 'neq', '')
  85. ->where('status', 1)
  86. ->column('config,title,name', 'name');
  87. foreach ($modules as $name => $module) {
  88. $list_group[$name] = $module['title'];
  89. }
  90. $tab_list = [];
  91. foreach ($list_group as $key => $value) {
  92. $tab_list[$key]['title'] = $value;
  93. $tab_list[$key]['url'] = url('index', ['group' => $key]);
  94. }
  95. if (isset(config('config_group')[$group])) {
  96. // 查询条件
  97. $map['group'] = $group;
  98. $map['status'] = 1;
  99. // 数据列表
  100. $data_list = ConfigModel::where($map)
  101. ->order('sort asc,id asc')
  102. ->field('group', true)
  103. ->select();
  104. $data_list = $data_list->toArray();
  105. foreach ($data_list as &$value) {
  106. // 解析options
  107. if ($value['options'] != '') {
  108. $value['options'] = parse_attr($value['options']);
  109. }
  110. // 默认模块列表
  111. if ($value['name'] == 'home_default_module') {
  112. $value['options'] = array_merge(['index' => '默认'], ModuleModel::getModule());
  113. }
  114. switch ($value['type']) {
  115. // 日期时间
  116. case 'date':
  117. $value['value'] = $value['value'] != '' ? date('Y-m-d', $value['value']) : '';
  118. break;
  119. case 'time':
  120. $value['value'] = $value['value'] != '' ? date('H:i:s', $value['value']) : '';
  121. break;
  122. case 'datetime':
  123. $value['value'] = $value['value'] != '' ? date('Y-m-d H:i:s', $value['value']) : '';
  124. break;
  125. case 'linkages':
  126. $value['token'] = $this->createLinkagesToken($value['table'], $value['option'], $value['key']);
  127. break;
  128. case 'colorpicker':
  129. $value['mode'] = 'rgba';
  130. break;
  131. }
  132. }
  133. // 使用ZBuilder快速创建表单
  134. return ZBuilder::make('form')
  135. ->setPageTitle('系统设置')
  136. ->setTabNav($tab_list, $group)
  137. ->setFormItems($data_list)
  138. ->fetch();
  139. } else {
  140. // 模块配置
  141. $module_info = ModuleModel::getInfoFromFile($group);
  142. $config = $module_info['config'];
  143. $trigger = isset($module_info['trigger']) ? $module_info['trigger'] : [];
  144. // 数据库内的模块信息
  145. $db_config = ModuleModel::where('name', $group)->value('config');
  146. $db_config = json_decode($db_config, true);
  147. // 使用ZBuilder快速创建表单
  148. return ZBuilder::make('form')
  149. ->setPageTitle('模块设置')
  150. ->setTabNav($tab_list, $group)
  151. ->addFormItems($config)
  152. ->setFormdata($db_config) // 设置表格数据
  153. ->setTrigger($trigger) // 设置触发
  154. ->fetch();
  155. }
  156. }
  157. }
  158. /**
  159. * 创建快速多级联动Token
  160. * @param string $table 表名
  161. * @param string $option
  162. * @param string $key
  163. * @author 蔡伟明 <314013107@qq.com>
  164. * @return bool|string
  165. */
  166. private function createLinkagesToken($table = '', $option = '', $key = '')
  167. {
  168. $table_token = substr(sha1($table.'-'.$option.'-'.$key.'-'.session('user_auth.last_login_ip').'-'.UID.'-'.session('user_auth.last_login_time')), 0, 8);
  169. session($table_token, ['table' => $table, 'option' => $option, 'key' => $key]);
  170. return $table_token;
  171. }
  172. }