Config.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\common\controller\Backend;
  4. use app\common\library\Email;
  5. use app\common\model\Config as ConfigModel;
  6. use think\Exception;
  7. use think\Validate;
  8. /**
  9. * 系统配置
  10. *
  11. * @icon fa fa-cogs
  12. * @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
  13. */
  14. class Config extends Backend
  15. {
  16. /**
  17. * @var \app\common\model\Config
  18. */
  19. protected $model = null;
  20. protected $noNeedRight = ['check', 'rulelist'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('Config');
  25. ConfigModel::event('before_write', function ($row) {
  26. if (isset($row['name']) && $row['name'] == 'name' && preg_match("/fast" . "admin/i", $row['value'])) {
  27. throw new Exception(__("Site name incorrect"));
  28. }
  29. });
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. $siteList = [];
  37. $groupList = ConfigModel::getGroupList();
  38. foreach ($groupList as $k => $v) {
  39. $siteList[$k]['name'] = $k;
  40. $siteList[$k]['title'] = $v;
  41. $siteList[$k]['list'] = [];
  42. }
  43. foreach ($this->model->all() as $k => $v) {
  44. if (!isset($siteList[$v['group']])) {
  45. continue;
  46. }
  47. $value = $v->toArray();
  48. $value['title'] = __($value['title']);
  49. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  50. $value['value'] = explode(',', $value['value']);
  51. }
  52. $value['content'] = json_decode($value['content'], true);
  53. $value['tip'] = htmlspecialchars($value['tip']);
  54. $siteList[$v['group']]['list'][] = $value;
  55. }
  56. $index = 0;
  57. foreach ($siteList as $k => &$v) {
  58. $v['active'] = !$index ? true : false;
  59. $index++;
  60. }
  61. $this->view->assign('siteList', $siteList);
  62. $this->view->assign('typeList', ConfigModel::getTypeList());
  63. $this->view->assign('ruleList', ConfigModel::getRegexList());
  64. $this->view->assign('groupList', ConfigModel::getGroupList());
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. if ($this->request->isPost()) {
  73. $this->token();
  74. $params = $this->request->post("row/a", [], 'trim');
  75. if ($params) {
  76. foreach ($params as $k => &$v) {
  77. $v = is_array($v) ? implode(',', $v) : $v;
  78. }
  79. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
  80. $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  81. } else {
  82. $params['content'] = '';
  83. }
  84. try {
  85. $result = $this->model->create($params);
  86. } catch (Exception $e) {
  87. $this->error($e->getMessage());
  88. }
  89. if ($result !== false) {
  90. try {
  91. $this->refreshFile();
  92. } catch (Exception $e) {
  93. $this->error($e->getMessage());
  94. }
  95. $this->success();
  96. } else {
  97. $this->error($this->model->getError());
  98. }
  99. }
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 编辑
  106. * @param null $ids
  107. */
  108. public function edit($ids = null)
  109. {
  110. if ($this->request->isPost()) {
  111. $this->token();
  112. $row = $this->request->post("row/a", [], 'trim');
  113. if ($row) {
  114. $configList = [];
  115. foreach ($this->model->all() as $v) {
  116. if (isset($row[$v['name']])) {
  117. $value = $row[$v['name']];
  118. if (is_array($value) && isset($value['field'])) {
  119. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  120. } else {
  121. $value = is_array($value) ? implode(',', $value) : $value;
  122. }
  123. $v['value'] = $value;
  124. $configList[] = $v->toArray();
  125. }
  126. }
  127. try {
  128. $this->model->allowField(true)->saveAll($configList);
  129. } catch (Exception $e) {
  130. $this->error($e->getMessage());
  131. }
  132. try {
  133. $this->refreshFile();
  134. } catch (Exception $e) {
  135. $this->error($e->getMessage());
  136. }
  137. $this->success();
  138. }
  139. $this->error(__('Parameter %s can not be empty', ''));
  140. }
  141. }
  142. /**
  143. * 删除
  144. * @param string $ids
  145. */
  146. public function del($ids = "")
  147. {
  148. $name = $this->request->post('name');
  149. $config = ConfigModel::getByName($name);
  150. if ($name && $config) {
  151. try {
  152. $config->delete();
  153. $this->refreshFile();
  154. } catch (Exception $e) {
  155. $this->error($e->getMessage());
  156. }
  157. $this->success();
  158. } else {
  159. $this->error(__('Invalid parameters'));
  160. }
  161. }
  162. /**
  163. * 刷新配置文件
  164. */
  165. protected function refreshFile()
  166. {
  167. $config = [];
  168. foreach ($this->model->all() as $k => $v) {
  169. $value = $v->toArray();
  170. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  171. $value['value'] = explode(',', $value['value']);
  172. }
  173. if ($value['type'] == 'array') {
  174. $value['value'] = (array)json_decode($value['value'], true);
  175. }
  176. $config[$value['name']] = $value['value'];
  177. }
  178. file_put_contents(
  179. APP_PATH . 'extra' . DS . 'site.php',
  180. '<?php' . "\n\nreturn " . var_export($config, true) . ";"
  181. );
  182. }
  183. /**
  184. * 检测配置项是否存在
  185. * @internal
  186. */
  187. public function check()
  188. {
  189. $params = $this->request->post("row/a");
  190. if ($params) {
  191. $config = $this->model->get($params);
  192. if (!$config) {
  193. return $this->success();
  194. } else {
  195. return $this->error(__('Name already exist'));
  196. }
  197. } else {
  198. return $this->error(__('Invalid parameters'));
  199. }
  200. }
  201. /**
  202. * 规则列表
  203. * @internal
  204. */
  205. public function rulelist()
  206. {
  207. //主键
  208. $primarykey = $this->request->request("keyField");
  209. //主键值
  210. $keyValue = $this->request->request("keyValue", "");
  211. $keyValueArr = array_filter(explode(',', $keyValue));
  212. $regexList = \app\common\model\Config::getRegexList();
  213. $list = [];
  214. foreach ($regexList as $k => $v) {
  215. if ($keyValueArr) {
  216. if (in_array($k, $keyValueArr)) {
  217. $list[] = ['id' => $k, 'name' => $v];
  218. }
  219. } else {
  220. $list[] = ['id' => $k, 'name' => $v];
  221. }
  222. }
  223. return json(['list' => $list]);
  224. }
  225. /**
  226. * 发送测试邮件
  227. * @internal
  228. */
  229. public function emailtest()
  230. {
  231. $row = $this->request->post('row/a');
  232. $receiver = $this->request->post("receiver");
  233. if ($receiver) {
  234. if (!Validate::is($receiver, "email")) {
  235. $this->error(__('Please input correct email'));
  236. }
  237. \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
  238. $email = new Email;
  239. $result = $email
  240. ->to($receiver)
  241. ->subject(__("This is a test mail"))
  242. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  243. ->send();
  244. if ($result) {
  245. $this->success();
  246. } else {
  247. $this->error($email->getError());
  248. }
  249. } else {
  250. return $this->error(__('Invalid parameters'));
  251. }
  252. }
  253. }