ConfigService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. declare(strict_types=1);
  20. namespace app\common\service;
  21. use app\common\model\Config;
  22. use think\facade\Cache;
  23. class ConfigService
  24. {
  25. /**
  26. * @notes
  27. * @param string $type
  28. * @param string $name
  29. * @return string
  30. * @author 令狐冲
  31. * @date 2022/10/19 16:13
  32. */
  33. public static function getCacheKey($type, $name): string
  34. {
  35. return 'config' . '-' . $type . '-' . $name;
  36. }
  37. /**
  38. * @notes 设置配置值
  39. * @param string $type
  40. * @param string $name
  41. * @param mixed $value
  42. * @return mixed
  43. * @author Tab
  44. * @date 2021/7/15 14:54
  45. */
  46. public static function set($type, $name, $value)
  47. {
  48. //刷新缓存
  49. $cacheKey = self::getCacheKey($type, $name);
  50. Cache::delete($cacheKey);
  51. $original = $value;
  52. // 数组数据进行json编码
  53. if (is_array($value)) {
  54. // JSON_UNESCAPED_UNICODE 不对中文进行unicode编码
  55. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  56. }
  57. $data = Config::where(['type' => $type, 'name' => $name])->findOrEmpty();
  58. if ($data->isEmpty()) {
  59. // 没有则添加
  60. Config::create([
  61. 'type' => $type,
  62. 'name' => $name,
  63. 'value' => $value,
  64. ]);
  65. } else {
  66. // 有则修改
  67. $data->value = $value;
  68. $data->save();
  69. }
  70. // 返回原始值
  71. return $original;
  72. }
  73. /**
  74. * @notes
  75. * @param $type
  76. * @param string $name
  77. * @param null $defaultValue
  78. * @return mixed
  79. * @author 令狐冲
  80. * @date 2022/10/19 16:16
  81. */
  82. public static function get($type, $name = '', $defaultValue = null)
  83. {
  84. //获取缓存
  85. $CacheKey = self::getCacheKey($type, $name);
  86. $result = Cache::get($CacheKey);
  87. $value = $result['config_server'] ?? null;
  88. if ($value !== null) {
  89. return $value;
  90. }
  91. //单项配置
  92. if ($name) {
  93. $result = Config::where(['type' => $type, 'name' => $name])->value('value');
  94. //json类型解析
  95. if (!is_null($result)) {
  96. $json = json_decode($result, true);
  97. $result = json_last_error() === JSON_ERROR_NONE ? $json : $result;
  98. }
  99. //获取默认配置
  100. if ($result === null) {
  101. $result = $defaultValue;
  102. }
  103. //获取本地配置文件配置
  104. if ($result === null) {
  105. $result = config('project.' . $type . '.' . $name);
  106. }
  107. Cache::set($CacheKey, ['config_server' => $result]);
  108. return $result;
  109. }
  110. //多项配置
  111. $result = Config::where(['type' => $type])->column('value', 'name');
  112. if (is_array($result)) {
  113. foreach ($result as $k => $v) {
  114. $json = json_decode($v, true);
  115. if (json_last_error() === JSON_ERROR_NONE) {
  116. $result[$k] = $json;
  117. }
  118. }
  119. }
  120. //读取默认配置
  121. if ($result === null) {
  122. $result = $defaultValue;
  123. }
  124. Cache::set($CacheKey, ['config_server' => $result]);
  125. return $result;
  126. }
  127. }