Config.php 12 KB

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