Files
yunzerwebsiteallinone/go/services/display_scope.go
T
2026-11-16 17:22:50 +08:00

128 lines
3.0 KiB
Go

package services
import (
"encoding/json"
"strings"
"server/models"
)
func ParsePlatforms(raw *string) []string {
if raw == nil || strings.TrimSpace(*raw) == "" {
return nil
}
var arr []string
if err := json.Unmarshal([]byte(*raw), &arr); err == nil {
out := make([]string, 0, len(arr))
for _, s := range arr {
s = strings.ToLower(strings.TrimSpace(s))
if s != "" {
out = append(out, s)
}
}
return out
}
v := strings.ToLower(strings.TrimSpace(*raw))
if v == "" {
return nil
}
return []string{v}
}
func HasPlatform(list []string, platform string) bool {
if len(list) == 0 {
return true
}
p := strings.ToLower(strings.TrimSpace(platform))
for _, s := range list {
if strings.ToLower(strings.TrimSpace(s)) == p {
return true
}
}
return false
}
func NormalizePlatforms(list []string) string {
set := map[string]bool{}
out := make([]string, 0, len(list))
for _, s := range list {
v := strings.ToLower(strings.TrimSpace(s))
if v == "" || set[v] {
continue
}
set[v] = true
out = append(out, v)
}
if len(out) == 0 {
return ""
}
if b, err := json.Marshal(out); err == nil {
return string(b)
}
return ""
}
func ListAllModulesForScope() ([]models.SystemModules, error) {
var rows []models.SystemModules
_, err := models.Orm.QueryTable(new(models.SystemModules)).
Filter("delete_time__isnull", true).
OrderBy("sort", "id").
All(&rows)
return rows, err
}
func FilterModulesByPlatform(rows []models.SystemModules, platform string) []models.SystemModules {
out := make([]models.SystemModules, 0, len(rows))
for _, m := range rows {
if HasPlatform(ParsePlatforms(m.Platforms), platform) {
out = append(out, m)
}
}
return out
}
func FilterMenusByPlatform(rows []models.SystemMenu, platform string) []models.SystemMenu {
out := make([]models.SystemMenu, 0, len(rows))
for _, m := range rows {
if HasPlatform(ParsePlatforms(m.Platforms), platform) {
out = append(out, m)
}
}
return out
}
func SortedModulesForScope(modules []models.SystemModules) []models.SystemModules {
return sortModulesByPathDesc(modules)
}
func MenuVisibleOn(m models.SystemMenu, sortedModules []models.SystemModules, allModules []models.SystemModules, platform string) bool {
if own := ParsePlatforms(m.Platforms); len(own) > 0 {
return HasPlatform(own, platform)
}
path := ""
if m.Path != nil {
path = strings.TrimSpace(*m.Path)
}
code, ok := ownerModuleCode(sortedModules, path)
if !ok {
return true
}
for _, mod := range allModules {
if NormalizeModuleCode(mod.Code) == NormalizeModuleCode(code) {
return HasPlatform(ParsePlatforms(mod.Platforms), platform)
}
}
return true
}
func FilterMenusByPlatformWithModules(rows []models.SystemMenu, modules []models.SystemModules, platform string) []models.SystemMenu {
sorted := sortModulesByPathDesc(modules)
out := make([]models.SystemMenu, 0, len(rows))
for _, m := range rows {
if MenuVisibleOn(m, sorted, modules, platform) {
out = append(out, m)
}
}
return out
}