Config.php 10 KB

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