package controllers import ( "Cold_Api/controllers/lib" "Cold_Api/models/Account" "encoding/json" "github.com/beego/beego/v2/core/logs" beego "github.com/beego/beego/v2/server/web" ) type MenuController struct { beego.Controller Admin_r Account.Admin // 登陆的用户 T_pid int // 公司id } func (c *MenuController) Prepare() { GetCookie := c.Ctx.GetCookie("User_tokey") GetString := c.GetString("User_tokey") User_tokey := GetString if len(User_tokey) == 0 { User_tokey = GetCookie } if Account.Admin_r == nil { return } c.Admin_r = *Account.Admin_r T_pid := c.Admin_r.T_pid EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey) if EntryPid > 0 { T_pid = EntryPid } c.T_pid = T_pid } // Menu_List 获取菜单列表(树形结构) func (c *MenuController) Menu_List() { var r_jsons lib.R_JSONS // 获取所有菜单 maps, _ := Account.Read_Menu_List_All() r_jsons.Data = maps c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // Menu_Get 根据ID获取菜单详情 func (c *MenuController) Menu_Get() { var r_jsons lib.R_JSONS id, _ := c.GetInt("Id") if id == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "参数错误:id格式不正确"} c.ServeJSON() return } menu, err := Account.Read_Menu_ById(id) if err != nil { logs.Error("获取菜单失败:", err) c.Data["json"] = lib.JSONS{Code: 500, Msg: "获取菜单失败"} c.ServeJSON() return } // 获取该菜单绑定的API列表 apis := Account.Read_API_List_ByMenuId(id) result := map[string]interface{}{ "menu": menu, "apis": apis, } r_jsons.Data = result c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // Menu_Add 添加菜单 func (c *MenuController) Menu_Add() { // 从form-data中获取菜单信息 var menu Account.Menu // 获取菜单基本信息 menu.T_mid, _ = c.GetInt("T_mid") menu.T_name = c.GetString("T_name") menu.T_permission = c.GetString("T_permission") menu.T_icon = c.GetString("T_icon") menu.T_type = c.GetString("T_type") menu.T_sort, _ = c.GetInt("T_sort") menu.T_pid, _ = c.GetInt("T_pid") // 1 进入公司后才能访问的菜单 0 公共菜单 // 验证必填字段 if len(menu.T_name) == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单名称不能为空"} c.ServeJSON() return } if len(menu.T_permission) == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "权限标识不能为空"} c.ServeJSON() return } if len(menu.T_type) == 0 { menu.T_type = Account.MenuType // 默认为菜单类型 } menu.T_State = 1 // 添加菜单 id, err := Account.Add_Menu(menu) if err != nil { logs.Error("添加菜单失败:", err) c.Data["json"] = lib.JSONS{Code: 500, Msg: "添加菜单失败"} c.ServeJSON() return } // 处理API绑定 apisJsonStr := c.GetString("apis") if len(apisJsonStr) > 0 { var apis []Account.API err := json.Unmarshal([]byte(apisJsonStr), &apis) if err != nil { logs.Error("解析API JSON字符串失败:", err) c.Data["json"] = lib.JSONS{Code: 400, Msg: "API JSON格式错误"} c.ServeJSON() return } // 为每个API设置菜单ID并添加 for _, api := range apis { api.T_Menu_Id = int(id) api.T_enable = 1 // 默认启用 if len(api.T_method) == 0 { api.T_method = "POST" // 默认GET方法 } _, err := Account.Add_API(api) if err != nil { logs.Error("添加API绑定失败:", err) // 继续处理其他API,不中断整个流程 } } } // 清空所有菜单和API缓存 go func() { Account.Redis_Menu_ClearAll() Account.Redis_API_ClearAll() }() c.Data["json"] = lib.JSONS{Code: 200, Msg: "添加成功!", Data: id} c.ServeJSON() return } // Menu_Edit 编辑菜单(包含API绑定) func (c *MenuController) Menu_Edit() { // 从form-data中获取菜单信息 var menu Account.Menu // 获取菜单ID(必填) id, _ := c.GetInt("Id") if id == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单ID不能为空"} c.ServeJSON() return } menu, err := Account.Read_Menu_ById(id) if err != nil { logs.Error("获取菜单失败:", err) c.Data["json"] = lib.JSONS{Code: 500, Msg: "获取菜单失败"} c.ServeJSON() return } // 获取菜单基本信息 menu.T_mid, _ = c.GetInt("T_mid") menu.T_name = c.GetString("T_name") menu.T_permission = c.GetString("T_permission") menu.T_icon = c.GetString("T_icon") menu.T_type = c.GetString("T_type") menu.T_sort, _ = c.GetInt("T_sort") menu.T_pid, _ = c.GetInt("T_pid") // 验证必填字段 if len(menu.T_name) == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单名称不能为空"} c.ServeJSON() return } if len(menu.T_permission) == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "权限标识不能为空"} c.ServeJSON() return } // 更新菜单信息 success := Account.Update_Menu(menu) if !success { logs.Error("更新菜单失败") c.Data["json"] = lib.JSONS{Code: 500, Msg: "更新菜单失败"} c.ServeJSON() return } // 处理API绑定 apisJsonStr := c.GetString("apis") if len(apisJsonStr) > 0 { var apis []Account.API err = json.Unmarshal([]byte(apisJsonStr), &apis) if err != nil { logs.Error("解析API JSON字符串失败:", err) c.Data["json"] = lib.JSONS{Code: 400, Msg: "API JSON格式错误"} c.ServeJSON() return } // 先删除该菜单下的所有API existingAPIs := Account.Read_API_List_ByMenuId(menu.Id) for _, api := range existingAPIs { Account.Delete_API(api) } // 添加新的API绑定 for _, api := range apis { api.T_Menu_Id = menu.Id if len(api.T_method) == 0 { api.T_method = "POST" // 默认GET方法 } _, err := Account.Add_API(api) if err != nil { logs.Error("添加API绑定失败:", err) // 继续处理其他API,不中断整个流程 } } } // 清空所有菜单和API缓存 go func() { Account.Redis_Menu_ClearAll() Account.Redis_API_ClearAll() }() c.Data["json"] = lib.JSONS{Code: 200, Msg: "更新成功!", Data: id} c.ServeJSON() return } // Menu_Del 删除菜单 func (c *MenuController) Menu_Del() { // 获取菜单ID(必填) id, _ := c.GetInt("T_id") if id == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单ID不能为空"} c.ServeJSON() return } // 检查是否有子菜单 maps, _ := Account.Read_Menu_List_All() hasChildren := false for _, menu := range maps { if menu.T_mid == id { hasChildren = true break } } if hasChildren { c.Data["json"] = lib.JSONS{Code: 400, Msg: "该菜单下还有子菜单,无法删除"} c.ServeJSON() return } // 删除菜单 menu := Account.Menu{Id: id} success := Account.Delete_Menu(menu) if !success { logs.Error("删除菜单失败") c.Data["json"] = lib.JSONS{Code: 500, Msg: "删除菜单失败"} c.ServeJSON() return } // 删除该菜单下的所有API绑定 apis := Account.Read_API_List_ByMenuId(id) for _, api := range apis { Account.Delete_API(api) } // 清空所有菜单和API缓存 go func() { Account.Redis_Menu_ClearAll() Account.Redis_API_ClearAll() }() c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除成功!", Data: id} c.ServeJSON() return } // Menu_Sort 更新菜单排序 func (c *MenuController) Menu_Sort() { var r_jsons lib.R_JSONS var requestData struct { MenuId int `json:"menu_id"` Sort int `json:"sort"` } err := json.Unmarshal(c.Ctx.Input.RequestBody, &requestData) if err != nil { logs.Error("解析数据失败:", err) c.Data["json"] = lib.JSONS{Code: 400, Msg: "参数格式错误"} c.ServeJSON() return } if requestData.MenuId == 0 { c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单ID不能为空"} c.ServeJSON() return } // 获取菜单信息 menu, err := Account.Read_Menu_ById(requestData.MenuId) if err != nil { logs.Error("获取菜单失败:", err) c.Data["json"] = lib.JSONS{Code: 500, Msg: "获取菜单失败"} c.ServeJSON() return } // 更新排序 menu.T_sort = requestData.Sort success := Account.Update_Menu(menu, "T_sort") if !success { logs.Error("更新菜单排序失败") c.Data["json"] = lib.JSONS{Code: 500, Msg: "更新菜单排序失败"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "更新排序成功!", Data: r_jsons} c.ServeJSON() return }