BaseDict.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\dict;
  12. use app\service\core\addon\CoreAddonBaseService;
  13. use app\service\core\site\CoreSiteService;
  14. use core\loader\Storage;
  15. use think\facade\Cache;
  16. use think\facade\Db;
  17. /**
  18. * Class BaseDict
  19. * @package core\dict
  20. */
  21. abstract class BaseDict extends Storage
  22. {
  23. //插件整体缓存标识
  24. public static $cache_tag_name = 'addon_cash';
  25. /**
  26. * 初始化
  27. * @param array $config
  28. * @return void
  29. */
  30. protected function initialize(array $config = [])
  31. {
  32. }
  33. /**
  34. * 获取本地插件目录(已安装)
  35. */
  36. protected function getLocalAddons()
  37. {
  38. if (!file_exists(root_path() . "install.lock")) {
  39. //尚未安装不加载插件
  40. return [];
  41. }
  42. $headers = request()->header();
  43. $admin_site_id_name = system_name('admin_site_id_name');
  44. $api_site_id_name = system_name('admin_site_id_name');
  45. $site_id = $headers[$admin_site_id_name] ?? $headers[$api_site_id_name] ?? 0;
  46. if ((int)$site_id) {
  47. $addons = Cache::get("local_install_addons_{$site_id}");
  48. if (!is_null($addons)) return $addons;
  49. $prefix = config('database.connections.mysql.prefix');
  50. $site = Db::name('site')->alias('s')->join(["{$prefix}site_group" => 'sg'], 's.group_id = sg.group_id')
  51. ->where([['s.site_id', '=', $site_id]])
  52. ->field('s.app,s.addons,sg.app as site_group_app,sg.addon as site_group_addon')->find();
  53. $addons = array_unique(array_merge(
  54. (empty($site['app']) ? [] : json_decode($site['app'], true)),
  55. (empty($site['addons']) ? [] : json_decode($site['addons'], true)),
  56. (empty($site['site_group_app']) ? [] : json_decode($site['site_group_app'], true)),
  57. (empty($site['site_group_addon']) ? [] : json_decode($site['site_group_addon'], true))
  58. ));
  59. Cache::tag(CoreSiteService::$cache_tag_name . $site_id)->set("local_install_addons_{$site_id}", $addons);
  60. } else {
  61. $addons = Cache::get("local_install_addons");
  62. if (!is_null($addons)) return $addons;
  63. $addons = Db::name("addon")->column("key");
  64. Cache::tag(CoreAddonBaseService::$cache_tag_name)->set("local_install_addons", $addons);
  65. }
  66. return $addons;
  67. }
  68. /**
  69. * 获取所有本地插件(包括未安装,用于系统指令执行)
  70. * @return array|false
  71. */
  72. public function getAllLocalAddons()
  73. {
  74. $addon_dir = root_path() . 'addon';
  75. $addons = array_diff(scandir($addon_dir), ['.', '..']);
  76. return $addons;
  77. }
  78. /**
  79. * 获取插件目录
  80. * @param string $addon
  81. * @return string
  82. */
  83. protected function getAddonPath(string $addon)
  84. {
  85. return root_path() . 'addon' . DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR;
  86. }
  87. /**
  88. * 获取系统整体app目录
  89. * @return string
  90. */
  91. protected function getAppPath()
  92. {
  93. return root_path() . "app" . DIRECTORY_SEPARATOR;
  94. }
  95. /**
  96. * 获取插件对应app目录
  97. * @param string $addon
  98. * @return string
  99. */
  100. protected function getAddonAppPath(string $addon)
  101. {
  102. return $this->getAddonPath($addon) . "app" . DIRECTORY_SEPARATOR;
  103. }
  104. /**
  105. *获取系统dict path
  106. */
  107. protected function getDictPath()
  108. {
  109. return root_path() . 'app' . DIRECTORY_SEPARATOR . 'dict' . DIRECTORY_SEPARATOR;
  110. }
  111. /**
  112. *获取插件对应的dict目录
  113. * @param string $addon
  114. * @return string
  115. */
  116. protected function getAddonDictPath(string $addon)
  117. {
  118. return $this->getAddonPath($addon) . 'app' . DIRECTORY_SEPARATOR . 'dict' . DIRECTORY_SEPARATOR;
  119. }
  120. /**
  121. *获取插件对应的config目录
  122. * @param string $addon
  123. * @return string
  124. */
  125. protected function getAddonConfigPath(string $addon)
  126. {
  127. return $this->getAddonPath($addon) . 'config' . DIRECTORY_SEPARATOR;
  128. }
  129. /**
  130. * 加载文件数据
  131. * @param $files
  132. * @return array
  133. */
  134. protected function loadFiles($files)
  135. {
  136. $default_sort = 100000;
  137. $files_data = [];
  138. if (!empty($files)) {
  139. foreach ($files as $file) {
  140. $config = include $file;
  141. if (!empty($config)) {
  142. if (isset($config['file_sort'])) {
  143. $sort = $config['file_sort'];
  144. unset($config['file_sort']);
  145. $sort = $sort * 10;
  146. while (array_key_exists($sort, $files_data)) {
  147. $sort++;
  148. }
  149. $files_data[$sort] = $config;
  150. } else {
  151. $files_data[$default_sort] = $config;
  152. $default_sort++;
  153. }
  154. }
  155. }
  156. }
  157. ksort($files_data);
  158. return $files_data;
  159. }
  160. /**
  161. * 加载
  162. * @return mixed
  163. */
  164. abstract public function load(array $data);
  165. }