SystemLogin.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\admin\library;
  3. use app\admin\model\AuthGroup;
  4. use fast\Tree;
  5. use think\Config;
  6. use think\Session;
  7. /**
  8. * 双系统登录隔离与后台风格(统一走 index/login,按角色组识别系统)
  9. */
  10. class SystemLogin
  11. {
  12. /**
  13. * 加载 systems.php / mproc.php
  14. */
  15. public static function loadConfig(): void
  16. {
  17. static $loaded = false;
  18. if ($loaded) {
  19. return;
  20. }
  21. $loaded = true;
  22. if (is_file(APP_PATH . 'extra/systems.php')) {
  23. Config::load(APP_PATH . 'extra/systems.php', 'systems');
  24. }
  25. if (is_file(APP_PATH . 'extra/mproc.php')) {
  26. Config::load(APP_PATH . 'extra/mproc.php', 'mproc');
  27. }
  28. }
  29. public static function normalizeKey(string $key): string
  30. {
  31. $key = strtolower(trim($key));
  32. return $key === 'cockpit' ? 'cockpit' : 'collab';
  33. }
  34. /**
  35. * @param mixed $default
  36. * @return mixed
  37. */
  38. public static function option(string $systemKey, string $key, $default = null)
  39. {
  40. self::loadConfig();
  41. $systemKey = self::normalizeKey($systemKey);
  42. // 分组根:优先读 mproc(改 mproc 立即生效,不被 systems.php 盖住)
  43. if ($key === 'allowed_group_roots') {
  44. if ($systemKey === 'collab') {
  45. $root = (int)Config::get('mproc.collab_login_group_root_id');
  46. if ($root <= 0) {
  47. $root = (int)Config::get('mproc.bid_open_auth_group_root_id');
  48. }
  49. if ($root > 0) {
  50. return [$root];
  51. }
  52. }
  53. if ($systemKey === 'cockpit') {
  54. $roots = Config::get('mproc.cockpit_login_group_root_ids');
  55. if (is_array($roots) && $roots !== []) {
  56. return $roots;
  57. }
  58. }
  59. }
  60. $val = Config::get('systems.' . $systemKey . '.' . $key);
  61. if ($val !== null && $val !== '') {
  62. return $val;
  63. }
  64. if ($systemKey === 'collab') {
  65. if ($key === 'name' || $key === 'brand') {
  66. if ($key === 'brand') {
  67. $brand = Config::get('mproc.collab_system_brand');
  68. if (!$brand) {
  69. $brand = Config::get('mproc.system_brand.collab');
  70. }
  71. return $brand ?: '生产协同系统';
  72. }
  73. return '生产协同系统';
  74. }
  75. if ($key === 'allow_rule_super') {
  76. $v = Config::get('mproc.collab_allow_rule_super');
  77. if ($v === null) {
  78. return true;
  79. }
  80. return (bool)$v;
  81. }
  82. }
  83. if ($systemKey === 'cockpit') {
  84. if ($key === 'name') {
  85. return '生产经营驾驶舱系统';
  86. }
  87. if ($key === 'brand') {
  88. $brand = Config::get('mproc.cockpit_system_brand');
  89. if (!$brand) {
  90. $brand = Config::get('mproc.system_brand.cockpit');
  91. }
  92. return $brand ?: '浙江印刷集团有限公司';
  93. }
  94. if ($key === 'allow_rule_super') {
  95. $v = Config::get('mproc.cockpit_allow_rule_super');
  96. if ($v === null) {
  97. return true;
  98. }
  99. return (bool)$v;
  100. }
  101. }
  102. return $default;
  103. }
  104. /**
  105. * @return array<int, true>
  106. */
  107. public static function allowedGroupIds(string $systemKey): array
  108. {
  109. self::loadConfig();
  110. $systemKey = self::normalizeKey($systemKey);
  111. $roots = self::option($systemKey, 'allowed_group_roots', []);
  112. if (!is_array($roots) || $roots === []) {
  113. return [];
  114. }
  115. $rootIds = [];
  116. foreach ($roots as $rid) {
  117. $rid = (int)$rid;
  118. if ($rid > 0) {
  119. $rootIds[$rid] = true;
  120. }
  121. }
  122. if ($rootIds === []) {
  123. return [];
  124. }
  125. static $cache = [];
  126. $cacheKey = $systemKey . ':' . implode(',', array_keys($rootIds));
  127. if (isset($cache[$cacheKey])) {
  128. return $cache[$cacheKey];
  129. }
  130. $groupList = AuthGroup::where('status', 'normal')->field('id,pid')->select();
  131. $rows = [];
  132. if (is_array($groupList) || $groupList instanceof \Traversable) {
  133. foreach ($groupList as $g) {
  134. if (is_object($g) && method_exists($g, 'toArray')) {
  135. $g = $g->toArray();
  136. }
  137. if (!is_array($g)) {
  138. continue;
  139. }
  140. $rows[] = [
  141. 'id' => (int)($g['id'] ?? 0),
  142. 'pid' => (int)($g['pid'] ?? 0),
  143. ];
  144. }
  145. }
  146. $allowed = [];
  147. foreach (array_keys($rootIds) as $rootId) {
  148. $allowed[$rootId] = true;
  149. $children = Tree::instance()->init($rows, 'pid')->getChildrenIds($rootId, false);
  150. if (!is_array($children)) {
  151. continue;
  152. }
  153. foreach ($children as $cid) {
  154. $cid = (int)$cid;
  155. if ($cid > 0) {
  156. $allowed[$cid] = true;
  157. }
  158. }
  159. }
  160. $cache[$cacheKey] = $allowed;
  161. return $allowed;
  162. }
  163. public static function belongsTo(Auth $auth, int $adminId, string $systemKey): bool
  164. {
  165. if ($adminId <= 0) {
  166. return false;
  167. }
  168. $systemKey = self::normalizeKey($systemKey);
  169. if (self::option($systemKey, 'allow_rule_super', false)) {
  170. $ruleIds = $auth->getRuleIds($adminId);
  171. if (is_array($ruleIds) && in_array('*', $ruleIds, true)) {
  172. return true;
  173. }
  174. }
  175. $allowed = self::allowedGroupIds($systemKey);
  176. if ($allowed === []) {
  177. return false;
  178. }
  179. $userGroupIds = $auth->getGroupIds($adminId);
  180. if ($userGroupIds === []) {
  181. return false;
  182. }
  183. foreach ($userGroupIds as $gid) {
  184. if (isset($allowed[(int)$gid])) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * 按角色组识别所属系统:先生产协同,再驾驶舱
  192. */
  193. public static function resolveSystem(Auth $auth, int $adminId): ?string
  194. {
  195. if ($adminId <= 0) {
  196. return null;
  197. }
  198. if (self::belongsTo($auth, $adminId, 'collab')) {
  199. return 'collab';
  200. }
  201. if (self::belongsTo($auth, $adminId, 'cockpit')) {
  202. return 'cockpit';
  203. }
  204. return null;
  205. }
  206. /**
  207. * 会话中的系统;缺失时按角色组补全
  208. */
  209. public static function currentSystem(Auth $auth = null): string
  210. {
  211. $system = Session::get('login_system');
  212. if ($system === 'collab' || $system === 'cockpit') {
  213. return $system;
  214. }
  215. if ($auth && $auth->isLogin()) {
  216. $resolved = self::resolveSystem($auth, (int)$auth->id);
  217. if ($resolved) {
  218. Session::set('login_system', $resolved);
  219. return $resolved;
  220. }
  221. }
  222. return 'collab';
  223. }
  224. /**
  225. * 读取风格码:1=白色 2=黑色原版
  226. */
  227. public static function styleCode(string $systemKey): int
  228. {
  229. self::loadConfig();
  230. $systemKey = self::normalizeKey($systemKey);
  231. // 优先:collab_system_style / cockpit_system_style(只改 1 或 2)
  232. $flatKey = $systemKey === 'cockpit' ? 'mproc.cockpit_system_style' : 'mproc.collab_system_style';
  233. $flat = Config::get($flatKey);
  234. if ($flat === 1 || $flat === 2 || $flat === '1' || $flat === '2') {
  235. return (int)$flat === 2 ? 2 : 1;
  236. }
  237. // 兼容旧数组 / 单数字写法
  238. $map = Config::get('mproc.system_style');
  239. if (is_array($map) && isset($map[$systemKey])) {
  240. $code = (int)$map[$systemKey];
  241. } elseif (!is_array($map) && ((int)$map === 1 || (int)$map === 2)) {
  242. $code = (int)$map;
  243. } else {
  244. $code = $systemKey === 'cockpit' ? 2 : 1;
  245. }
  246. return $code === 2 ? 2 : 1;
  247. }
  248. /**
  249. * 应用后台皮肤到 Config,并返回模板变量
  250. *
  251. * @return array{login_system:string,system_style:int,adminskin:string,load_xinhua_theme:bool,system_brand:string}
  252. */
  253. public static function applyTheme(Auth $auth = null): array
  254. {
  255. $system = self::currentSystem($auth);
  256. $style = self::styleCode($system);
  257. // 1 白色:skin-blue-light + xinhua-theme(生产协同)
  258. // 2 黑色原版:skin-black(驾驶舱;本站 skin-blue 已改成亮蓝,不能当原版用)
  259. if ($style === 2) {
  260. $skin = 'skin-black';
  261. $loadXinhua = false;
  262. } else {
  263. $skin = 'skin-blue-light';
  264. $loadXinhua = true;
  265. }
  266. Config::set('fastadmin.adminskin', $skin);
  267. $brand = (string)self::option($system, 'brand', '');
  268. if ($brand === '') {
  269. $brand = (string)self::option($system, 'name', '生产协同系统');
  270. }
  271. $flatBrand = Config::get($system === 'cockpit' ? 'mproc.cockpit_system_brand' : 'mproc.collab_system_brand');
  272. if (is_string($flatBrand) && $flatBrand !== '') {
  273. $brand = $flatBrand;
  274. } else {
  275. $fromMproc = Config::get('mproc.system_brand.' . $system);
  276. if (is_string($fromMproc) && $fromMproc !== '') {
  277. $brand = $fromMproc;
  278. }
  279. }
  280. return [
  281. 'login_system' => $system,
  282. 'system_style' => $style,
  283. 'adminskin' => $skin,
  284. 'load_xinhua_theme' => $loadXinhua,
  285. 'system_brand' => $brand,
  286. ];
  287. }
  288. }