Config.php 6.2 KB

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