Config.php 12 KB

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