Config.php 9.9 KB

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