Config.php 7.1 KB

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