Files
xk-api-yii/admin/models/Config.php

74 lines
2.1 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace admin\models;
2024-12-19 19:20:27 +08:00
class Config extends \common\modelsgii\oldDb\Config
2024-09-19 11:32:39 +08:00
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
/**
* 写库和更新库时,时间自动完成
* 注意rules验证必填时可使用AttributeBehavior行为model的EVENT_BEFORE_VALIDATE事件
*/
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
'value' => time(),
],
];
}
/**
* ---------------------------------------
* 获取 数据库中的 配置列表
* @return array
* ---------------------------------------
*/
public static function lists(){
$config = [];
$data = (new \yii\db\Query())
->select(['type', 'name', 'value'])
->from(self::tableName())
->where(['status'=>1])
->all();
if (!empty($data) && is_array($data)) {
foreach ($data as $key => $value) {
$config[$value['name']] = self::parse($value['type'], $value['value']);
}
}
return $config;
}
/**
* ---------------------------------------
* 根据配置类型解析配置
* @param integer $type 配置类型
* @param string $value 配置值
* @return mixed
* ---------------------------------------
*/
public static function parse($type, $value){
switch ($type) {
case 3: //解析数组
$array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
if(strpos($value,':')){
$value = [];
foreach ($array as $val) {
list($k, $v) = explode(':', $val);
$value[$k] = $v;
}
}else{
$value = $array;
}
break;
}
return $value;
}
}