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