Config.php 10 KB

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