Config.php 6.7 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. /**
  8. * 系统配置
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Config extends Backend
  13. {
  14. protected $model = null;
  15. protected $noNeedRight = ['check'];
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = model('Config');
  20. }
  21. public function index()
  22. {
  23. $siteList = [];
  24. $groupList = ConfigModel::getGroupList();
  25. foreach ($groupList as $k => $v)
  26. {
  27. $siteList[$k]['name'] = $k;
  28. $siteList[$k]['title'] = $v;
  29. $siteList[$k]['list'] = [];
  30. }
  31. foreach ($this->model->all() as $k => $v)
  32. {
  33. if (!isset($siteList[$v['group']]))
  34. {
  35. continue;
  36. }
  37. $value = $v->toArray();
  38. $value['title'] = __($value['title']);
  39. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio']))
  40. {
  41. $value['value'] = explode(',', $value['value']);
  42. }
  43. if ($value['type'] == 'array')
  44. {
  45. $value['value'] = (array) json_decode($value['value'], TRUE);
  46. }
  47. $value['content'] = json_decode($value['content'], TRUE);
  48. $siteList[$v['group']]['list'][] = $value;
  49. }
  50. $index = 0;
  51. foreach ($siteList as $k => &$v)
  52. {
  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. {
  68. $params = $this->request->post("row/a");
  69. if ($params)
  70. {
  71. foreach ($params as $k => &$v)
  72. {
  73. $v = is_array($v) ? implode(',', $v) : $v;
  74. }
  75. try
  76. {
  77. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array']))
  78. {
  79. $params['content'] = ConfigModel::decode($params['content']);
  80. }
  81. else
  82. {
  83. $params['content'] = '';
  84. }
  85. $result = $this->model->create($params);
  86. if ($result !== false)
  87. {
  88. try
  89. {
  90. $this->refreshFile();
  91. $this->success();
  92. }
  93. catch (Exception $e)
  94. {
  95. $this->error($e->getMessage());
  96. }
  97. }
  98. else
  99. {
  100. $this->error($this->model->getError());
  101. }
  102. }
  103. catch (Exception $e)
  104. {
  105. $this->error($e->getMessage());
  106. }
  107. }
  108. $this->error(__('Parameter %s can not be empty', ''));
  109. }
  110. return $this->view->fetch();
  111. }
  112. public function edit($ids = NULL)
  113. {
  114. if ($this->request->isPost())
  115. {
  116. $row = $this->request->post("row/a");
  117. if ($row)
  118. {
  119. $configList = [];
  120. foreach ($this->model->all() as $v)
  121. {
  122. if (isset($row[$v['name']]))
  123. {
  124. $value = $row[$v['name']];
  125. if (is_array($value) && isset($value['field']))
  126. {
  127. $value = json_encode(\app\common\model\Config::getArrayData($value), JSON_UNESCAPED_UNICODE);
  128. }
  129. else
  130. {
  131. $value = is_array($value) ? implode(',', $value) : $value;
  132. }
  133. $v['value'] = $value;
  134. $configList[] = $v;
  135. }
  136. }
  137. $this->model->allowField(true)->saveAll($configList);
  138. try
  139. {
  140. $this->refreshFile();
  141. $this->success();
  142. }
  143. catch (Exception $e)
  144. {
  145. $this->error($e->getMessage());
  146. }
  147. }
  148. $this->error(__('Parameter %s can not be empty', ''));
  149. }
  150. }
  151. protected function refreshFile()
  152. {
  153. $config = [];
  154. foreach ($this->model->all() as $k => $v)
  155. {
  156. $value = $v->toArray();
  157. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files']))
  158. {
  159. $value['value'] = explode(',', $value['value']);
  160. }
  161. if ($value['type'] == 'array')
  162. {
  163. $value['value'] = (array) json_decode($value['value'], TRUE);
  164. }
  165. $config[$value['name']] = $value['value'];
  166. }
  167. file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
  168. }
  169. /**
  170. * @internal
  171. */
  172. public function check()
  173. {
  174. $params = $this->request->post("row/a");
  175. if ($params)
  176. {
  177. $config = $this->model->get($params);
  178. if (!$config)
  179. {
  180. return json(['ok' => '']);
  181. }
  182. else
  183. {
  184. return json(['error' => __('Name already exist')]);
  185. }
  186. }
  187. else
  188. {
  189. return json(['error' => __('Invalid parameters')]);
  190. }
  191. }
  192. /**
  193. * 发送测试邮件
  194. * @internal
  195. */
  196. public function emailtest()
  197. {
  198. $receiver = $this->request->request("receiver");
  199. $email = new Email;
  200. $result = $email
  201. ->to($receiver)
  202. ->subject(__("This is a test mail"))
  203. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  204. ->send();
  205. if ($result)
  206. {
  207. $this->success();
  208. }
  209. else
  210. {
  211. $this->error($email->getError());
  212. }
  213. }
  214. }