123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- <?php
- namespace app\common\Service;
- use app\common\model\Config;
- use think\Cache;
- use think\Db;
- /**
- * 平台配置服务类
- */
- class PlatformService
- {
- /**
- * 根据配置名获取配置值
- * @param string $name 配置名
- * @return mixed
- */
- public static function getConfigValue($name)
- {
- $cacheKey = 'config_' . $name;
- $value = Cache::get($cacheKey);
-
- if ($value === false) {
- $config = Config::where('name', $name)->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']);
- }
- }
|