Config.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /**
  8. * 系统配置
  9. *
  10. * @icon fa fa-cogs
  11. * @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
  12. */
  13. class Config extends Backend
  14. {
  15. protected $model = null;
  16. protected $noNeedRight = ['check'];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Config');
  21. }
  22. /**
  23. * 查看
  24. */
  25. public function index()
  26. {
  27. $siteList = [];
  28. $groupList = ConfigModel::getGroupList();
  29. foreach ($groupList as $k => $v) {
  30. $siteList[$k]['name'] = $k;
  31. $siteList[$k]['title'] = $v;
  32. $siteList[$k]['list'] = [];
  33. }
  34. foreach ($this->model->all() as $k => $v) {
  35. if (!isset($siteList[$v['group']])) {
  36. continue;
  37. }
  38. $value = $v->toArray();
  39. $value['title'] = __($value['title']);
  40. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  41. $value['value'] = explode(',', $value['value']);
  42. }
  43. $value['content'] = json_decode($value['content'], TRUE);
  44. $siteList[$v['group']]['list'][] = $value;
  45. }
  46. $index = 0;
  47. foreach ($siteList as $k => &$v) {
  48. $v['active'] = !$index ? true : false;
  49. $index++;
  50. }
  51. $this->view->assign('siteList', $siteList);
  52. $this->view->assign('typeList', ConfigModel::getTypeList());
  53. $this->view->assign('groupList', ConfigModel::getGroupList());
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 添加
  58. */
  59. public function add()
  60. {
  61. if ($this->request->isPost()) {
  62. $params = $this->request->post("row/a");
  63. if ($params) {
  64. foreach ($params as $k => &$v) {
  65. $v = is_array($v) ? implode(',', $v) : $v;
  66. }
  67. try {
  68. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
  69. $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  70. } else {
  71. $params['content'] = '';
  72. }
  73. $result = $this->model->create($params);
  74. if ($result !== false) {
  75. try {
  76. $this->refreshFile();
  77. $this->success();
  78. } catch (Exception $e) {
  79. $this->error($e->getMessage());
  80. }
  81. } else {
  82. $this->error($this->model->getError());
  83. }
  84. } catch (Exception $e) {
  85. $this->error($e->getMessage());
  86. }
  87. }
  88. $this->error(__('Parameter %s can not be empty', ''));
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 编辑
  94. * @param null $ids
  95. */
  96. public function edit($ids = NULL)
  97. {
  98. if ($this->request->isPost()) {
  99. $row = $this->request->post("row/a");
  100. if ($row) {
  101. $configList = [];
  102. foreach ($this->model->all() as $v) {
  103. if (isset($row[$v['name']])) {
  104. $value = $row[$v['name']];
  105. if (is_array($value) && isset($value['field'])) {
  106. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  107. } else {
  108. $value = is_array($value) ? implode(',', $value) : $value;
  109. }
  110. $v['value'] = $value;
  111. $configList[] = $v->toArray();
  112. }
  113. }
  114. $this->model->allowField(true)->saveAll($configList);
  115. try {
  116. $this->refreshFile();
  117. $this->success();
  118. } catch (Exception $e) {
  119. $this->error($e->getMessage());
  120. }
  121. }
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }
  124. }
  125. /**
  126. * 刷新配置文件
  127. */
  128. protected function refreshFile()
  129. {
  130. $config = [];
  131. foreach ($this->model->all() as $k => $v) {
  132. $value = $v->toArray();
  133. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  134. $value['value'] = explode(',', $value['value']);
  135. }
  136. if ($value['type'] == 'array') {
  137. $value['value'] = (array)json_decode($value['value'], TRUE);
  138. }
  139. $config[$value['name']] = $value['value'];
  140. }
  141. file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
  142. }
  143. /**
  144. * 检测配置项是否存在
  145. * @internal
  146. */
  147. public function check()
  148. {
  149. $params = $this->request->post("row/a");
  150. if ($params) {
  151. $config = $this->model->get($params);
  152. if (!$config) {
  153. return $this->success();
  154. } else {
  155. return $this->error(__('Name already exist'));
  156. }
  157. } else {
  158. return $this->error(__('Invalid parameters'));
  159. }
  160. }
  161. /**
  162. * 发送测试邮件
  163. * @internal
  164. */
  165. public function emailtest()
  166. {
  167. $row = $this->request->post('row/a');
  168. \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
  169. $receiver = $this->request->request("receiver");
  170. $email = new Email;
  171. $result = $email
  172. ->to($receiver)
  173. ->subject(__("This is a test mail"))
  174. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  175. ->send();
  176. if ($result) {
  177. $this->success();
  178. } else {
  179. $this->error($email->getError());
  180. }
  181. }
  182. }