find(); if(!$row) return null; if($row['type'] !== 'group') { $config = $row->value; }else { $config = []; $list = ShopConfigModel::where('parent_code', $code)->select(); foreach ($list as &$row) { if ($row['type'] === 'group') { $row->value = self::getConfigs($row->code, false); } else { cache("config:{$row->code}", $row->value); } $config[self::getShortCode($row->toArray())] = $row->value; } } // 设置配置缓存 cache("config:{$code}", $config); } return $config; } /** * 获取单一配置项 * * @param string $code * @param boolean $cache * @return mixed */ public static function getConfigField($code, $cache = true) { // 从缓存中获取 if ($cache) { $config = cache("config:{$code}"); if (empty($config)) { $config = self::getConfigField($code, false); } } // 从数据库中查找 if (!$cache) { $config = ShopConfigModel::where('code', $code)->value('value'); // 设置配置缓存 cache("config:{$code}", $config); } return $config; } private static function getShortCode($config) { if (!empty($config['parent_code'])) { return str_replace("{$config['parent_code']}.", "", $config['code']); } return $config['code']; } /** * 更新配置 * * @param string $code * @param array $configParams * @return void */ public static function setConfigs(string $code, array $configParams) { foreach ($configParams as $configKey => $configValue) { self::setConfigField($code . '.' . $configKey, $configValue); } self::getConfigs(explode('.', $code)[0], false); return self::getConfigs($code); } /** * 更新配置项 */ private static function setConfigField($code, $value) { $config = ShopConfigModel::where('code', $code)->find(); if ($config) { if ($config['type'] === 'group') { foreach ($value as $k => $v) { self::setConfigField($code . '.' . $k, $v); } } else { $config->value = $value; $config->save(); } } } }