feat : config

This commit is contained in:
2025-03-20 09:43:16 +08:00
parent 22fef86d76
commit 40f205fcda
4 changed files with 89 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ use App\Constants\ConfigCode;
use App\Constants\RedisCode;
use App\Model\Config;
use App\Model\Cycle;
use App\Service\ServiceTrait\Common\OssTrait;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Tappable\HigherOrderTapProxy;
use Psr\Container\ContainerExceptionInterface;
@@ -14,6 +15,7 @@ use Psr\Container\NotFoundExceptionInterface;
class ConfigCache
{
use OssTrait;
/**
* @var RedisCache
@@ -62,21 +64,29 @@ class ConfigCache
/**
* @param $key
* @param int $isRedis
* @param int $isRedis //默认走缓存 可以关闭
* @param int $expire
* @return false|HigherOrderTapProxy|mixed|\Redis|string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getConfigValueByKey($key, int $isRedis = 0,int $expire = 600): mixed
public function getConfigValueByKey($key, int $isRedis = 1,int $expire = 600): mixed
{
$redisKey = CommonRedisKey::getConfigKey($key);
if (!$this->redis->exists($redisKey,RedisCode::SYSTEM_DB)) {
$value = $this->configModel->where('key',$key)->value('value');
// 做转换
$value = match ($key) {
ConfigCode::APP_LOGO => $this->getOssObjectById($value),
default => $value,
};
if ($isRedis == 1) {
$this->redis->setEx($redisKey, $value, $expire,RedisCode::SYSTEM_DB);
}
return $value ?? ConfigCode::DEFAULT_VALUE[$key];
}

View File

@@ -14,6 +14,12 @@ class ConfigCode
const string NUMBER_OF_USER_ADDRESS_POOLS = 'NumberOfUserAddressPools'; // 用户可以设置的送达地址的数量
const string MAXIMUM_VALUE_IN_FULL_BOX = 'MaximumValueInFullBox'; //运输箱最大可装自选盒数
const string MAXIMUM_WHOLE_CASE_SPLIT = 'MaximumWholeCaseSplit'; //运输箱分割列数最大值
const string ABOUT_US_SYNOPSIS = 'AboutUsSynopsis'; // 关于我们 - 简介
const string BUSINESS_LICENSE = 'BusinessLicense'; // 营业执照
const string USER_AGREEMENT = 'UserAgreement'; // 用户使用协议
const string USER_PRIVACY_AGREEMENT = 'UserPrivacyAgreement'; // 用户隐私协议
const string APP_LOGO = 'AppLogo'; // app 图标
/**
* @var string CouponConfiguration|优惠券配置
@@ -34,6 +40,11 @@ class ConfigCode
self::NUMBER_OF_USER_ADDRESS_POOLS => 10,
self::MAXIMUM_VALUE_IN_FULL_BOX => 32,
self::MAXIMUM_WHOLE_CASE_SPLIT => 4,
self::ABOUT_US_SYNOPSIS => '关于我们-xxxx',
self::BUSINESS_LICENSE => 'https://www.baidu.com',
self::USER_AGREEMENT => 'https://www.baidu.com',
self::USER_PRIVACY_AGREEMENT => 'https://www.baidu.com',
self::APP_LOGO => 'https://www.baidu.com',
//CouponConfiguration
self::COUPONS_FOR_NEWCOMERS => '0',
self::NEWBIE_COUPON_VALIDITY => '0',

View File

@@ -7,6 +7,7 @@ namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Service\Api\System\CityListService;
use App\Service\Api\System\MiniWxConfigService;
use App\Service\Api\System\SiteListService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
@@ -30,4 +31,11 @@ class SystemController extends AbstractController
{
return (new CityListService)->handle();
}
#[RequestMapping(path: 'mini_wx/config',methods: 'GET')]
#[Scene(scene: 'mini_wx_config')]
public function getMiniWxConfig()
{
return (new MiniWxConfigService)->handle();
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\System;
use App\Cache\Redis\Common\ConfigCache;
use App\Constants\ConfigCode;
use App\Service\Api\BaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class MiniWxConfigService extends BaseService
{
/**
* @var ConfigCache
*/
#[Inject]
protected ConfigCache $configCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$res = [
'logo' => $this->configCache->getConfigValueByKey(ConfigCode::APP_LOGO),
'about_us' => [
'synopsis' => $this->configCache->getConfigValueByKey(ConfigCode::ABOUT_US_SYNOPSIS),
'jump_page' => [
[
'title' => '营业执照',
'url' => $this->configCache->getConfigValueByKey(ConfigCode::BUSINESS_LICENSE)
],
[
'title' => '用户使用协议',
'url' => $this->configCache->getConfigValueByKey(ConfigCode::USER_AGREEMENT)
],
[
'title' => '用户隐私协议',
'url' => $this->configCache->getConfigValueByKey(ConfigCode::USER_PRIVACY_AGREEMENT)
],
]
]
];
return $this->return->success('success', $res);
}
}