86 lines
2.7 KiB
PHP
Executable File
86 lines
2.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\common\model;
|
|
|
|
use think\Model;
|
|
use think\Request;
|
|
use think\Config;
|
|
|
|
class Sysconfig extends Model {
|
|
|
|
use \app\common\traits\IndexModel;
|
|
|
|
/**
|
|
* 获取数据库中的配置列表
|
|
* @return array 配置数组
|
|
*/
|
|
public function configLists($where = null) {
|
|
$data = $this::all(function($query)use($where) {
|
|
$query->where(['stat' => 0])->field('type,name,value,extra');
|
|
if ($where) {
|
|
$query->where($where);
|
|
}
|
|
$query->order(['sort' => 'asc', 'id' => 'asc']);
|
|
});
|
|
$config = [];
|
|
if ($data && is_array($data)) {
|
|
foreach ($data as $value) {
|
|
$config[$value->data['name']] = self::parse($value->data['type'], $value->data['value'], $value->data['extra']);
|
|
}
|
|
}
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 根据配置类型解析配置
|
|
* @param integer $type 配置类型
|
|
* @param string $value 配置值
|
|
* @param string $extra 配置项
|
|
*/
|
|
private function parse($type, $value, $extra = '') {
|
|
switch ($type) {
|
|
// case 'number':
|
|
// $value = intval($value);
|
|
// break;
|
|
case 'json':
|
|
$value = json_decode($value, true);
|
|
break;
|
|
case 'array': //解析数组
|
|
$array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
|
|
if (strpos($value, ':')) {
|
|
$value = array();
|
|
foreach ($array as $val) {
|
|
list($k, $v) = explode(':', $val);
|
|
$value[$k] = $v;
|
|
}
|
|
} else {
|
|
$value = $array;
|
|
}
|
|
break;
|
|
// case 'enum': //解析数组
|
|
// $array = preg_split('/[,;\r\n]+/', trim($extra, ",;\r\n"));
|
|
// if (strpos($value, ':')) {
|
|
// $valarr = array();
|
|
// foreach ($array as $val) {
|
|
// list($k, $v) = explode(':', $val);
|
|
// $valarr[$k] = $v;
|
|
// }
|
|
// } else {
|
|
// $valarr = $array;
|
|
// }
|
|
// $value = isset($valarr[$value]) ? $valarr[$value] : $value;
|
|
// break;
|
|
case 'dump':
|
|
dump($value);
|
|
// die;
|
|
break;
|
|
default:
|
|
//dump($value);
|
|
// die;
|
|
break;
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
}
|