Config.php 6.7 KB

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