Config.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 系统配置
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\general;
  15. use app\common\controller\Backend;
  16. use app\common\model\Config as ConfigModel;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. class Config extends Backend
  20. {
  21. protected function initialize()
  22. {
  23. parent::initialize();
  24. $filepath = $this->app->getBasePath() . 'admin' . DS . "view" . DS . 'custom' . DS;
  25. $custom = str_replace($filepath . DS, '', glob($filepath . DS . 'custom*'));
  26. $this->assign('custom', $custom);
  27. $this->assign('groupArray', config('site.config_group'));
  28. $this->modelClass = new ConfigModel;
  29. }
  30. //配置首页
  31. public function index($group = 'base')
  32. {
  33. if ($this->request->isAjax()) {
  34. $list = $this->modelClass->view('config')
  35. ->where('group', $group)
  36. ->where('config.name', '<>', 'cdnurl')
  37. ->view('field_type', 'title as ftitle', 'field_type.name=config.type', 'LEFT')
  38. ->order('listorder desc,id desc')
  39. ->select();
  40. return json(["code" => 0, "data" => $list]);
  41. } else {
  42. $this->assign('group', $group);
  43. $this->assignconfig('group', $group);
  44. return $this->fetch();
  45. }
  46. }
  47. //配置设置
  48. public function setting($group = 'base')
  49. {
  50. if ($this->request->isPost()) {
  51. $this->token();
  52. $data = $this->request->post('row/a');
  53. // 查询该分组下所有的配置项名和类型
  54. $items = ConfigModel::where('group', $group)->where('status', 1)->column('type', 'name');
  55. foreach ($items as $name => $type) {
  56. //查看是否赋值
  57. if (!isset($data[$name])) {
  58. switch ($type) {
  59. // 开关
  60. case 'switch':
  61. $data[$name] = 0;
  62. break;
  63. case 'checkbox':
  64. $data[$name] = '';
  65. break;
  66. }
  67. } else {
  68. // 如果值是数组则转换成字符串,适用于复选框等类型
  69. if (is_array($data[$name])) {
  70. $data[$name] = implode(',', $data[$name]);
  71. }
  72. switch ($type) {
  73. // 开关
  74. case 'switch':
  75. $data[$name] = 1;
  76. break;
  77. }
  78. }
  79. //数据格式验证
  80. if (!empty($data[$name]) && in_array($type, ['number']) && !\think\facade\Validate::isNumber($data[$name])) {
  81. return $this->error("'" . $name . "'格式错误~");
  82. }
  83. if (isset($data[$name])) {
  84. ConfigModel::where(['name' => $name])->update(['value' => $data[$name]]);
  85. }
  86. }
  87. try {
  88. ConfigModel::refreshFile();
  89. } catch (Exception $e) {
  90. $this->error($e->getMessage());
  91. }
  92. return $this->success('设置更新成功');
  93. } else {
  94. $configList = ConfigModel::where('group', $group)
  95. ->where('status', 1)
  96. ->order('listorder desc,id desc')
  97. ->select();
  98. $siteList = [];
  99. foreach ($configList as $v) {
  100. $value = $v->toArray();
  101. $value['fieldArr'] = 'row';
  102. if ($value['type'] == 'custom') {
  103. if ($value['options'] != '') {
  104. $tpar = explode(".", $value['options'], 2);
  105. $value['options'] = $this->fetch('admin@custom/' . $tpar[0], ['vo' => $value]);
  106. unset($tpar);
  107. }
  108. } elseif ($value['options'] != '') {
  109. $value['options'] = parse_attr($value['options']);
  110. }
  111. if ($value['type'] == 'checkbox') {
  112. $value['value'] = empty($value['value']) ? [] : explode(',', $value['value']);
  113. }
  114. if ($value['type'] == 'datetime') {
  115. $value['value'] = empty($value['value']) ? date('Y-m-d H:i:s') : $value['value'];
  116. }
  117. if ($value['name'] == 'cdnurl') {
  118. //cdnurl不支持在线修改
  119. continue;
  120. }
  121. $siteList[] = $value;
  122. }
  123. $this->assign([
  124. 'fieldList' => $siteList,
  125. 'group' => $group,
  126. ]);
  127. return $this->fetch();
  128. }
  129. }
  130. //新增配置
  131. public function add()
  132. {
  133. $groupType = $this->request->param('groupType', 'base');
  134. if ($this->request->isPost()) {
  135. $this->token();
  136. $params = $this->request->post("row/a");
  137. if ($params) {
  138. try {
  139. $this->validate($params, 'app\admin\validate\Config');
  140. $res = $this->modelClass->create($params);
  141. } catch (ValidateException | Exception $e) {
  142. $this->error($e->getMessage());
  143. }
  144. if ($res !== false) {
  145. try {
  146. ConfigModel::refreshFile();
  147. } catch (Exception $e) {
  148. $this->error($e->getMessage());
  149. }
  150. $this->success('新增成功');
  151. } else {
  152. $this->error($this->modelClass->getError());
  153. }
  154. }
  155. $this->error('参数不能为空');
  156. }
  157. $fieldType = Db::name('field_type')->order('listorder')->select();
  158. $this->assign([
  159. 'fieldType' => $fieldType,
  160. 'groupType' => $groupType,
  161. ]);
  162. return $this->fetch();
  163. }
  164. //编辑配置
  165. public function edit()
  166. {
  167. $id = $this->request->param('id/d');
  168. $row = $this->modelClass->find($id);
  169. if (!$row) {
  170. $this->error('记录未找到');
  171. }
  172. if ($this->request->isPost()) {
  173. $this->token();
  174. $params = $this->request->post("row/a");
  175. if ($params) {
  176. try {
  177. $this->validate($params, 'app\admin\validate\Config');
  178. $res = $row->save($params);
  179. } catch (ValidateException | Exception $e) {
  180. $this->error($e->getMessage());
  181. }
  182. if ($res !== false) {
  183. try {
  184. ConfigModel::refreshFile();
  185. } catch (Exception $e) {
  186. $this->error($e->getMessage());
  187. }
  188. $this->success('编辑成功');
  189. } else {
  190. $this->error($row->getError());
  191. }
  192. }
  193. $this->error('参数不能为空');
  194. }
  195. $fieldType = Db::name('field_type')->order('listorder')->select();
  196. $this->assign([
  197. 'data' => $row,
  198. 'id' => $id,
  199. 'fieldType' => $fieldType,
  200. ]);
  201. return $this->fetch();
  202. }
  203. //删除配置
  204. public function del()
  205. {
  206. if (false === $this->request->isPost()) {
  207. $this->error('未知参数');
  208. }
  209. $id = $this->request->param('id/a');
  210. $row = ConfigModel::find($id[0]);
  211. if (in_array($row->name, [
  212. 'name',
  213. 'version',
  214. 'config_group',
  215. 'theme', 'admin_forbid_ip',
  216. 'upload_driver',
  217. 'upload_thumb_water',
  218. 'upload_thumb_water_pic',
  219. 'upload_thumb_water_position',
  220. 'upload_thumb_water_alpha',
  221. ])) {
  222. $this->error('核心配置禁止删除');
  223. }
  224. if ($row) {
  225. try {
  226. $row->delete();
  227. ConfigModel::refreshFile();
  228. } catch (Exception $e) {
  229. $this->error($e->getMessage());
  230. }
  231. $this->success("操作成功!");
  232. } else {
  233. $this->error('配置不存在');
  234. }
  235. }
  236. }