92 lines
2.3 KiB
PHP
92 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\model\app;
|
|
|
|
use think\Model;
|
|
use think\Cache;
|
|
/**
|
|
* 配置模型
|
|
*/
|
|
class Config extends Model
|
|
{
|
|
|
|
// 表名,不含前缀
|
|
protected $name = 'app_config';
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = false;
|
|
// 定义时间戳字段名
|
|
protected $createTime = false;
|
|
protected $updateTime = false;
|
|
// 追加属性
|
|
protected $append = [
|
|
];
|
|
|
|
|
|
/**
|
|
* 获取配置
|
|
* @param $name
|
|
* @return mixed
|
|
*/
|
|
public static function getConfig($name){
|
|
|
|
$cacheConfig = Cache::get(\app\common\constant\cache\Keys::APP_CONFIG.$name);
|
|
if($cacheConfig){
|
|
return $cacheConfig;
|
|
}
|
|
|
|
$config = self::where(['name'=>$name])->find();
|
|
|
|
if(!$config){
|
|
$configFilePath = APP_PATH . 'common/model/fill/data/app_config.php';
|
|
$config = require($configFilePath);
|
|
|
|
if($config){
|
|
foreach ($config as $item){
|
|
if($item['name'] == $name){
|
|
Cache::set(\app\common\constant\cache\Keys::APP_CONFIG.$name,json_decode($item['value'],true));
|
|
return json_decode($item['value'],true);
|
|
}
|
|
}
|
|
}
|
|
Cache::set(\app\common\constant\cache\Keys::APP_CONFIG.$name,\app\common\model\app\ConfigDefault::$agent);
|
|
//获取默认配置
|
|
return \app\common\model\app\ConfigDefault::$agent;
|
|
}else{
|
|
|
|
Cache::set(\app\common\constant\cache\Keys::APP_CONFIG.$name,json_decode($config['value'],true));
|
|
return json_decode($config['value'],true);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 设置配置
|
|
* @param $name
|
|
* @param $value
|
|
* @return bool
|
|
*/
|
|
public static function setConfig($name,$value){
|
|
$config = self::where([
|
|
'name'=>$name
|
|
])->find();
|
|
|
|
$value = json_encode($value,true);
|
|
|
|
if(!$config){
|
|
self::insert([
|
|
'name'=>$name,
|
|
'uniacid'=>UNIACID,
|
|
'createtime'=>time(),
|
|
'value'=>$value
|
|
]);
|
|
}else{
|
|
self::where([
|
|
'name'=>$name
|
|
])->cache(\app\common\constant\cache\Keys::APP_CONFIG.$name)->update([
|
|
'value'=>$value
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |