SystemLogin.php 9.3 KB

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