find(); if ($config) { $value = $config['value']; // 处理数组类型 if ($config['type'] === 'array') { $value = json_decode($value, true) ?: []; } // 处理布尔类型 if ($config['type'] === 'boolean') { $value = (bool)$value; } // 缓存配置值 Cache::set($cacheKey, $value, 3600); } else { $value = null; } } return $value; } /** * 根据分组获取配置 * @param string $group 分组名 * @return array */ public static function getConfigByGroup($group) { $cacheKey = 'config_group_' . $group; $configs = Cache::get($cacheKey); if ($configs === false) { $list = Config::where('group', $group) ->where('type', '<>', 'group') ->select(); $configs = []; foreach ($list as $config) { $name = str_replace($group . '.', '', $config['name']); $value = $config['value']; // 处理数组类型 if ($config['type'] === 'array') { $value = json_decode($value, true) ?: []; } // 处理布尔类型 if ($config['type'] === 'boolean') { $value = (bool)$value; } $configs[$name] = $value; } // 缓存配置 Cache::set($cacheKey, $configs, 3600); } return $configs; } /** * 设置配置值 * @param string $name 配置名 * @param mixed $value 配置值 * @return bool */ public static function setConfigValue($name, $value) { $config = Config::where('name', $name)->find(); if (!$config) { return false; } // 处理数组类型 if ($config['type'] === 'array') { $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value; } // 更新配置 $config->value = $value; $result = $config->save(); if ($result) { // 清除相关缓存 Cache::rm('config_' . $name); Cache::rm('config_group_' . $config['group']); } return $result; } /** * 批量设置配置值 * @param array $configs 配置数组 * @param string $group 分组名 * @return bool */ public static function setConfigsByGroup($configs, $group) { $result = true; foreach ($configs as $name => $value) { $configName = $group . '.' . $name; if (!self::setConfigValue($configName, $value)) { $result = false; } } return $result; } /** * 获取所有平台配置 * @return array */ public static function getAllPlatformConfigs() { $cacheKey = 'all_platform_configs'; $configs = Cache::get($cacheKey); if ($configs === false) { $platforms = Config::where('name', 'like', 'shop.platform.%') ->where('type', 'group') ->where('group', 'shop.platform') ->select(); $configs = []; foreach ($platforms as $platform) { $platformName = str_replace('shop.platform.', '', $platform['name']); $configs[$platformName] = [ 'name' => $platformName, 'title' => $platform['title'], 'tip' => $platform['tip'], 'status' => self::getConfigValue($platform['name'] . '.status'), 'config' => self::getConfigByGroup($platform['name']) ]; } // 缓存配置 Cache::set($cacheKey, $configs, 3600); } return $configs; } /** * 检查配置是否存在 * @param string $name 配置名 * @return bool */ public static function hasConfig($name) { return Config::where('name', $name)->count() > 0; } /** * 创建配置项 * @param array $data 配置数据 * @return bool */ public static function createConfig($data) { // 检查是否已存在 if (self::hasConfig($data['name'])) { return false; } $config = new Config(); $config->data($data); $result = $config->save(); if ($result) { // 清除缓存 Cache::clear(); } return $result; } /** * 删除配置项 * @param string $name 配置名 * @return bool */ public static function deleteConfig($name) { $config = Config::where('name', $name)->find(); if (!$config) { return false; } $result = $config->delete(); if ($result) { // 清除缓存 Cache::clear(); } return $result; } /** * 清除所有配置缓存 */ public static function clearConfigCache() { Cache::clear(); } /** * 获取配置项的显示值 * @param array $config 配置项数组 * @return string */ public static function getDisplayValue($config) { $value = $config['value']; switch ($config['type']) { case 'boolean': return $value ? '是' : '否'; case 'array': $arrayValue = json_decode($value, true); return is_array($arrayValue) ? implode(', ', $arrayValue) : $value; case 'password': return str_repeat('*', strlen($value)); default: return $value; } } /** * 验证配置值 * @param string $type 配置类型 * @param mixed $value 配置值 * @return bool */ public static function validateValue($type, $value) { switch ($type) { case 'int': return is_numeric($value); case 'boolean': return in_array($value, ['0', '1', 0, 1, true, false], true); case 'array': if (is_array($value)) { return true; } $decoded = json_decode($value, true); return json_last_error() === JSON_ERROR_NONE; case 'email': return filter_var($value, FILTER_VALIDATE_EMAIL) !== false; case 'url': return filter_var($value, FILTER_VALIDATE_URL) !== false; default: return true; } } /** * 获取平台状态 * @param string $platformName 平台名称 * @return bool */ public static function getPlatformStatus($platformName) { $statusConfig = Config::where('name', $platformName . '.status')->find(); return $statusConfig ? (bool)$statusConfig['value'] : false; } /** * 获取平台配置项数量 * @param string $platformName 平台名称 * @return int */ public static function getConfigCount($platformName) { return Config::where('group', $platformName)->count(); } /** * 获取平台配置数据 * @param string $platform 平台类型 * @return array */ public static function getPlatformConfigData($platform) { $configs = []; switch ($platform) { case 'WechatMiniProgram': $configs = [ [ 'name' => 'shop.platform.WechatMiniProgram', 'group' => 'shop.platform', 'title' => '微信小程序', 'tip' => '微信小程序平台配置', 'type' => 'group', 'value' => '', 'rule' => '', 'extend' => '' ], [ 'name' => 'shop.platform.WechatMiniProgram.app_id', 'group' => 'shop.platform.WechatMiniProgram', 'title' => '小程序AppId', 'tip' => 'AppID是小程序开发标识码,配合AppSecret可调用小程序的接口能力', 'type' => 'string', 'value' => '', 'rule' => 'required', 'extend' => '' ], [ 'name' => 'shop.platform.WechatMiniProgram.secret', 'group' => 'shop.platform.WechatMiniProgram', 'title' => '小程序密钥', 'tip' => 'AppSecret是校验小程序开发者身份的密钥,具有极高的安全性', 'type' => 'string', 'value' => '', 'rule' => 'required', 'extend' => '' ], [ 'name' => 'shop.platform.WechatMiniProgram.status', 'group' => 'shop.platform.WechatMiniProgram', 'title' => '小程序开启状态', 'tip' => '是否开启微信小程序功能', 'type' => 'boolean', 'value' => '0', 'rule' => '', 'extend' => '' ], [ 'name' => 'shop.platform.WechatMiniProgram.auto_login', 'group' => 'shop.platform.WechatMiniProgram', 'title' => '微信自动登录', 'tip' => '进入应用后,用户将会自动授权登录,未注册用户将会自动注册', 'type' => 'boolean', 'value' => '1', 'rule' => '', 'extend' => '' ], [ 'name' => 'shop.platform.WechatMiniProgram.bind_mobile', 'group' => 'shop.platform.WechatMiniProgram', 'title' => '绑定手机号', 'tip' => '授权登录后,未绑定手机号的用户,将会立即提醒绑定手机号', 'type' => 'boolean', 'value' => '1', 'rule' => '', 'extend' => '' ] ]; break; case 'DouyinMiniProgram': $configs = [ [ 'name' => 'shop.platform.DouyinMiniProgram', 'group' => 'shop.platform', 'title' => '抖音小程序', 'tip' => '抖音小程序平台配置', 'type' => 'group', 'value' => '', 'rule' => '', 'extend' => '' ], [ 'name' => 'shop.platform.DouyinMiniProgram.app_id', 'group' => 'shop.platform.DouyinMiniProgram', 'title' => '抖音小程序AppId', 'tip' => '抖音小程序应用ID', 'type' => 'string', 'value' => '', 'rule' => 'required', 'extend' => '' ], [ 'name' => 'shop.platform.DouyinMiniProgram.secret', 'group' => 'shop.platform.DouyinMiniProgram', 'title' => '抖音小程序密钥', 'tip' => '抖音小程序应用密钥', 'type' => 'string', 'value' => '', 'rule' => 'required', 'extend' => '' ], [ 'name' => 'shop.platform.DouyinMiniProgram.status', 'group' => 'shop.platform.DouyinMiniProgram', 'title' => '抖音小程序开启状态', 'tip' => '是否开启抖音小程序功能', 'type' => 'boolean', 'value' => '0', 'rule' => '', 'extend' => '' ], [ 'name' => 'shop.platform.DouyinMiniProgram.auto_login', 'group' => 'shop.platform.DouyinMiniProgram', 'title' => '自动登录', 'tip' => '进入应用后,用户将会自动授权登录,未注册用户将会自动注册', 'type' => 'boolean', 'value' => '1', 'rule' => '', 'extend' => '' ], [ 'name' => 'shop.platform.DouyinMiniProgram.bind_mobile', 'group' => 'shop.platform.DouyinMiniProgram', 'title' => '绑定手机号', 'tip' => '授权登录后,未绑定手机号的用户,将会立即提醒绑定手机号', 'type' => 'boolean', 'value' => '1', 'rule' => '', 'extend' => '' ] ]; break; } return $configs; } /** * 初始化平台配置数据 * @param string $platform 平台类型 * @return bool * @throws \Exception */ public static function initPlatformConfig($platform) { $configs = self::getPlatformConfigData($platform); Db::startTrans(); try { foreach ($configs as $config) { $exists = Config::where('name', $config['name'])->find(); if (!$exists) { Config::create($config); } } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw $e; } } /** * 根据渠道获取平台配置 * @param string $channel 渠道标识 * @return array */ public static function getConfigByChannel($channel) { $platformMap = [ 'wechat_mini_program' => 'shop.platform.WechatMiniProgram', 'douyin_mini_program' => 'shop.platform.DouyinMiniProgram', ]; if (!isset($platformMap[$channel])) { return []; } return self::getConfigByGroup($platformMap[$channel]); } /** * 检查平台是否启用 * @param string $channel 渠道标识 * @return bool */ public static function isPlatformEnabled($channel) { $config = self::getConfigByChannel($channel); return !empty($config['status']); } }