配置数据表的增删改查和刷新更新配置文件
<?php
namespace app\controller\admin\general;
use \think\facade\Db;//Db类库
use \think\facade\Config;//配置
use think\Request;//请求类
use \think\facade\Lang;//加载语言包
use app\controller\common\Admin;
/**
* 系统配置
*
* @icon fa fa-cogs
* @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
*/
class Configs extends Admin
{
/**
* @var \app\common\model\Config
*/
/**
* 查看
*/
public function index()
{
$configgroup= Db::name("Config")->where("name","configgroup")->find();
$configgroup= json_decode($configgroup["value"],true);
// $configarray= Db::name("Config")->select();
$siteList = [];
foreach ($configgroup as $k => $v) {
$siteList[$k]['name'] = $k;
$siteList[$k]['title'] = $v;
$siteList[$k]['list'] = [];
}
foreach (Db::name("Config")->select() as $k => $v) {
if (!isset($siteList[$v['group']])) {
continue;
}
$value = $v;
$value['title'] = $value['title'] ;
if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
$value['value'] = explode(',', $value['value']);
}
$value['content'] = json_decode($value['content'], true);
$value['tip'] = htmlspecialchars($value['tip']);
$siteList[$v['group']]['list'][] = $value;
}
$index = 0;
foreach ($siteList as $k => &$v) {
$v['active'] = !$index ? true : false;
$index++;
}
$typeList = [
'string' => Config::load('String'),
'text' => Config::load('Text'),
'editor' => Config::load('Editor'),
'number' => Config::load('Number'),
'date' => Config::load('Date'),
'time' => Config::load('Time'),
'datetime' => Config::load('Datetime'),
'datetimerange' => Config::load('Datetimerange'),
'select' => Config::load('Select'),
'selects' => Config::load('Selects'),
'image' => Config::load('Image'),
'images' => Config::load('Images'),
'file' => Config::load('File'),
'files' => Config::load('Files'),
'switch' => Config::load('Switch'),
'checkbox' => Config::load('Checkbox'),
'radio' => Config::load('Radio'),
'city' => Config::load('City'),
'selectpage' => Config::load('Selectpage'),
'selectpages' => Config::load('Selectpages'),
'array' => Config::load('Array'),
'custom' => Config::load('Custom'),
];
$regexList = [
'required' => '必选',
'digits' => '数字',
'letters' => '字母',
'date' => '日期',
'time' => '时间',
'email' => '邮箱',
'url' => '网址',
'qq' => 'QQ号',
'IDcard' => '身份证',
'tel' => '座机电话',
'mobile' => '手机号',
'zipcode' => '邮编',
'chinese' => '中文',
'username' => '用户名',
'password' => '密码'
];
return $this->success("成功",['configgroup'=>$configgroup,'siteList'=>$siteList,"typeList"=>$typeList,"ruleList"=>$regexList]);
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$this->token();
$params = $this->request->post("", [], 'trim');
if ($params) {
foreach ($params as $k => &$v) {
$v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;
}
if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
$params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
} else {
$params['content'] = '';
}
$result = Db::name("Config")->insert($params);
if ($result !== false) {
//刷新配置文件
$this->refreshFile();
$this->success();
} else {
$this->error("失败");
}
}
$this->error("没有参数哦");
}
}
/**
* 编辑
* @param null $ids
*/
public function edit($ids = null)
{
if ($this->request->isPost()) {
$this->token();
$row = $this->request->post("", [], 'trim');
if ($row) {
$configList = [];
foreach (Db::name("Config")->select() as $v) {
if (isset($row[$v['name']])) {
$value = $row[$v['name']];
if (is_array($value) && isset($value['field'])) {
$value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
} else {
$value = is_array($value) ? implode(',', $value) : $value;
}
$v['value'] = $value;
$configList[] = $v;
}
}
Db::name("Config")->update($configList);
//刷新配置文件
$this->refreshFile();
$this->success();
}
$this->error(__('Parameter %s can not be empty', ''));
}
}
/**
* 删除
* @param string $ids
*/
public function del($ids = "")
{
$name = $this->request->post('name');
$config = Db::name("Config")->where("name",$name);
if ($name && $config) {
$config->delete();
$this->refreshFile();
$this->success();
} else {
$this->error(__('Invalid parameters'));
}
}
/**
* 刷新配置文件
*/
public function refreshFile()
{
$config=[];
$configarray= Db::name("Config")->select();
foreach ($configarray as $k => $v) {
$value = $v ;
if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
$value['value'] = explode(',', $value['value']);
}
if ($value['type'] == 'array') {
$value['value'] = (array)json_decode($value['value'], true);
}
$config[$value['name']] = $value['value'];
}
file_put_contents(config_path() . 'extra/site.php',
'<?php' . "\n\nreturn " . var_export($config, true) . ";"
);
}
/**
* 检测配置项是否存在
* @internal
*/
public function check()
{
$params = $this->request->post();
if ($params) {
$config = Db::name("Config")->where($params)->find();
if (!$config) {
$this->success();
} else {
$this->error(__('Name already exist'));
}
} else {
$this->error(__('Invalid parameters'));
}
}
/**
* 规则列表
* @internal
*/
public function rulelist()
{
//主键
$primarykey = $this->request->request("keyField");
//主键值
$keyValue = $this->request->request("keyValue", "");
$keyValueArr = array_filter(explode(',', $keyValue));
$regexList = \app\common\model\Config::getRegexList();
$list = [];
foreach ($regexList as $k => $v) {
if ($keyValueArr) {
if (in_array($k, $keyValueArr)) {
$list[] = ['id' => $k, 'name' => $v];
}
} else {
$list[] = ['id' => $k, 'name' => $v];
}
}
return json(['list' => $list]);
}
/**
* 发送测试邮件
* @internal
*/
public function emailtest()
{
$row = $this->request->post('row/a');
$receiver = $this->request->post("receiver");
if ($receiver) {
if (!Validate::is($receiver, "email")) {
$this->error(__('Please input correct email'));
}
\think\Config::set('site', array_merge(\think\Config::get('site'), $row));
$email = new Email;
$result = $email
->to($receiver)
->subject(__("This is a test mail"))
->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
->send();
if ($result) {
$this->success();
} else {
$this->error($email->getError());
}
} else {
$this->error(__('Invalid parameters'));
}
}
public function selectpage()
{
}
/**
* 获取表列表
* @internal
*/
public function get_table_list()
{
$tableList = [];
$dbname = \think\Config::get('database.database');
$tableList = \think\Db::query("SELECT `TABLE_NAME` AS `name`,`TABLE_COMMENT` AS `title` FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` = '{$dbname}';");
$this->success('', null, ['tableList' => $tableList]);
}
/**
* 获取表字段列表
* @internal
*/
public function get_fields_list()
{
$table = $this->request->request('table');
$dbname = \think\Config::get('database.database');
//从数据库中获取表字段信息
$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";
//加载主表的列
$fieldList = Db::query($sql, [$dbname, $table]);
$this->success("", null, ['fieldList' => $fieldList]);
}
}