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