Files

227 lines
6.0 KiB
PHP

<?php
namespace app\common\model\fill;
use think\Model;
use think\Db;
use think\Cache;
/**
* 填充默认数据
*/
class Handle Extends Model
{
protected static $configList = ['page_decorate','page_navigation','config_system','app_config'];
/**
* 检查是否存在默认配置
* @param string $type 配置类型
* @param string|null $name 配置名称
* @return bool
*/
public static function check($type, $name = null): bool
{
if (!in_array($type, self::$configList)) {
return false;
}
// 缓存 key
$cacheKey = \app\common\constant\cache\Keys::SYSTEM_CONFIG_CHECK($type, $name);
// 命中缓存则直接返回
$cacheValue = Cache::get($cacheKey);
if ($cacheValue) {
return $cacheValue;
}
$where = [
'uniacid' => UNIACID
];
if ($name) {
$where['name'] = $name;
} else {
// 系统配置特殊处理:循环校验文件里的配置
if ($type === 'config_system') {
$configFilePath = APP_PATH . 'common/model/fill/data/config_system.php';
$config = require($configFilePath);
foreach ($config as $item) {
if (!empty($item['name'])) {
self::check('config_system', $item['name']);
}
}
Cache::set($cacheKey, true, 3600);
return true;
}
}
$where = array_merge($where, self::getCondition($type));
$data = Db::name($type)->where($where)->find();
if (!$data) {
self::set($type, $name);
Cache::set($cacheKey, true, 3600);
return true;
}
Cache::set($cacheKey, true, 3600);
return true;
}
/**
* 根据分组检查并初始化配置
* @param string $group 分组名称
* @return bool
*/
public static function checkByGroup($group): bool
{
$configFilePath = APP_PATH . 'common/model/fill/data/config_system.php';
$config = require($configFilePath);
$toInsert = [];
foreach ($config as $item) {
if (isset($item['group']) && $item['group'] == $group) {
$exists = Db::name('config_system')
->where(['uniacid' => UNIACID, 'name' => $item['name']])
->find();
if (!$exists) {
$item['uniacid'] = UNIACID;
$toInsert[] = $item;
}
}
}
if (!empty($toInsert)) {
self::name('config_system')->insertAll($toInsert);
}
return true;
}
/**
* 获取系统配置的默认配置
* @param $configName 配置名称
* @param $fieldValue 某个配置项的名称
* @return void
*/
public static function getSystemDefaultConfig($configName,$fieldName=''){
$configFilePath = APP_PATH . 'common/model/fill/data/config_system.php';
$config = require($configFilePath);
$systemConfig = [];
foreach ($config as $item){
if($item['name'] == $configName){
$item['value'] = json_decode($item['value'],true);
$systemConfig = $item;
break;
}
}
if(!$systemConfig){
return [];
}
if(!$fieldName){
return $systemConfig['value'];
}
foreach ($systemConfig['value'] as $item){
if($item['name'] == $fieldName){
return $item;
}
}
return [];
}
/**
* 设置配置
* @param $type 配置类型
* @param $name 配置名称
* @return void
*/
public static function set($type,$name=null){
$configFilePath = APP_PATH . 'common/model/fill/data/'.$type.'.php';
$config = require($configFilePath);
if($name){
$temp = null;
foreach ($config as &$item){
if($item['name'] == $name){
$item['uniacid'] = UNIACID;
$temp = $item;
break;
}
}
if(!$temp){
return false;
}
$data = [$temp];
}else{
foreach ($config as &$item){
$item['uniacid'] = UNIACID;
}
$data = $config;
}
self::name($type)->insertAll($data);
}
/**
* 导出默认配置
* @return void
*/
public static function export($key = ''){
if(!$key){
foreach (self::$configList as $name){
$where = [];
$where = array_merge($where,self::getCondition($name));
$data = Db::name($name)->where($where)->field(['id','uniacid'],true)->select();
if($data){
file_put_contents(
APP_PATH . 'common/model/fill/data/'.$name.'.php',
'<?php' . "\n\nreturn " . var_export($data, true) . ";"
);
}
}
}else{
$name = $key;
$where = [];
$where = array_merge($where,self::getCondition($name));
$data = Db::name($name)->where($where)->field(['id','uniacid'],true)->select();
if($data){
file_put_contents(
APP_PATH . 'common/model/fill/data/'.$name.'.php',
'<?php' . "\n\nreturn " . var_export($data, true) . ";"
);
}
}
}
/**
* 获取判断条件
* @param $name
* @return array|int[]
*/
public static function getCondition($name){
$where = [];
switch ($name){
case "page_decorate":
$where = ['default_page'=>1];
break;
case "page_navigation":
$where = ['default_navigation'=>1];
break;
}
return $where;
}
}