Config.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio']))
  39. {
  40. $value['value'] = explode(',', $value['value']);
  41. }
  42. if ($value['type'] == 'array')
  43. {
  44. $value['value'] = (array) json_decode($value['value'], TRUE);
  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. {
  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. {
  67. $this->code = -1;
  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 ($params['content'] && in_array($params['type'], ['select', 'selects', 'checkbox', 'radio']))
  78. {
  79. $content = explode("\r\n", $params['content']);
  80. $arr = [];
  81. foreach ($content as $k => &$v)
  82. {
  83. if (stripos($v, "|") !== false)
  84. {
  85. $item = explode('|', $v);
  86. $arr[$item[0]] = $item[1];
  87. }
  88. }
  89. $params['content'] = $arr ? json_encode($arr, JSON_UNESCAPED_UNICODE) : '';
  90. }
  91. else
  92. {
  93. $params['content'] = '';
  94. }
  95. $result = $this->model->create($params);
  96. if ($result !== false)
  97. {
  98. try
  99. {
  100. $this->refreshFile();
  101. $this->code = 1;
  102. }
  103. catch (Exception $e)
  104. {
  105. $this->msg = $e->getMessage();
  106. }
  107. }
  108. else
  109. {
  110. $this->msg = $this->model->getError();
  111. }
  112. }
  113. catch (Exception $e)
  114. {
  115. $this->msg = $e->getMessage();
  116. }
  117. }
  118. else
  119. {
  120. $this->msg = __('Parameter %s can not be empty', '');
  121. }
  122. return;
  123. }
  124. return $this->view->fetch();
  125. }
  126. public function edit($ids = NULL)
  127. {
  128. $this->code = -1;
  129. if ($this->request->isPost())
  130. {
  131. $params = $this->request->post("row/a");
  132. if ($params)
  133. {
  134. $configList = [];
  135. foreach ($this->model->all() as $k => $v)
  136. {
  137. if (isset($params[$v['name']]))
  138. {
  139. if ($v['type'] == 'array')
  140. {
  141. $fieldarr = $valuearr = [];
  142. $field = $params[$v['name']]['field'];
  143. $value = $params[$v['name']]['value'];
  144. foreach ($field as $m => $n)
  145. {
  146. if ($n != '')
  147. {
  148. $fieldarr[] = $field[$m];
  149. $valuearr[] = $value[$m];
  150. }
  151. }
  152. $params[$v['name']] = array_combine($fieldarr, $valuearr);
  153. $value = json_encode($params[$v['name']], JSON_UNESCAPED_UNICODE);
  154. }
  155. else
  156. {
  157. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  158. }
  159. $configList[] = ['id' => $v['id'], 'value' => $value];
  160. }
  161. }
  162. $this->model->saveAll($configList);
  163. try
  164. {
  165. $this->refreshFile();
  166. $this->code = 1;
  167. }
  168. catch (Exception $e)
  169. {
  170. $this->msg = $e->getMessage();
  171. }
  172. }
  173. else
  174. {
  175. $this->msg = __('Parameter %s can not be empty', '');
  176. }
  177. return;
  178. }
  179. }
  180. protected function refreshFile()
  181. {
  182. $config = [];
  183. foreach ($this->model->all() as $k => $v)
  184. {
  185. $value = $v->toArray();
  186. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files']))
  187. {
  188. $value['value'] = explode(',', $value['value']);
  189. }
  190. if ($value['type'] == 'array')
  191. {
  192. $value['value'] = (array) json_decode($value['value'], TRUE);
  193. }
  194. $config[$value['name']] = $value['value'];
  195. }
  196. file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
  197. }
  198. /**
  199. * @internal
  200. */
  201. public function check()
  202. {
  203. $params = $this->request->post("row/a");
  204. if ($params)
  205. {
  206. $config = $this->model->get($params);
  207. if (!$config)
  208. {
  209. return json(['ok' => '']);
  210. }
  211. else
  212. {
  213. return json(['error' => __('Name already exist')]);
  214. }
  215. }
  216. else
  217. {
  218. return json(['error' => __('Invalid parameters')]);
  219. }
  220. }
  221. /**
  222. * 发送测试邮件
  223. * @internal
  224. */
  225. public function emailtest()
  226. {
  227. $receiver = $this->request->request("receiver");
  228. $email = new Email;
  229. $result = $email
  230. ->to($receiver)
  231. ->subject(__("This is a test mail"))
  232. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  233. ->send();
  234. if ($result)
  235. {
  236. $this->code = 1;
  237. }
  238. else
  239. {
  240. $this->code = -1;
  241. $this->msg = $email->getError();
  242. }
  243. }
  244. }