Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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