Config.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /**
  23. * 查看
  24. */
  25. public function index()
  26. {
  27. $siteList = [];
  28. $groupList = ConfigModel::getGroupList();
  29. foreach ($groupList as $k => $v)
  30. {
  31. $siteList[$k]['name'] = $k;
  32. $siteList[$k]['title'] = $v;
  33. $siteList[$k]['list'] = [];
  34. }
  35. foreach ($this->model->all() as $k => $v)
  36. {
  37. if (!isset($siteList[$v['group']]))
  38. {
  39. continue;
  40. }
  41. $value = $v->toArray();
  42. $value['title'] = __($value['title']);
  43. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio']))
  44. {
  45. $value['value'] = explode(',', $value['value']);
  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'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  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. /**
  113. * 编辑
  114. * @param null $ids
  115. */
  116. public function edit($ids = NULL)
  117. {
  118. if ($this->request->isPost())
  119. {
  120. $row = $this->request->post("row/a");
  121. if ($row)
  122. {
  123. $configList = [];
  124. foreach ($this->model->all() as $v)
  125. {
  126. if (isset($row[$v['name']]))
  127. {
  128. $value = $row[$v['name']];
  129. if (is_array($value) && isset($value['field']))
  130. {
  131. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  132. }
  133. else
  134. {
  135. $value = is_array($value) ? implode(',', $value) : $value;
  136. }
  137. $v['value'] = $value;
  138. $configList[] = $v->toArray();
  139. }
  140. }
  141. $this->model->allowField(true)->saveAll($configList);
  142. try
  143. {
  144. $this->refreshFile();
  145. $this->success();
  146. }
  147. catch (Exception $e)
  148. {
  149. $this->error($e->getMessage());
  150. }
  151. }
  152. $this->error(__('Parameter %s can not be empty', ''));
  153. }
  154. }
  155. protected function refreshFile()
  156. {
  157. $config = [];
  158. foreach ($this->model->all() as $k => $v)
  159. {
  160. $value = $v->toArray();
  161. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files']))
  162. {
  163. $value['value'] = explode(',', $value['value']);
  164. }
  165. if ($value['type'] == 'array')
  166. {
  167. $value['value'] = (array) json_decode($value['value'], TRUE);
  168. }
  169. $config[$value['name']] = $value['value'];
  170. }
  171. file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
  172. }
  173. /**
  174. * @internal
  175. */
  176. public function check()
  177. {
  178. $params = $this->request->post("row/a");
  179. if ($params)
  180. {
  181. $config = $this->model->get($params);
  182. if (!$config)
  183. {
  184. return $this->success();
  185. }
  186. else
  187. {
  188. return $this->error(__('Name already exist'));
  189. }
  190. }
  191. else
  192. {
  193. return $this->error(__('Invalid parameters'));
  194. }
  195. }
  196. /**
  197. * 发送测试邮件
  198. * @internal
  199. */
  200. public function emailtest()
  201. {
  202. $receiver = $this->request->request("receiver");
  203. $email = new Email;
  204. $result = $email
  205. ->to($receiver)
  206. ->subject(__("This is a test mail"))
  207. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  208. ->send();
  209. if ($result)
  210. {
  211. $this->success();
  212. }
  213. else
  214. {
  215. $this->error($email->getError());
  216. }
  217. }
  218. }