SystemService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\service\system;
  3. use app\extra\basic\Service;
  4. use app\model\system\SystemConfig;
  5. use app\model\system\SystemData;
  6. use support\think\Cache;
  7. class SystemService extends Service
  8. {
  9. /**
  10. * 配置缓存数据
  11. * @var array
  12. */
  13. private static array $config = [];
  14. public static function get(string $name = '', string $default = '')
  15. {
  16. try {
  17. $cacheConfig = Cache::get("HexagonConfig");
  18. if (empty($cacheConfig))
  19. {
  20. $config = (new SystemConfig)->select();
  21. self::setConfig($config);
  22. Cache::set("HexagonConfig",json_encode($config));
  23. } else {
  24. $config = json_decode($cacheConfig,true);
  25. self::setConfig($config);
  26. }
  27. [$type, $field, $outer] = static::_parse($name);
  28. if (empty($name)) {
  29. return static::$config;
  30. } elseif (isset(static::$config[$type])) {
  31. $group = static::$config[$type];
  32. if ($outer !== 'raw') foreach ($group as $kk => $vo) {
  33. $group[$kk] = htmlspecialchars(strval($vo));
  34. }
  35. return $field ? ($group[$field] ?? $default) : $group;
  36. } else {
  37. return $default;
  38. }
  39. } catch (\Exception $exception)
  40. {
  41. return false;
  42. }
  43. }
  44. protected static function setConfig($data)
  45. {
  46. foreach($data as $item)
  47. {
  48. static::$config[$item['type']][$item['name']] = $item['value'];
  49. }
  50. }
  51. public static function set(string $name, $value = '')
  52. {
  53. static::$config = [];
  54. [$type, $field] = static::_parse($name);
  55. if (is_array($value)) {
  56. $count = 0;
  57. foreach ($value as $kk => $vv) {
  58. $count += static::set("{$field}.{$kk}", $vv);
  59. }
  60. return $count;
  61. } else try {
  62. Cache::delete("HexagonConfig");
  63. $map = ['type' => $type, 'name' => $field];
  64. $data = array_merge($map, ['value' => $value]);
  65. $query = (new SystemConfig)->where($map);
  66. return (clone $query)->count() > 0 ? $query->update($data) : $query->insert($data);
  67. } catch (\Throwable $exception) {
  68. return false;
  69. }
  70. }
  71. /**
  72. * 解析缓存名称
  73. * @param string $rule 配置名称
  74. * @return array
  75. */
  76. private static function _parse(string $rule): array
  77. {
  78. $type = 'base';
  79. if (stripos($rule, '.') !== false) {
  80. [$type, $rule] = explode('.', $rule, 2);
  81. }
  82. [$field, $outer] = explode('|', "{$rule}|");
  83. return [$type, $field, strtolower($outer)];
  84. }
  85. /**
  86. * 读取数据内容
  87. * @param string $name
  88. * @param array $default
  89. * @return array|mixed
  90. */
  91. public static function getData(string $name, array $default = [])
  92. {
  93. try {
  94. $value = (new SystemData)->where("name",$name)->value("content");
  95. if (!empty($value)) return json_decode($value,true);
  96. return $default;
  97. } catch (\Throwable $exception)
  98. {
  99. return $default;
  100. }
  101. }
  102. /**
  103. * 保存数据内容
  104. * @param string $name 数据名称
  105. * @param mixed $value 数据内容
  106. * @return bool
  107. */
  108. public static function setData(string $name, $value)
  109. {
  110. try {
  111. $data = ['name' => $name, 'content' => json_encode($value, 64 | 256)];
  112. return (new SystemData)->setAutoData($data,"name");
  113. } catch (\Throwable $exception) {
  114. echo $exception->getFile()."\n";
  115. echo $exception->getLine()."\n";
  116. echo $exception->getMessage()."\n";
  117. return false;
  118. }
  119. }
  120. /**
  121. * 写入系统日志内容
  122. * @param string $action
  123. * @param string $content
  124. * @param string $userName
  125. * @return boolean
  126. */
  127. public static function setOplog(string $action, string $content, string $userName): bool
  128. {
  129. return (new SystemOplog)->insert(static::getOplog($action, $content,$userName)) !== false;
  130. }
  131. /**
  132. * 获取系统日志内容
  133. * @param string $action
  134. * @param string $content
  135. * @param string $userName
  136. * @return array
  137. */
  138. public static function getUserOplog(string $action, string $content, string $userName): array
  139. {
  140. return [
  141. 'username' => $userName,
  142. 'action' => $action, 'content' => $content,
  143. 'geoip' => request()->getRealIp() ?: '127.0.0.1',
  144. ];
  145. }
  146. /**
  147. * 获取系统日志内容
  148. * @param string $action
  149. * @param string $content
  150. * @param string $userName
  151. * @return array
  152. */
  153. public static function getOplog(string $action, string $content, string $userName): array
  154. {
  155. return [
  156. 'node' => request()->uri(),
  157. 'action' => $action, 'content' => $content,
  158. 'geoip' => request()->getRealIp() ?: '127.0.0.1',
  159. 'username' => $userName ?: '-'
  160. ];
  161. }
  162. }