SystemService.php 5.3 KB

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