| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace app\admin\library;
- use app\admin\model\AuthGroup;
- use fast\Tree;
- use think\Config;
- use think\Session;
- /**
- * 双系统登录隔离与后台风格(统一走 index/login,按角色组识别系统)
- */
- class SystemLogin
- {
- /**
- * 加载 systems.php / mproc.php
- */
- public static function loadConfig(): void
- {
- static $loaded = false;
- if ($loaded) {
- return;
- }
- $loaded = true;
- if (is_file(APP_PATH . 'extra/systems.php')) {
- Config::load(APP_PATH . 'extra/systems.php', 'systems');
- }
- if (is_file(APP_PATH . 'extra/mproc.php')) {
- Config::load(APP_PATH . 'extra/mproc.php', 'mproc');
- }
- }
- public static function normalizeKey(string $key): string
- {
- $key = strtolower(trim($key));
- return $key === 'cockpit' ? 'cockpit' : 'collab';
- }
- /**
- * @param mixed $default
- * @return mixed
- */
- public static function option(string $systemKey, string $key, $default = null)
- {
- self::loadConfig();
- $systemKey = self::normalizeKey($systemKey);
- $val = Config::get('systems.' . $systemKey . '.' . $key);
- if ($val !== null && $val !== '') {
- return $val;
- }
- if ($systemKey === 'collab') {
- if ($key === 'allowed_group_roots') {
- $root = (int)Config::get('mproc.collab_login_group_root_id');
- if ($root <= 0) {
- $root = (int)Config::get('mproc.bid_open_auth_group_root_id');
- }
- return $root > 0 ? [$root] : $default;
- }
- if ($key === 'name' || $key === 'brand') {
- if ($key === 'brand') {
- $brand = Config::get('mproc.collab_system_brand');
- if (!$brand) {
- $brand = Config::get('mproc.system_brand.collab');
- }
- return $brand ?: '生产协同系统';
- }
- return '生产协同系统';
- }
- if ($key === 'allow_rule_super') {
- return false;
- }
- }
- if ($systemKey === 'cockpit') {
- if ($key === 'allowed_group_roots') {
- $roots = Config::get('mproc.cockpit_login_group_root_ids');
- return is_array($roots) ? $roots : $default;
- }
- if ($key === 'name') {
- return '生产经营驾驶舱系统';
- }
- if ($key === 'brand') {
- $brand = Config::get('mproc.cockpit_system_brand');
- if (!$brand) {
- $brand = Config::get('mproc.system_brand.cockpit');
- }
- return $brand ?: '浙江印刷集团有限公司';
- }
- if ($key === 'allow_rule_super') {
- return (bool)Config::get('mproc.cockpit_allow_rule_super');
- }
- }
- return $default;
- }
- /**
- * @return array<int, true>
- */
- public static function allowedGroupIds(string $systemKey): array
- {
- self::loadConfig();
- $systemKey = self::normalizeKey($systemKey);
- $roots = self::option($systemKey, 'allowed_group_roots', []);
- if (!is_array($roots) || $roots === []) {
- return [];
- }
- $rootIds = [];
- foreach ($roots as $rid) {
- $rid = (int)$rid;
- if ($rid > 0) {
- $rootIds[$rid] = true;
- }
- }
- if ($rootIds === []) {
- return [];
- }
- static $cache = [];
- $cacheKey = $systemKey . ':' . implode(',', array_keys($rootIds));
- if (isset($cache[$cacheKey])) {
- return $cache[$cacheKey];
- }
- $groupList = AuthGroup::where('status', 'normal')->field('id,pid')->select();
- $rows = [];
- if (is_array($groupList) || $groupList instanceof \Traversable) {
- foreach ($groupList as $g) {
- if (is_object($g) && method_exists($g, 'toArray')) {
- $g = $g->toArray();
- }
- if (!is_array($g)) {
- continue;
- }
- $rows[] = [
- 'id' => (int)($g['id'] ?? 0),
- 'pid' => (int)($g['pid'] ?? 0),
- ];
- }
- }
- $allowed = [];
- foreach (array_keys($rootIds) as $rootId) {
- $allowed[$rootId] = true;
- $children = Tree::instance()->init($rows, 'pid')->getChildrenIds($rootId, false);
- if (!is_array($children)) {
- continue;
- }
- foreach ($children as $cid) {
- $cid = (int)$cid;
- if ($cid > 0) {
- $allowed[$cid] = true;
- }
- }
- }
- $cache[$cacheKey] = $allowed;
- return $allowed;
- }
- public static function belongsTo(Auth $auth, int $adminId, string $systemKey): bool
- {
- if ($adminId <= 0) {
- return false;
- }
- $systemKey = self::normalizeKey($systemKey);
- if (self::option($systemKey, 'allow_rule_super', false)) {
- $ruleIds = $auth->getRuleIds($adminId);
- if (is_array($ruleIds) && in_array('*', $ruleIds, true)) {
- return true;
- }
- }
- $allowed = self::allowedGroupIds($systemKey);
- if ($allowed === []) {
- return false;
- }
- $userGroupIds = $auth->getGroupIds($adminId);
- if ($userGroupIds === []) {
- return false;
- }
- foreach ($userGroupIds as $gid) {
- if (isset($allowed[(int)$gid])) {
- return true;
- }
- }
- return false;
- }
- /**
- * 按角色组识别所属系统:先生产协同,再驾驶舱
- */
- public static function resolveSystem(Auth $auth, int $adminId): ?string
- {
- if ($adminId <= 0) {
- return null;
- }
- if (self::belongsTo($auth, $adminId, 'collab')) {
- return 'collab';
- }
- if (self::belongsTo($auth, $adminId, 'cockpit')) {
- return 'cockpit';
- }
- return null;
- }
- /**
- * 会话中的系统;缺失时按角色组补全
- */
- public static function currentSystem(Auth $auth = null): string
- {
- $system = Session::get('login_system');
- if ($system === 'collab' || $system === 'cockpit') {
- return $system;
- }
- if ($auth && $auth->isLogin()) {
- $resolved = self::resolveSystem($auth, (int)$auth->id);
- if ($resolved) {
- Session::set('login_system', $resolved);
- return $resolved;
- }
- }
- return 'collab';
- }
- /**
- * 读取风格码:1=白色 2=黑色原版
- */
- public static function styleCode(string $systemKey): int
- {
- self::loadConfig();
- $systemKey = self::normalizeKey($systemKey);
- // 优先:collab_system_style / cockpit_system_style(只改 1 或 2)
- $flatKey = $systemKey === 'cockpit' ? 'mproc.cockpit_system_style' : 'mproc.collab_system_style';
- $flat = Config::get($flatKey);
- if ($flat === 1 || $flat === 2 || $flat === '1' || $flat === '2') {
- return (int)$flat === 2 ? 2 : 1;
- }
- // 兼容旧数组 / 单数字写法
- $map = Config::get('mproc.system_style');
- if (is_array($map) && isset($map[$systemKey])) {
- $code = (int)$map[$systemKey];
- } elseif (!is_array($map) && ((int)$map === 1 || (int)$map === 2)) {
- $code = (int)$map;
- } else {
- $code = $systemKey === 'cockpit' ? 2 : 1;
- }
- return $code === 2 ? 2 : 1;
- }
- /**
- * 应用后台皮肤到 Config,并返回模板变量
- *
- * @return array{login_system:string,system_style:int,adminskin:string,load_xinhua_theme:bool,system_brand:string}
- */
- public static function applyTheme(Auth $auth = null): array
- {
- $system = self::currentSystem($auth);
- $style = self::styleCode($system);
- // 1 白色:skin-blue-light + xinhua-theme(生产协同)
- // 2 黑色原版:skin-black(驾驶舱;本站 skin-blue 已改成亮蓝,不能当原版用)
- if ($style === 2) {
- $skin = 'skin-black';
- $loadXinhua = false;
- } else {
- $skin = 'skin-blue-light';
- $loadXinhua = true;
- }
- Config::set('fastadmin.adminskin', $skin);
- $brand = (string)self::option($system, 'brand', '');
- if ($brand === '') {
- $brand = (string)self::option($system, 'name', '生产协同系统');
- }
- $flatBrand = Config::get($system === 'cockpit' ? 'mproc.cockpit_system_brand' : 'mproc.collab_system_brand');
- if (is_string($flatBrand) && $flatBrand !== '') {
- $brand = $flatBrand;
- } else {
- $fromMproc = Config::get('mproc.system_brand.' . $system);
- if (is_string($fromMproc) && $fromMproc !== '') {
- $brand = $fromMproc;
- }
- }
- return [
- 'login_system' => $system,
- 'system_style' => $style,
- 'adminskin' => $skin,
- 'load_xinhua_theme' => $loadXinhua,
- 'system_brand' => $brand,
- ];
- }
- }
|