Config.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. } catch (Exception $e) {
  81. $this->error($e->getMessage());
  82. }
  83. $this->success();
  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. } catch (Exception $e) {
  121. $this->error($e->getMessage());
  122. }
  123. $this->success();
  124. }
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. }
  128. public function del($ids = "")
  129. {
  130. $name = $this->request->request('name');
  131. $config = ConfigModel::getByName($name);
  132. if ($config) {
  133. try {
  134. $config->delete();
  135. $this->refreshFile();
  136. } catch (Exception $e) {
  137. $this->error($e->getMessage());
  138. }
  139. $this->success();
  140. } else {
  141. $this->error(__('Invalid parameters'));
  142. }
  143. }
  144. /**
  145. * 刷新配置文件
  146. */
  147. protected function refreshFile()
  148. {
  149. $config = [];
  150. foreach ($this->model->all() as $k => $v) {
  151. $value = $v->toArray();
  152. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  153. $value['value'] = explode(',', $value['value']);
  154. }
  155. if ($value['type'] == 'array') {
  156. $value['value'] = (array)json_decode($value['value'], TRUE);
  157. }
  158. $config[$value['name']] = $value['value'];
  159. }
  160. file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
  161. }
  162. /**
  163. * 检测配置项是否存在
  164. * @internal
  165. */
  166. public function check()
  167. {
  168. $params = $this->request->post("row/a");
  169. if ($params) {
  170. $config = $this->model->get($params);
  171. if (!$config) {
  172. return $this->success();
  173. } else {
  174. return $this->error(__('Name already exist'));
  175. }
  176. } else {
  177. return $this->error(__('Invalid parameters'));
  178. }
  179. }
  180. /**
  181. * 发送测试邮件
  182. * @internal
  183. */
  184. public function emailtest()
  185. {
  186. $row = $this->request->post('row/a');
  187. \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
  188. $receiver = $this->request->request("receiver");
  189. $email = new Email;
  190. $result = $email
  191. ->to($receiver)
  192. ->subject(__("This is a test mail"))
  193. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  194. ->send();
  195. if ($result) {
  196. $this->success();
  197. } else {
  198. $this->error($email->getError());
  199. }
  200. }
  201. }