Config.php 7.7 KB

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