Config.php 12 KB

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