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