优化配置目录

pull/197/head
alias 2020-05-20 14:18:43 +08:00
parent cf4949db77
commit 36dfe45715
2 changed files with 241 additions and 241 deletions

View File

@ -197,7 +197,7 @@ class Install extends Command
$adminFile = ROOT_PATH . 'public' . DS . 'admin.php'; $adminFile = ROOT_PATH . 'public' . DS . 'admin.php';
// 数据库配置文件 // 数据库配置文件
$dbConfigFile = APP_PATH . 'database.php'; $dbConfigFile = CONF_PATH . 'database.php';
$config = @file_get_contents($dbConfigFile); $config = @file_get_contents($dbConfigFile);
$callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) { $callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) {
$field = "mysql" . ucfirst($matches[1]); $field = "mysql" . ucfirst($matches[1]);
@ -232,7 +232,7 @@ class Install extends Command
//修改站点名称 //修改站点名称
if ($siteName != __('My Website')) { if ($siteName != __('My Website')) {
$instance->name('config')->where('name', 'name')->update(['value' => $siteName]); $instance->name('config')->where('name', 'name')->update(['value' => $siteName]);
$configFile = APP_PATH . 'extra' . DS . 'site.php'; $configFile = CONF_PATH . 'extra' . DS . 'site.php';
$config = include $configFile; $config = include $configFile;
$configList = $instance->name("config")->select(); $configList = $instance->name("config")->select();
foreach ($configList as $k => $value) { foreach ($configList as $k => $value) {
@ -240,7 +240,7 @@ class Install extends Command
$value['value'] = explode(',', $value['value']); $value['value'] = explode(',', $value['value']);
} }
if ($value['type'] == 'array') { if ($value['type'] == 'array') {
$value['value'] = (array)json_decode($value['value'], true); $value['value'] = (array) json_decode($value['value'], true);
} }
$config[$value['name']] = $value['value']; $config[$value['name']] = $value['value'];
} }
@ -271,7 +271,7 @@ class Install extends Command
]; ];
//数据库配置文件 //数据库配置文件
$dbConfigFile = APP_PATH . 'database.php'; $dbConfigFile = CONF_PATH . 'database.php';
if (version_compare(PHP_VERSION, '5.5.0', '<')) { if (version_compare(PHP_VERSION, '5.5.0', '<')) {
throw new Exception(__("The current version %s is too low, please use PHP 5.5 or higher", PHP_VERSION)); throw new Exception(__("The current version %s is too low, please use PHP 5.5 or higher", PHP_VERSION));

View File

@ -17,252 +17,252 @@ use think\Validate;
class Config extends Backend class Config extends Backend
{ {
/** /**
* @var \app\common\model\Config * @var \app\common\model\Config
*/ */
protected $model = null; protected $model = null;
protected $noNeedRight = ['check', 'rulelist']; protected $noNeedRight = ['check', 'rulelist'];
public function _initialize() public function _initialize()
{ {
parent::_initialize(); parent::_initialize();
$this->model = model('Config'); $this->model = model('Config');
ConfigModel::event('before_write', function ($row) { ConfigModel::event('before_write', function ($row) {
if (isset($row['name']) && $row['name'] == 'name' && preg_match("/fast" . "admin/i", $row['value'])) { if (isset($row['name']) && $row['name'] == 'name' && preg_match("/fast" . "admin/i", $row['value'])) {
throw new Exception(__("Site name incorrect")); throw new Exception(__("Site name incorrect"));
} }
}); });
} }
/** /**
* 查看 * 查看
*/ */
public function index() public function index()
{ {
$siteList = []; $siteList = [];
$groupList = ConfigModel::getGroupList(); $groupList = ConfigModel::getGroupList();
foreach ($groupList as $k => $v) { foreach ($groupList as $k => $v) {
$siteList[$k]['name'] = $k; $siteList[$k]['name'] = $k;
$siteList[$k]['title'] = $v; $siteList[$k]['title'] = $v;
$siteList[$k]['list'] = []; $siteList[$k]['list'] = [];
} }
foreach ($this->model->all() as $k => $v) { foreach ($this->model->all() as $k => $v) {
if (!isset($siteList[$v['group']])) { if (!isset($siteList[$v['group']])) {
continue; continue;
} }
$value = $v->toArray(); $value = $v->toArray();
$value['title'] = __($value['title']); $value['title'] = __($value['title']);
if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) { if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
$value['value'] = explode(',', $value['value']); $value['value'] = explode(',', $value['value']);
} }
$value['content'] = json_decode($value['content'], true); $value['content'] = json_decode($value['content'], true);
$value['tip'] = htmlspecialchars($value['tip']); $value['tip'] = htmlspecialchars($value['tip']);
$siteList[$v['group']]['list'][] = $value; $siteList[$v['group']]['list'][] = $value;
} }
$index = 0; $index = 0;
foreach ($siteList as $k => &$v) { foreach ($siteList as $k => &$v) {
$v['active'] = !$index ? true : false; $v['active'] = !$index ? true : false;
$index++; $index++;
} }
$this->view->assign('siteList', $siteList); $this->view->assign('siteList', $siteList);
$this->view->assign('typeList', ConfigModel::getTypeList()); $this->view->assign('typeList', ConfigModel::getTypeList());
$this->view->assign('ruleList', ConfigModel::getRegexList()); $this->view->assign('ruleList', ConfigModel::getRegexList());
$this->view->assign('groupList', ConfigModel::getGroupList()); $this->view->assign('groupList', ConfigModel::getGroupList());
return $this->view->fetch(); return $this->view->fetch();
} }
/** /**
* 添加 * 添加
*/ */
public function add() public function add()
{ {
if ($this->request->isPost()) { if ($this->request->isPost()) {
$this->token(); $this->token();
$params = $this->request->post("row/a", [], 'trim'); $params = $this->request->post("row/a", [], 'trim');
if ($params) { if ($params) {
foreach ($params as $k => &$v) { foreach ($params as $k => &$v) {
$v = is_array($v) ? implode(',', $v) : $v; $v = is_array($v) ? implode(',', $v) : $v;
} }
if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) { if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
$params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE); $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
} else { } else {
$params['content'] = ''; $params['content'] = '';
} }
try { try {
$result = $this->model->create($params); $result = $this->model->create($params);
} catch (Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
if ($result !== false) { if ($result !== false) {
try { try {
$this->refreshFile(); $this->refreshFile();
} catch (Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
$this->success(); $this->success();
} else { } else {
$this->error($this->model->getError()); $this->error($this->model->getError());
} }
} }
$this->error(__('Parameter %s can not be empty', '')); $this->error(__('Parameter %s can not be empty', ''));
} }
return $this->view->fetch(); return $this->view->fetch();
} }
/** /**
* 编辑 * 编辑
* @param null $ids * @param null $ids
*/ */
public function edit($ids = null) public function edit($ids = null)
{ {
if ($this->request->isPost()) { if ($this->request->isPost()) {
$this->token(); $this->token();
$row = $this->request->post("row/a", [], 'trim'); $row = $this->request->post("row/a", [], 'trim');
if ($row) { if ($row) {
$configList = []; $configList = [];
foreach ($this->model->all() as $v) { foreach ($this->model->all() as $v) {
if (isset($row[$v['name']])) { if (isset($row[$v['name']])) {
$value = $row[$v['name']]; $value = $row[$v['name']];
if (is_array($value) && isset($value['field'])) { if (is_array($value) && isset($value['field'])) {
$value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE); $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
} else { } else {
$value = is_array($value) ? implode(',', $value) : $value; $value = is_array($value) ? implode(',', $value) : $value;
} }
$v['value'] = $value; $v['value'] = $value;
$configList[] = $v->toArray(); $configList[] = $v->toArray();
} }
} }
try { try {
$this->model->allowField(true)->saveAll($configList); $this->model->allowField(true)->saveAll($configList);
} catch (Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
try { try {
$this->refreshFile(); $this->refreshFile();
} catch (Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
$this->success(); $this->success();
} }
$this->error(__('Parameter %s can not be empty', '')); $this->error(__('Parameter %s can not be empty', ''));
} }
} }
/** /**
* 删除 * 删除
* @param string $ids * @param string $ids
*/ */
public function del($ids = "") public function del($ids = "")
{ {
$name = $this->request->post('name'); $name = $this->request->post('name');
$config = ConfigModel::getByName($name); $config = ConfigModel::getByName($name);
if ($name && $config) { if ($name && $config) {
try { try {
$config->delete(); $config->delete();
$this->refreshFile(); $this->refreshFile();
} catch (Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
$this->success(); $this->success();
} else { } else {
$this->error(__('Invalid parameters')); $this->error(__('Invalid parameters'));
} }
} }
/** /**
* 刷新配置文件 * 刷新配置文件
*/ */
protected function refreshFile() protected function refreshFile()
{ {
$config = []; $config = [];
foreach ($this->model->all() as $k => $v) { foreach ($this->model->all() as $k => $v) {
$value = $v->toArray(); $value = $v->toArray();
if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) { if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
$value['value'] = explode(',', $value['value']); $value['value'] = explode(',', $value['value']);
} }
if ($value['type'] == 'array') { if ($value['type'] == 'array') {
$value['value'] = (array)json_decode($value['value'], true); $value['value'] = (array) json_decode($value['value'], true);
} }
$config[$value['name']] = $value['value']; $config[$value['name']] = $value['value'];
} }
file_put_contents( file_put_contents(
APP_PATH . 'extra' . DS . 'site.php', CONF_PATH . 'extra' . DS . 'site.php',
'<?php' . "\n\nreturn " . var_export($config, true) . ";" '<?php' . "\n\nreturn " . var_export($config, true) . ";"
); );
} }
/** /**
* 检测配置项是否存在 * 检测配置项是否存在
* @internal * @internal
*/ */
public function check() public function check()
{ {
$params = $this->request->post("row/a"); $params = $this->request->post("row/a");
if ($params) { if ($params) {
$config = $this->model->get($params); $config = $this->model->get($params);
if (!$config) { if (!$config) {
return $this->success(); return $this->success();
} else { } else {
return $this->error(__('Name already exist')); return $this->error(__('Name already exist'));
} }
} else { } else {
return $this->error(__('Invalid parameters')); return $this->error(__('Invalid parameters'));
} }
} }
/** /**
* 规则列表 * 规则列表
* @internal * @internal
*/ */
public function rulelist() public function rulelist()
{ {
//主键 //主键
$primarykey = $this->request->request("keyField"); $primarykey = $this->request->request("keyField");
//主键值 //主键值
$keyValue = $this->request->request("keyValue", ""); $keyValue = $this->request->request("keyValue", "");
$keyValueArr = array_filter(explode(',', $keyValue)); $keyValueArr = array_filter(explode(',', $keyValue));
$regexList = \app\common\model\Config::getRegexList(); $regexList = \app\common\model\Config::getRegexList();
$list = []; $list = [];
foreach ($regexList as $k => $v) { foreach ($regexList as $k => $v) {
if ($keyValueArr) { if ($keyValueArr) {
if (in_array($k, $keyValueArr)) { if (in_array($k, $keyValueArr)) {
$list[] = ['id' => $k, 'name' => $v]; $list[] = ['id' => $k, 'name' => $v];
} }
} else { } else {
$list[] = ['id' => $k, 'name' => $v]; $list[] = ['id' => $k, 'name' => $v];
} }
} }
return json(['list' => $list]); return json(['list' => $list]);
} }
/** /**
* 发送测试邮件 * 发送测试邮件
* @internal * @internal
*/ */
public function emailtest() public function emailtest()
{ {
$row = $this->request->post('row/a'); $row = $this->request->post('row/a');
$receiver = $this->request->post("receiver"); $receiver = $this->request->post("receiver");
if ($receiver) { if ($receiver) {
if (!Validate::is($receiver, "email")) { if (!Validate::is($receiver, "email")) {
$this->error(__('Please input correct email')); $this->error(__('Please input correct email'));
} }
\think\Config::set('site', array_merge(\think\Config::get('site'), $row)); \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
$email = new Email; $email = new Email;
$result = $email $result = $email
->to($receiver) ->to($receiver)
->subject(__("This is a test mail")) ->subject(__("This is a test mail"))
->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>') ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
->send(); ->send();
if ($result) { if ($result) {
$this->success(); $this->success();
} else { } else {
$this->error($email->getError()); $this->error($email->getError());
} }
} else { } else {
return $this->error(__('Invalid parameters')); return $this->error(__('Invalid parameters'));
} }
} }
} }