Config.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Cache;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Validate;
  10. /**
  11. * 系统配置
  12. *
  13. * @icon fa fa-cogs
  14. * @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
  15. */
  16. class Config extends Backend
  17. {
  18. /**
  19. * @var \app\common\model\Config
  20. */
  21. protected $model = null;
  22. protected $noNeedRight = ['check', 'rulelist', 'selectpage', 'get_fields_list'];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('Config');
  27. ConfigModel::event('before_write', function ($row) {
  28. if (isset($row['name']) && $row['name'] == 'name' && preg_match("/fast" . "admin/i", $row['value'])) {
  29. throw new Exception(__("Site name incorrect"));
  30. }
  31. });
  32. }
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. $siteList = [];
  39. $groupList = ConfigModel::getGroupList();
  40. foreach ($groupList as $k => $v) {
  41. $siteList[$k]['name'] = $k;
  42. $siteList[$k]['title'] = $v;
  43. $siteList[$k]['list'] = [];
  44. }
  45. foreach ($this->model->all() as $k => $v) {
  46. if (!isset($siteList[$v['group']])) {
  47. continue;
  48. }
  49. $value = $v->toArray();
  50. $value['title'] = __($value['title']);
  51. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  52. $value['value'] = explode(',', $value['value']);
  53. }
  54. $value['content'] = json_decode($value['content'], true);
  55. $value['tip'] = htmlspecialchars($value['tip']);
  56. $siteList[$v['group']]['list'][] = $value;
  57. }
  58. $index = 0;
  59. foreach ($siteList as $k => &$v) {
  60. $v['active'] = !$index ? true : false;
  61. $index++;
  62. }
  63. $this->view->assign('siteList', $siteList);
  64. $this->view->assign('typeList', ConfigModel::getTypeList());
  65. $this->view->assign('ruleList', ConfigModel::getRegexList());
  66. $this->view->assign('groupList', ConfigModel::getGroupList());
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $this->token();
  76. $params = $this->request->post("row/a", [], 'trim');
  77. if ($params) {
  78. foreach ($params as $k => &$v) {
  79. $v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;
  80. }
  81. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
  82. $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  83. } else {
  84. $params['content'] = '';
  85. }
  86. try {
  87. $result = $this->model->create($params);
  88. } catch (Exception $e) {
  89. $this->error($e->getMessage());
  90. }
  91. if ($result !== false) {
  92. try {
  93. $this->refreshFile();
  94. } catch (Exception $e) {
  95. $this->error($e->getMessage());
  96. }
  97. $this->success();
  98. } else {
  99. $this->error($this->model->getError());
  100. }
  101. }
  102. $this->error(__('Parameter %s can not be empty', ''));
  103. }
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. * @param null $ids
  109. */
  110. public function edit($ids = null)
  111. {
  112. if ($this->request->isPost()) {
  113. $this->token();
  114. $row = $this->request->post("row/a", [], 'trim');
  115. if ($row) {
  116. $configList = [];
  117. foreach ($this->model->all() as $v) {
  118. if (isset($row[$v['name']])) {
  119. $value = $row[$v['name']];
  120. if (is_array($value) && isset($value['field'])) {
  121. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  122. } else {
  123. $value = is_array($value) ? implode(',', $value) : $value;
  124. }
  125. $v['value'] = $value;
  126. $configList[] = $v->toArray();
  127. }
  128. }
  129. try {
  130. $this->model->allowField(true)->saveAll($configList);
  131. } catch (Exception $e) {
  132. $this->error($e->getMessage());
  133. }
  134. try {
  135. $this->refreshFile();
  136. } catch (Exception $e) {
  137. $this->error($e->getMessage());
  138. }
  139. $this->success();
  140. }
  141. $this->error(__('Parameter %s can not be empty', ''));
  142. }
  143. }
  144. /**
  145. * 删除
  146. * @param string $ids
  147. */
  148. public function del($ids = "")
  149. {
  150. $name = $this->request->post('name');
  151. $config = ConfigModel::getByName($name);
  152. if ($name && $config) {
  153. try {
  154. $config->delete();
  155. $this->refreshFile();
  156. } catch (Exception $e) {
  157. $this->error($e->getMessage());
  158. }
  159. $this->success();
  160. } else {
  161. $this->error(__('Invalid parameters'));
  162. }
  163. }
  164. /**
  165. * 刷新配置文件
  166. */
  167. protected function refreshFile()
  168. {
  169. $config = [];
  170. foreach ($this->model->all() as $k => $v) {
  171. $value = $v->toArray();
  172. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  173. $value['value'] = explode(',', $value['value']);
  174. }
  175. if ($value['type'] == 'array') {
  176. $value['value'] = (array)json_decode($value['value'], true);
  177. }
  178. $config[$value['name']] = $value['value'];
  179. }
  180. file_put_contents(
  181. CONF_PATH . 'extra' . DS . 'site.php',
  182. '<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
  183. );
  184. }
  185. /**
  186. * 检测配置项是否存在
  187. * @internal
  188. */
  189. public function check()
  190. {
  191. $params = $this->request->post("row/a");
  192. if ($params) {
  193. $config = $this->model->get($params);
  194. if (!$config) {
  195. $this->success();
  196. } else {
  197. $this->error(__('Name already exist'));
  198. }
  199. } else {
  200. $this->error(__('Invalid parameters'));
  201. }
  202. }
  203. /**
  204. * 规则列表
  205. * @internal
  206. */
  207. public function rulelist()
  208. {
  209. //主键
  210. $primarykey = $this->request->request("keyField");
  211. //主键值
  212. $keyValue = $this->request->request("keyValue", "");
  213. $keyValueArr = array_filter(explode(',', $keyValue));
  214. $regexList = \app\common\model\Config::getRegexList();
  215. $list = [];
  216. foreach ($regexList as $k => $v) {
  217. if ($keyValueArr) {
  218. if (in_array($k, $keyValueArr)) {
  219. $list[] = ['id' => $k, 'name' => $v];
  220. }
  221. } else {
  222. $list[] = ['id' => $k, 'name' => $v];
  223. }
  224. }
  225. return json(['list' => $list]);
  226. }
  227. /**
  228. * 发送测试邮件
  229. * @internal
  230. */
  231. public function emailtest()
  232. {
  233. $row = $this->request->post('row/a');
  234. $receiver = $this->request->post("receiver");
  235. if ($receiver) {
  236. if (!Validate::is($receiver, "email")) {
  237. $this->error(__('Please input correct email'));
  238. }
  239. \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
  240. $email = new Email;
  241. $result = $email
  242. ->to($receiver)
  243. ->subject(__("This is a test mail"))
  244. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  245. ->send();
  246. if ($result) {
  247. $this->success();
  248. } else {
  249. $this->error($email->getError());
  250. }
  251. } else {
  252. $this->error(__('Invalid parameters'));
  253. }
  254. }
  255. public function selectpage()
  256. {
  257. $id = $this->request->get("id/d");
  258. $config = \app\common\model\Config::get($id);
  259. if (!$config) {
  260. $this->error(__('Invalid parameters'));
  261. }
  262. $setting = $config['setting'];
  263. //自定义条件
  264. $custom = isset($setting['conditions']) ? (array)json_decode($setting['conditions'], true) : [];
  265. $custom = array_filter($custom);
  266. $this->request->request(['showField' => $setting['field'], 'keyField' => $setting['primarykey'], 'custom' => $custom, 'searchField' => [$setting['field'], $setting['primarykey']]]);
  267. $this->model = \think\Db::connect()->setTable($setting['table']);
  268. return parent::selectpage();
  269. }
  270. /**
  271. * 获取表列表
  272. * @internal
  273. */
  274. public function get_table_list()
  275. {
  276. $tableList = [];
  277. $dbname = \think\Config::get('database.database');
  278. $tableList = \think\Db::query("SELECT `TABLE_NAME` AS `name`,`TABLE_COMMENT` AS `title` FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` = '{$dbname}';");
  279. $this->success('', null, ['tableList' => $tableList]);
  280. }
  281. /**
  282. * 获取表字段列表
  283. * @internal
  284. */
  285. public function get_fields_list()
  286. {
  287. $table = $this->request->request('table');
  288. $dbname = \think\Config::get('database.database');
  289. //从数据库中获取表字段信息
  290. $sql = "SELECT `COLUMN_NAME` AS `name`,`COLUMN_COMMENT` AS `title`,`DATA_TYPE` AS `type` FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION";
  291. //加载主表的列
  292. $fieldList = Db::query($sql, [$dbname, $table]);
  293. $this->success("", null, ['fieldList' => $fieldList]);
  294. }
  295. }