| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Module\System\Services;
- use App\Module\File\Img;
- use App\Module\System\Enums\CONFIG_TYPE;
- use App\Module\System\Models\SysConfig;
- use UCore\Helper\Cache;
- use UCore\Helper\CacheTag;
- use UCore\Trace;
- class ConfigService
- {
- const CACHE_TAG = 'config';
- /**
- * 获取最后配置时间
- *
- * @return int
- */
- static public function getConfigTime()
- {
- return \UCore\Helper\Cache::cacheCall('app-config-time', function () {
- return time();
- }, [], 1000);
- }
- static public function setConfigTime()
- {
- \Illuminate\Support\Facades\Cache::put('app-config-time', time());
- }
- /**
- * 默认
- * @param $key
- * @param $default
- * @return int|mixed|string|null
- */
- static public function getValueDefault($key, $default = null)
- {
- $value = self::getValue($key);
- $value = $value ?? $default;
- Trace::applyData('config-read',[$key,$value]);
- return $value;
- }
- /**
- * 获取二维数据
- * @param $key
- * @param $key2
- * @return mixed|string|null
- */
- static public function getValue2($key, $key2)
- {
- $value = self::getValue($key);
- return $value[$key2]??null;
- }
- /**
- *
- * @param $key
- * @param $default
- * @param $ttl
- * @return mixed
- */
- static public function getValueDefaultCache($key, $default = null,$ttl=60)
- {
- $key2 = 'config-key-'.$key;
- $old = \Illuminate\Support\Facades\Cache::get($key2,function()use ($key,$default,$ttl){
- $new = self::getValueDefault($key,$default);
- $key2 = 'config-key-'.$key;
- \Illuminate\Support\Facades\Cache::put($key2,$new,$ttl);
- return $new;
- });
- return $old;
- }
- /**
- * 获取值
- *
- * @param $key
- * @return int|string|null
- */
- static private function getValue($key, $model = null)
- {
- if (!$model) {
- /**
- * @var SysConfig $model
- */
- $model = SysConfig::query()->where(
- [
- 'keyname' => $key
- ]
- )->first();
- }
- if ($model) {
- // dump($model);
- if ($model->type === CONFIG_TYPE::TYPE_INT->valueInt()) {
- return (int)$model->value;
- }
- if ($model->type === CONFIG_TYPE::TYPE_FLOAT->valueInt()) {
- return (float)$model->value;
- }
- if ($model->type === CONFIG_TYPE::TYPE_TIME->valueInt()) {
- return (int)$model->value;
- }
- if ($model->type === CONFIG_TYPE::TYPE_IMG->valueInt()) {
- return Img::getAdminPicUrl($model->value);
- }
- if ($model->type === CONFIG_TYPE::TYPE_BOOL->valueInt()) {
- return boolval($model->value);
- }
- if ($model->type === CONFIG_TYPE::TYPE_IS->valueInt()) {
- return boolval($model->value);
- }
- if ($model->type === CONFIG_TYPE::TYPE_JSON->valueInt()) {
- return json_decode($model->value,true);
- }
- if ($model->type === CONFIG_TYPE::TYPE_EMBEDS->valueInt()) {
- return json_decode($model->value,true);
- }
- return $model->value;
- }
- return null;
- }
- /**
- * 获取分组kv
- *
- * @return mixed
- */
- static public function getGroupKv()
- {
- return Cache::cacheCall([__FUNCTION__, __CLASS__,1], function () {
- $data = SysConfig::query()->distinct()->pluck('group','group')->toArray();
- return $data;
- }, [], 3600, [self::CACHE_TAG]);
- }
- static public function getGroupKv2($group)
- {
- return Cache::cacheCall([__FUNCTION__, __CLASS__,$group,2], function ($group) {
- $data =SysConfig::query() ->where('group',$group)
- ->distinct()
- ->pluck('group2','group2')
- ->toArray();
- // dd($data);
- return $data;
- }, [$group], 3600, [self::CACHE_TAG]);
- }
- /**
- * 获取客户端的可用
- *
- * @return mixed
- */
- static public function getClient()
- {
- return Cache::cacheCall([__FUNCTION__, __CLASS__], function () {
- $data = SysConfig::query()->where('is_client','=','1')->get();
- $res = [];
- foreach ($data as $item){
- $res[$item->keyname] = self::getValue($item->keyname,$item);
- }
- return $res;
- }, [], 1, [self::CACHE_TAG]);
- }
- /**
- * 获取分组的配置想
- * @param $group
- * @return mixed
- */
- static public function getByGroup($group)
- {
- return Cache::cacheCall([__FUNCTION__, __CLASS__], function ($group) {
- $data = SysConfig::query()->where('group','=',$group)->get();
- $res = [];
- foreach ($data as $item){
- $res[$item->keyname] = self::getValue($item->keyname,$item);
- }
- return $res;
- }, [$group], 10, [self::CACHE_TAG]);
- }
- static public function clear_cache()
- {
- CacheTag::tags_clear([self::CACHE_TAG]);
- }
- }
|