606 lines
16 KiB
Go
606 lines
16 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/pkg/identity"
|
|
"server/pkg/jwtutil"
|
|
"server/services"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
var (
|
|
errNotLogin = errors.New("未登录")
|
|
errAuthFormat = errors.New("认证信息格式错误")
|
|
errTokenInvalid = errors.New("无效的token")
|
|
errNoPermission = errors.New("无权访问")
|
|
)
|
|
|
|
type AppAggregateController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
type appMenuItem struct {
|
|
ID uint64 `json:"id"`
|
|
Pid int64 `json:"pid"`
|
|
Title string `json:"title"`
|
|
Path string `json:"path,omitempty"`
|
|
Icon string `json:"icon,omitempty"`
|
|
Type int8 `json:"type"`
|
|
Sort int64 `json:"sort"`
|
|
Module string `json:"module"`
|
|
Locked bool `json:"locked"`
|
|
Leaf bool `json:"leaf"`
|
|
Docs int `json:"docs,omitempty"`
|
|
}
|
|
|
|
func (c *AppAggregateController) serveJSON(data map[string]interface{}) {
|
|
c.Data["json"] = data
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *AppAggregateController) fail(msg string) {
|
|
c.Ctx.Output.SetStatus(401)
|
|
c.serveJSON(map[string]interface{}{"code": 401, "msg": msg})
|
|
}
|
|
|
|
func (c *AppAggregateController) appClaims() (*jwtutil.Claims, error) {
|
|
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndApp, identity.EndBackend)
|
|
}
|
|
|
|
type appModuleItem struct {
|
|
models.SystemModules
|
|
Enabled bool `json:"enabled"`
|
|
Locked bool `json:"locked"`
|
|
Menus int `json:"menus"`
|
|
}
|
|
|
|
func (c *AppAggregateController) Home() {
|
|
claims, err := c.appClaims()
|
|
if err != nil {
|
|
c.fail(err.Error())
|
|
return
|
|
}
|
|
tid := uint64(claims.TenantId)
|
|
if tid == 0 {
|
|
c.serveJSON(map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": c.noTenantHome(claims),
|
|
})
|
|
return
|
|
}
|
|
uid := services.ResolveTenantUID(tid, uint64(claims.UserID))
|
|
|
|
modules := c.appModules(tid)
|
|
menus := c.appMenus(uid, tid)
|
|
menuCount := map[string]int{}
|
|
for _, m := range menus {
|
|
if m.Module != "" {
|
|
menuCount[m.Module]++
|
|
}
|
|
}
|
|
for i := range modules {
|
|
modules[i].Menus = menuCount[services.NormalizeModuleCode(modules[i].Code)]
|
|
}
|
|
|
|
c.serveJSON(map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": map[string]interface{}{
|
|
"user": c.appUser(claims, uid, tid),
|
|
"modules": modules,
|
|
"menus": menus,
|
|
"stats": c.appStats(claims, tid),
|
|
"quick_menu_ids": c.appQuickMenuIds(tid, uint64(claims.UserID)),
|
|
},
|
|
})
|
|
}
|
|
|
|
func (c *AppAggregateController) noTenantHome(claims *jwtutil.Claims) map[string]interface{} {
|
|
var modRows []models.SystemModules
|
|
_, _ = models.Orm.QueryTable(new(models.SystemModules)).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("status", 1).
|
|
Filter("is_show", 1).
|
|
OrderBy("sort", "id").
|
|
All(&modRows)
|
|
modRows = services.FilterModulesByPlatform(modRows, models.PlatformApp)
|
|
modules := make([]publicModuleItem, 0, len(modRows))
|
|
for _, m := range modRows {
|
|
modules = append(modules, publicModuleItem{
|
|
ID: m.ID, Code: m.Code, Name: m.Name, Icon: m.Icon,
|
|
Path: m.Path, Description: m.Description, RequireLogin: m.RequireLogin,
|
|
})
|
|
}
|
|
|
|
var menuRows []models.SystemMenu
|
|
_, _ = models.Orm.QueryTable(new(models.SystemMenu)).
|
|
Filter("status", 1).
|
|
Filter("require_login", 0).
|
|
All(&menuRows)
|
|
allModules := c.allModules()
|
|
menus := make([]appMenuItem, 0, len(menuRows))
|
|
for _, m := range menuRows {
|
|
if m.Type == 3 {
|
|
continue
|
|
}
|
|
item := appMenuItem{
|
|
ID: m.ID, Pid: m.Pid, Type: m.Type, Sort: m.Sort,
|
|
Title: strings.TrimSpace(m.Title), Leaf: true,
|
|
}
|
|
if m.Path != nil {
|
|
item.Path = strings.TrimSpace(*m.Path)
|
|
}
|
|
if m.Icon != nil {
|
|
item.Icon = strings.TrimSpace(*m.Icon)
|
|
}
|
|
item.Module = appModuleCodeOfPath(item.Path, allModules)
|
|
menus = append(menus, item)
|
|
}
|
|
|
|
state := "none"
|
|
var apply models.AuthTenantApply
|
|
if err := models.Orm.QueryTable(new(models.AuthTenantApply)).
|
|
Filter("identity_id", uint64(claims.UserID)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("-id").One(&apply); err == nil {
|
|
switch apply.Status {
|
|
case models.TenantApplyStatusPending:
|
|
state = "pending"
|
|
case models.TenantApplyStatusReject:
|
|
state = "rejected"
|
|
}
|
|
}
|
|
|
|
displayName := ""
|
|
var ai models.AuthIdentity
|
|
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
|
Filter("id", uint64(claims.UserID)).
|
|
Filter("delete_time__isnull", true).One(&ai); err == nil && ai.Nickname != nil {
|
|
displayName = strings.TrimSpace(*ai.Nickname)
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"user": map[string]interface{}{
|
|
"name": displayName, "account": claims.Username, "tenant": "", "tid": 0, "need_tenant": true,
|
|
},
|
|
"modules": modules,
|
|
"menus": menus,
|
|
"stats": map[string]interface{}{},
|
|
"quick_menu_ids": []uint64{},
|
|
"tenant_state": state,
|
|
}
|
|
}
|
|
|
|
type publicModuleItem struct {
|
|
ID uint64 `json:"id"`
|
|
Code string `json:"code"`
|
|
Name string `json:"name"`
|
|
Icon string `json:"icon"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
RequireLogin int8 `json:"require_login"`
|
|
}
|
|
|
|
func (c *AppAggregateController) PublicModules() {
|
|
var rows []models.SystemModules
|
|
if _, err := models.Orm.QueryTable(new(models.SystemModules)).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("status", 1).
|
|
Filter("is_show", 1).
|
|
OrderBy("sort", "id").
|
|
All(&rows); err != nil {
|
|
c.serveJSON(map[string]interface{}{"code": 200, "msg": "success", "data": []publicModuleItem{}})
|
|
return
|
|
}
|
|
rows = services.FilterModulesByPlatform(rows, models.PlatformApp)
|
|
|
|
out := make([]publicModuleItem, 0, len(rows))
|
|
for _, m := range rows {
|
|
out = append(out, publicModuleItem{
|
|
ID: m.ID,
|
|
Code: m.Code,
|
|
Name: m.Name,
|
|
Icon: m.Icon,
|
|
Path: m.Path,
|
|
Description: m.Description,
|
|
RequireLogin: m.RequireLogin,
|
|
})
|
|
}
|
|
c.serveJSON(map[string]interface{}{"code": 200, "msg": "success", "data": out})
|
|
}
|
|
|
|
func (c *AppAggregateController) PublicMenus() {
|
|
var rows []models.SystemMenu
|
|
if _, err := models.Orm.QueryTable(new(models.SystemMenu)).
|
|
Filter("status", 1).
|
|
Filter("require_login", 0).
|
|
All(&rows); err != nil {
|
|
c.serveJSON(map[string]interface{}{"code": 200, "msg": "success", "data": []appMenuItem{}})
|
|
return
|
|
}
|
|
|
|
allModules := c.allModules()
|
|
sortedModules := services.SortedModulesForScope(allModules)
|
|
|
|
visible := make([]models.SystemMenu, 0, len(rows))
|
|
for _, m := range rows {
|
|
if m.Type == 3 {
|
|
continue
|
|
}
|
|
if m.IsVisible != nil && *m.IsVisible == 0 {
|
|
continue
|
|
}
|
|
if !services.MenuVisibleOn(m, sortedModules, allModules, models.PlatformApp) {
|
|
continue
|
|
}
|
|
visible = append(visible, m)
|
|
}
|
|
|
|
childCount := map[int64]int{}
|
|
for _, m := range visible {
|
|
if m.Pid != 0 {
|
|
childCount[m.Pid]++
|
|
}
|
|
}
|
|
|
|
out := make([]appMenuItem, 0, len(visible))
|
|
for _, m := range visible {
|
|
item := appMenuItem{
|
|
ID: m.ID,
|
|
Pid: m.Pid,
|
|
Type: m.Type,
|
|
Sort: m.Sort,
|
|
Title: strings.TrimSpace(m.Title),
|
|
}
|
|
if m.Path != nil {
|
|
item.Path = strings.TrimSpace(*m.Path)
|
|
}
|
|
if m.Icon != nil {
|
|
item.Icon = strings.TrimSpace(*m.Icon)
|
|
}
|
|
item.Module = appModuleCodeOfPath(item.Path, allModules)
|
|
item.Leaf = childCount[int64(m.ID)] == 0
|
|
out = append(out, item)
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Sort < out[j].Sort })
|
|
c.serveJSON(map[string]interface{}{"code": 200, "msg": "success", "data": out})
|
|
}
|
|
|
|
func (c *AppAggregateController) appQuickMenuIds(tid, uid uint64) []uint64 {
|
|
out := []uint64{}
|
|
var row models.UniappUserQuickMenu
|
|
if err := models.Orm.QueryTable(new(models.UniappUserQuickMenu)).
|
|
Filter("tid", tid).Filter("uid", uid).One(&row); err != nil {
|
|
return out
|
|
}
|
|
raw := strings.TrimSpace(row.MenuIds)
|
|
if raw == "" {
|
|
return out
|
|
}
|
|
var ids []uint64
|
|
if err := json.Unmarshal([]byte(raw), &ids); err != nil {
|
|
return out
|
|
}
|
|
if ids == nil {
|
|
return out
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func (c *AppAggregateController) SaveQuickMenus() {
|
|
claims, err := c.appClaims()
|
|
if err != nil {
|
|
c.fail(err.Error())
|
|
return
|
|
}
|
|
tid := uint64(claims.TenantId)
|
|
uid := uint64(claims.UserID)
|
|
|
|
var body struct {
|
|
MenuIds []uint64 `json:"menu_ids"`
|
|
}
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &body); err != nil {
|
|
c.Ctx.Output.SetStatus(400)
|
|
c.serveJSON(map[string]interface{}{"code": 400, "msg": "请求体格式错误"})
|
|
return
|
|
}
|
|
if body.MenuIds == nil {
|
|
body.MenuIds = []uint64{}
|
|
}
|
|
raw, _ := json.Marshal(body.MenuIds)
|
|
|
|
now := time.Now()
|
|
var row models.UniappUserQuickMenu
|
|
if err := models.Orm.QueryTable(new(models.UniappUserQuickMenu)).
|
|
Filter("tid", tid).Filter("uid", uid).One(&row); err == nil {
|
|
_, _ = models.Orm.QueryTable(new(models.UniappUserQuickMenu)).
|
|
Filter("id", row.ID).Update(orm.Params{
|
|
"menu_ids": string(raw),
|
|
"update_time": now,
|
|
})
|
|
} else {
|
|
_, _ = models.Orm.Insert(&models.UniappUserQuickMenu{
|
|
Tid: tid,
|
|
Uid: uid,
|
|
MenuIds: string(raw),
|
|
CreateTime: now,
|
|
UpdateTime: &now,
|
|
})
|
|
}
|
|
c.serveJSON(map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": map[string]interface{}{"menu_ids": body.MenuIds},
|
|
})
|
|
}
|
|
|
|
func (c *AppAggregateController) appUser(claims *jwtutil.Claims, uid, tid uint64) map[string]interface{} {
|
|
out := map[string]interface{}{
|
|
"uid": uid,
|
|
"tid": tid,
|
|
"name": "",
|
|
"account": "",
|
|
"role_name": "",
|
|
"tenant": "",
|
|
"tenant_id": tid,
|
|
"avatar": "",
|
|
}
|
|
var ai models.AuthIdentity
|
|
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
|
Filter("id", uint64(claims.UserID)).
|
|
Filter("delete_time__isnull", true).One(&ai); err == nil && ai.Nickname != nil {
|
|
out["name"] = strings.TrimSpace(*ai.Nickname)
|
|
}
|
|
var tu models.SystemTenantUser
|
|
if models.Orm.QueryTable(new(models.SystemTenantUser)).
|
|
Filter("uid", uid).Filter("tid", tid).One(&tu) == nil {
|
|
if tu.Name != nil && strings.TrimSpace(*tu.Name) != "" {
|
|
out["name"] = strings.TrimSpace(*tu.Name)
|
|
}
|
|
if tu.Account != nil && strings.TrimSpace(*tu.Account) != "" {
|
|
out["account"] = strings.TrimSpace(*tu.Account)
|
|
}
|
|
if tu.GroupID > 0 {
|
|
var role models.AdminRole
|
|
if models.Orm.QueryTable(new(models.AdminRole)).
|
|
Filter("id", tu.GroupID).Filter("cid", 2).One(&role) == nil {
|
|
if strings.TrimSpace(role.Name) != "" {
|
|
out["role_name"] = strings.TrimSpace(role.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
var tenant models.SystemTenant
|
|
if models.Orm.QueryTable(new(models.SystemTenant)).Filter("id", tid).One(&tenant) == nil {
|
|
if strings.TrimSpace(tenant.TenantName) != "" {
|
|
out["tenant"] = strings.TrimSpace(tenant.TenantName)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (c *AppAggregateController) appModules(tid uint64) []appModuleItem {
|
|
var rows []models.SystemModules
|
|
if _, err := models.Orm.QueryTable(new(models.SystemModules)).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("status", 1).
|
|
Filter("is_show", 1).
|
|
OrderBy("sort", "id").
|
|
All(&rows); err != nil {
|
|
return []appModuleItem{}
|
|
}
|
|
|
|
rows = services.FilterModulesByPlatform(rows, models.PlatformApp)
|
|
|
|
allowed := services.GetTenantModuleCodes(tid)
|
|
out := make([]appModuleItem, 0, len(rows))
|
|
for _, m := range rows {
|
|
enabled := true
|
|
if len(allowed) > 0 {
|
|
enabled = allowed[services.NormalizeModuleCode(m.Code)]
|
|
}
|
|
out = append(out, appModuleItem{
|
|
SystemModules: m,
|
|
Enabled: enabled,
|
|
Locked: !enabled,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (c *AppAggregateController) appMenus(uid, tid uint64) []appMenuItem {
|
|
var rows []models.SystemMenu
|
|
if _, err := models.Orm.QueryTable(new(models.SystemMenu)).
|
|
Filter("status", 1).All(&rows); err != nil {
|
|
return []appMenuItem{}
|
|
}
|
|
menus := filterMenusByView(rows, 2)
|
|
|
|
if uid > 0 {
|
|
var tu models.SystemTenantUser
|
|
if models.Orm.QueryTable(new(models.SystemTenantUser)).
|
|
Filter("uid", uid).Filter("tid", tid).One(&tu) == nil && tu.GroupID > 0 {
|
|
var role models.AdminRole
|
|
if models.Orm.QueryTable(new(models.AdminRole)).
|
|
Filter("id", tu.GroupID).Filter("cid", 2).One(&role) == nil {
|
|
menus = filterMenusByRights(menus, role.Rights)
|
|
}
|
|
}
|
|
}
|
|
|
|
menus, lockedIDs := filterMenusByTenantPackageLocked("", menus, tid, true)
|
|
|
|
allModules := c.allModules()
|
|
|
|
// 第一遍:按展示端筛出本端可见菜单(is_visible=0 的也不下发)。
|
|
// 必须先筛再统计子节点,否则会出现「父节点非叶子但零个可见子项」的死路。
|
|
// 按展示端筛选:菜单自身未配置时继承所属模块
|
|
sortedModules := services.SortedModulesForScope(allModules)
|
|
visible := make([]models.SystemMenu, 0, len(menus))
|
|
for _, m := range menus {
|
|
if m.IsVisible != nil && *m.IsVisible == 0 {
|
|
continue
|
|
}
|
|
if !services.MenuVisibleOn(m, sortedModules, allModules, models.PlatformApp) {
|
|
continue
|
|
}
|
|
visible = append(visible, m)
|
|
}
|
|
|
|
childCount := map[int64]int{}
|
|
for _, m := range visible {
|
|
if m.Pid != 0 {
|
|
childCount[m.Pid]++
|
|
}
|
|
}
|
|
|
|
out := make([]appMenuItem, 0, len(visible))
|
|
for _, m := range visible {
|
|
item := appMenuItem{
|
|
ID: m.ID,
|
|
Pid: m.Pid,
|
|
Type: m.Type,
|
|
Sort: m.Sort,
|
|
Title: strings.TrimSpace(m.Title),
|
|
}
|
|
if m.Path != nil {
|
|
item.Path = strings.TrimSpace(*m.Path)
|
|
}
|
|
if m.Icon != nil {
|
|
item.Icon = strings.TrimSpace(*m.Icon)
|
|
}
|
|
if lockedIDs != nil {
|
|
item.Locked = lockedIDs[m.ID]
|
|
}
|
|
item.Module = appModuleCodeOfPath(item.Path, allModules)
|
|
item.Leaf = childCount[int64(m.ID)] == 0
|
|
item.Docs = childCount[int64(m.ID)]
|
|
out = append(out, item)
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Sort < out[j].Sort })
|
|
return out
|
|
}
|
|
|
|
func pathOf(m models.SystemMenu) string {
|
|
if m.Path == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(*m.Path)
|
|
}
|
|
|
|
func appModuleCodeOfPath(path string, allModules []models.SystemModules) string {
|
|
p := strings.TrimSuffix(strings.TrimSpace(path), "/")
|
|
if p == "" {
|
|
return ""
|
|
}
|
|
sorted := make([]models.SystemModules, len(allModules))
|
|
copy(sorted, allModules)
|
|
sort.SliceStable(sorted, func(i, j int) bool {
|
|
return len(sorted[i].Path) > len(sorted[j].Path)
|
|
})
|
|
for _, m := range sorted {
|
|
mp := strings.TrimSuffix(strings.TrimSpace(m.Path), "/")
|
|
if mp == "" {
|
|
continue
|
|
}
|
|
if p == mp || strings.HasPrefix(p, mp+"/") {
|
|
return services.NormalizeModuleCode(m.Code)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (c *AppAggregateController) allModules() []models.SystemModules {
|
|
var rows []models.SystemModules
|
|
if _, err := models.Orm.QueryTable(new(models.SystemModules)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("sort", "id").
|
|
All(&rows); err != nil {
|
|
return nil
|
|
}
|
|
return rows
|
|
}
|
|
|
|
func (c *AppAggregateController) appStats(claims *jwtutil.Claims, tid uint64) map[string]interface{} {
|
|
uid := claims.UserID
|
|
now := time.Now()
|
|
today := now.Format("2006-01-02")
|
|
|
|
todo, overdue := 0, 0
|
|
{
|
|
var rows []models.BackendSchedule
|
|
if _, err := models.Orm.QueryTable(new(models.BackendSchedule)).
|
|
Filter("user_id", uid).Filter("tid", claims.TenantId).
|
|
OrderBy("-id").Limit(500).All(&rows); err == nil {
|
|
for _, s := range rows {
|
|
var reminders []models.BackendScheduleReminder
|
|
if _, e := models.Orm.QueryTable(new(models.BackendScheduleReminder)).
|
|
Filter("schedule_id", s.ID).Filter("is_deleted", 0).
|
|
All(&reminders); e != nil {
|
|
continue
|
|
}
|
|
if len(reminders) == 0 {
|
|
todo++
|
|
if s.ScheduleTime.Format("2006-01-02") < today {
|
|
overdue++
|
|
}
|
|
continue
|
|
}
|
|
done := true
|
|
for _, r := range reminders {
|
|
if r.RemindStatus != 2 {
|
|
done = false
|
|
break
|
|
}
|
|
}
|
|
if !done {
|
|
todo++
|
|
if s.ScheduleTime.Format("2006-01-02") < today {
|
|
overdue++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
note := 0
|
|
{
|
|
cnt, _ := models.Orm.QueryTable(new(models.BackendNotebook)).
|
|
Filter("user_id", uid).Filter("is_deleted", 0).Count()
|
|
note = int(cnt)
|
|
}
|
|
|
|
message := 0
|
|
{
|
|
cnt, _ := models.Orm.QueryTable(new(models.SystemReminderList)).
|
|
Filter("receiver_id", uid).Filter("receiver_type", "tenant").
|
|
Filter("is_read", 0).Filter("delete_time__isnull", true).Count()
|
|
message = int(cnt)
|
|
}
|
|
|
|
reimburse := 0
|
|
{
|
|
cnt, _ := models.Orm.QueryTable(new(models.BackendReimbursement)).
|
|
Filter("user_id", uid).Filter("tid", tid).
|
|
Filter("is_deleted", 0).Filter("status", 1).Count()
|
|
reimburse = int(cnt)
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"todo": todo,
|
|
"overdue": overdue,
|
|
"note": note,
|
|
"message": message,
|
|
"reimburse": reimburse,
|
|
}
|
|
}
|