ConfigService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Module\System\Services;
  3. use App\Module\File\Img;
  4. use App\Module\System\Enums\CONFIG_TYPE;
  5. use App\Module\System\Models\SysConfig;
  6. use UCore\Helper\Cache;
  7. use UCore\Helper\CacheTag;
  8. use UCore\Trace;
  9. class ConfigService
  10. {
  11. const CACHE_TAG = 'config';
  12. /**
  13. * 获取最后配置时间
  14. *
  15. * @return int
  16. */
  17. static public function getConfigTime()
  18. {
  19. return \UCore\Helper\Cache::cacheCall('app-config-time', function () {
  20. return time();
  21. }, [], 1000);
  22. }
  23. static public function setConfigTime()
  24. {
  25. \Illuminate\Support\Facades\Cache::put('app-config-time', time());
  26. }
  27. /**
  28. * 默认
  29. * @param $key
  30. * @param $default
  31. * @return int|mixed|string|null
  32. */
  33. static public function getValueDefault($key, $default = null)
  34. {
  35. $value = self::getValue($key);
  36. $value = $value ?? $default;
  37. Trace::applyData('config-read',[$key,$value]);
  38. return $value;
  39. }
  40. /**
  41. * 获取二维数据
  42. * @param $key
  43. * @param $key2
  44. * @return mixed|string|null
  45. */
  46. static public function getValue2($key, $key2)
  47. {
  48. $value = self::getValue($key);
  49. return $value[$key2]??null;
  50. }
  51. /**
  52. *
  53. * @param $key
  54. * @param $default
  55. * @param $ttl
  56. * @return mixed
  57. */
  58. static public function getValueDefaultCache($key, $default = null,$ttl=60)
  59. {
  60. $key2 = 'config-key-'.$key;
  61. $old = \Illuminate\Support\Facades\Cache::get($key2,function()use ($key,$default,$ttl){
  62. $new = self::getValueDefault($key,$default);
  63. $key2 = 'config-key-'.$key;
  64. \Illuminate\Support\Facades\Cache::put($key2,$new,$ttl);
  65. return $new;
  66. });
  67. return $old;
  68. }
  69. /**
  70. * 获取值
  71. *
  72. * @param $key
  73. * @return int|string|null
  74. */
  75. static private function getValue($key, $model = null)
  76. {
  77. if (!$model) {
  78. /**
  79. * @var SysConfig $model
  80. */
  81. $model = SysConfig::query()->where(
  82. [
  83. 'keyname' => $key
  84. ]
  85. )->first();
  86. }
  87. if ($model) {
  88. // dump($model);
  89. if ($model->type === CONFIG_TYPE::TYPE_INT->valueInt()) {
  90. return (int)$model->value;
  91. }
  92. if ($model->type === CONFIG_TYPE::TYPE_FLOAT->valueInt()) {
  93. return (float)$model->value;
  94. }
  95. if ($model->type === CONFIG_TYPE::TYPE_TIME->valueInt()) {
  96. return (int)$model->value;
  97. }
  98. if ($model->type === CONFIG_TYPE::TYPE_IMG->valueInt()) {
  99. return Img::getAdminPicUrl($model->value);
  100. }
  101. if ($model->type === CONFIG_TYPE::TYPE_BOOL->valueInt()) {
  102. return boolval($model->value);
  103. }
  104. if ($model->type === CONFIG_TYPE::TYPE_IS->valueInt()) {
  105. return boolval($model->value);
  106. }
  107. if ($model->type === CONFIG_TYPE::TYPE_JSON->valueInt()) {
  108. return json_decode($model->value,true);
  109. }
  110. if ($model->type === CONFIG_TYPE::TYPE_EMBEDS->valueInt()) {
  111. return json_decode($model->value,true);
  112. }
  113. return $model->value;
  114. }
  115. return null;
  116. }
  117. /**
  118. * 获取分组kv
  119. *
  120. * @return mixed
  121. */
  122. static public function getGroupKv()
  123. {
  124. return Cache::cacheCall([__FUNCTION__, __CLASS__,1], function () {
  125. $data = SysConfig::query()->distinct()->pluck('group','group')->toArray();
  126. return $data;
  127. }, [], 3600, [self::CACHE_TAG]);
  128. }
  129. static public function getGroupKv2($group)
  130. {
  131. return Cache::cacheCall([__FUNCTION__, __CLASS__,$group,2], function ($group) {
  132. $data =SysConfig::query() ->where('group',$group)
  133. ->distinct()
  134. ->pluck('group2','group2')
  135. ->toArray();
  136. // dd($data);
  137. return $data;
  138. }, [$group], 3600, [self::CACHE_TAG]);
  139. }
  140. /**
  141. * 获取客户端的可用
  142. *
  143. * @return mixed
  144. */
  145. static public function getClient()
  146. {
  147. return Cache::cacheCall([__FUNCTION__, __CLASS__], function () {
  148. $data = SysConfig::query()->where('is_client','=','1')->get();
  149. $res = [];
  150. foreach ($data as $item){
  151. $res[$item->keyname] = self::getValue($item->keyname,$item);
  152. }
  153. return $res;
  154. }, [], 1, [self::CACHE_TAG]);
  155. }
  156. /**
  157. * 获取分组的配置想
  158. * @param $group
  159. * @return mixed
  160. */
  161. static public function getByGroup($group)
  162. {
  163. return Cache::cacheCall([__FUNCTION__, __CLASS__], function ($group) {
  164. $data = SysConfig::query()->where('group','=',$group)->get();
  165. $res = [];
  166. foreach ($data as $item){
  167. $res[$item->keyname] = self::getValue($item->keyname,$item);
  168. }
  169. return $res;
  170. }, [$group], 10, [self::CACHE_TAG]);
  171. }
  172. static public function clear_cache()
  173. {
  174. CacheTag::tags_clear([self::CACHE_TAG]);
  175. }
  176. }