Config.php 6.7 KB

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