|
// +------------------------------------------------+
namespace app\common\model;
use think\Cache;
class Config extends _Init
{
/**
* 模型初始化【事件注册】
*/
protected static function init()
{
self::afterWrite(function () {
Cache::clear();
});
}
/**
* 获取器
*/
protected function getTypeTextAttr($value, $data)
{
return Config::getValue($data['type'], 'config_type_list');
}
protected function getGroupTextAttr($value, $data)
{
return Config::getValue($data['group'], 'config_group_list');
}
/**
* 配置列表,优化输出
*/
protected function getValueTextAttr($value, $data)
{
return preg_replace('/\r\n/', "
", $data['value']);
}
/**
* 格式化枚举类型的配置
*/
protected function getExtraArrayAttr($value, $data)
{
$array = preg_split('/[\r\n]+/', trim($data['extra'], "\r\n"));
$enum = [];
if (strpos($data['extra'], ':')) {
foreach ($array as $val) {
list($k, $v) = explode(':', $val, 2);
$enum[$k] = $v;
}
} else {
$enum = $array;
}
return $enum;
}
/**
* 获取配置内容
* @param string $key
* @param string $type
* @return string
*/
public static function getValue($key, $type = null)
{
if ($key == -1) {
return '已删除';
}
if (!is_null($type) && is_string($type)) {
$res = \think\Config::get($type);
$res = isset($res[$key]) ? $res[$key] : '';
} elseif (!is_null($type) && is_array($type)) {
$res = isset($type[$key]) ? $type[$key] : '';
} else {
switch ($key) {
case 0:$res = '禁用';
break;
case 1:$res = '正常';
break;
case 2:$res = '待审核';
break;
case 3:$res = '被驳回';
break;
default:$res = '';
break;
}
}
return $res;
}
}