优化数据归属问题

This commit is contained in:
2026-04-23 15:08:37 +08:00
parent 378be9909d
commit 0373234750
29 changed files with 1993 additions and 75 deletions

View File

@@ -278,35 +278,41 @@ class Auth extends \ba\Auth
public function getMenus(int $uid = 0): array
{
$menus = parent::getMenus($uid ?: $this->id);
// 库内 title 为中文;仅英文界面走 __()。若对 zh-cn 也 __()Symfony 在找不到键时会 fallback 到 en
// 命中 admin_rule_title 后会把中文标题误译成英文。
$localeNorm = str_replace('_', '-', strtolower(locale()));
$toEnglish = ($localeNorm === 'en');
return $this->translateMenuRuleTitles($menus, $toEnglish);
return $this->translateMenuRuleTitles($menus);
}
/**
* 将 admin_rule.title 在英文界面译为英文;中文界面保持库内原文
* 英文映射见 app/common/lang/en/admin_rule_title.php
* 菜单标题统一按中文显示(不随语言切换)
* 若 title 为英文动作名/英文菜单名,按中文映射表转换。
*
* @param array<int, array<string, mixed>> $menus
* @return array<int, array<string, mixed>>
*/
private function translateMenuRuleTitles(array $menus, bool $toEnglish): array
private function translateMenuRuleTitles(array $menus): array
{
foreach ($menus as $k => $item) {
if (isset($item['title']) && is_string($item['title']) && $item['title'] !== '') {
$menus[$k]['title'] = $toEnglish ? __($item['title']) : $item['title'];
$menus[$k]['title'] = $this->menuTitleToZh($item['title']);
}
if (!empty($item['children']) && is_array($item['children'])) {
$menus[$k]['children'] = $this->translateMenuRuleTitles($item['children'], $toEnglish);
$menus[$k]['children'] = $this->translateMenuRuleTitles($item['children']);
}
}
return $menus;
}
private function menuTitleToZh(string $title): string
{
static $zhMap = null;
if (!is_array($zhMap)) {
$mapFile = app_path() . '/common/lang/zh-cn/admin_rule_title.php';
$loaded = is_file($mapFile) ? include $mapFile : [];
$zhMap = is_array($loaded) ? $loaded : [];
}
return isset($zhMap[$title]) && is_string($zhMap[$title]) ? $zhMap[$title] : $title;
}
public function isSuperAdmin(): bool
{
return in_array('*', $this->getRuleIds());