Config.php 7.2 KB

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