Menu.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package controllers
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "Cold_Api/models/Account"
  5. "encoding/json"
  6. "github.com/beego/beego/v2/core/logs"
  7. beego "github.com/beego/beego/v2/server/web"
  8. )
  9. type MenuController struct {
  10. beego.Controller
  11. Admin_r Account.Admin // 登陆的用户
  12. T_pid int // 公司id
  13. }
  14. func (c *MenuController) Prepare() {
  15. GetCookie := c.Ctx.GetCookie("User_tokey")
  16. GetString := c.GetString("User_tokey")
  17. User_tokey := GetString
  18. if len(User_tokey) == 0 {
  19. User_tokey = GetCookie
  20. }
  21. if Account.Admin_r == nil {
  22. return
  23. }
  24. c.Admin_r = *Account.Admin_r
  25. T_pid := c.Admin_r.T_pid
  26. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  27. if EntryPid > 0 {
  28. T_pid = EntryPid
  29. }
  30. c.T_pid = T_pid
  31. }
  32. // Menu_List 获取菜单列表(树形结构)
  33. func (c *MenuController) Menu_List() {
  34. var r_jsons lib.R_JSONS
  35. // 获取所有菜单
  36. maps, _ := Account.Read_Menu_List_All()
  37. r_jsons.Data = maps
  38. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  39. c.ServeJSON()
  40. return
  41. }
  42. // Menu_Get 根据ID获取菜单详情
  43. func (c *MenuController) Menu_Get() {
  44. var r_jsons lib.R_JSONS
  45. id, _ := c.GetInt("Id")
  46. if id == 0 {
  47. c.Data["json"] = lib.JSONS{Code: 400, Msg: "参数错误:id格式不正确"}
  48. c.ServeJSON()
  49. return
  50. }
  51. menu, err := Account.Read_Menu_ById(id)
  52. if err != nil {
  53. logs.Error("获取菜单失败:", err)
  54. c.Data["json"] = lib.JSONS{Code: 500, Msg: "获取菜单失败"}
  55. c.ServeJSON()
  56. return
  57. }
  58. // 获取该菜单绑定的API列表
  59. apis := Account.Read_API_List_ByMenuId(id)
  60. result := map[string]interface{}{
  61. "menu": menu,
  62. "apis": apis,
  63. }
  64. r_jsons.Data = result
  65. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  66. c.ServeJSON()
  67. return
  68. }
  69. // Menu_Add 添加菜单
  70. func (c *MenuController) Menu_Add() {
  71. // 从form-data中获取菜单信息
  72. var menu Account.Menu
  73. // 获取菜单基本信息
  74. menu.T_mid, _ = c.GetInt("T_mid")
  75. menu.T_name = c.GetString("T_name")
  76. menu.T_permission = c.GetString("T_permission")
  77. menu.T_icon = c.GetString("T_icon")
  78. menu.T_type = c.GetString("T_type")
  79. menu.T_sort, _ = c.GetInt("T_sort")
  80. menu.T_pid, _ = c.GetInt("T_pid") // 1 进入公司后才能访问的菜单 0 公共菜单
  81. // 验证必填字段
  82. if len(menu.T_name) == 0 {
  83. c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单名称不能为空"}
  84. c.ServeJSON()
  85. return
  86. }
  87. if len(menu.T_permission) == 0 {
  88. c.Data["json"] = lib.JSONS{Code: 400, Msg: "权限标识不能为空"}
  89. c.ServeJSON()
  90. return
  91. }
  92. if len(menu.T_type) == 0 {
  93. menu.T_type = Account.MenuType // 默认为菜单类型
  94. }
  95. menu.T_State = 1
  96. // 添加菜单
  97. id, err := Account.Add_Menu(menu)
  98. if err != nil {
  99. logs.Error("添加菜单失败:", err)
  100. c.Data["json"] = lib.JSONS{Code: 500, Msg: "添加菜单失败"}
  101. c.ServeJSON()
  102. return
  103. }
  104. // 处理API绑定
  105. apisJsonStr := c.GetString("apis")
  106. if len(apisJsonStr) > 0 {
  107. var apis []Account.API
  108. err := json.Unmarshal([]byte(apisJsonStr), &apis)
  109. if err != nil {
  110. logs.Error("解析API JSON字符串失败:", err)
  111. c.Data["json"] = lib.JSONS{Code: 400, Msg: "API JSON格式错误"}
  112. c.ServeJSON()
  113. return
  114. }
  115. // 为每个API设置菜单ID并添加
  116. for _, api := range apis {
  117. api.T_Menu_Id = int(id)
  118. api.T_enable = 1 // 默认启用
  119. if len(api.T_method) == 0 {
  120. api.T_method = "POST" // 默认GET方法
  121. }
  122. _, err := Account.Add_API(api)
  123. if err != nil {
  124. logs.Error("添加API绑定失败:", err)
  125. // 继续处理其他API,不中断整个流程
  126. }
  127. }
  128. }
  129. // 清空所有菜单和API缓存
  130. go func() {
  131. Account.Redis_Menu_ClearAll()
  132. Account.Redis_API_ClearAll()
  133. }()
  134. c.Data["json"] = lib.JSONS{Code: 200, Msg: "添加成功!", Data: id}
  135. c.ServeJSON()
  136. return
  137. }
  138. // Menu_Edit 编辑菜单(包含API绑定)
  139. func (c *MenuController) Menu_Edit() {
  140. // 从form-data中获取菜单信息
  141. var menu Account.Menu
  142. // 获取菜单ID(必填)
  143. id, _ := c.GetInt("Id")
  144. if id == 0 {
  145. c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单ID不能为空"}
  146. c.ServeJSON()
  147. return
  148. }
  149. menu, err := Account.Read_Menu_ById(id)
  150. if err != nil {
  151. logs.Error("获取菜单失败:", err)
  152. c.Data["json"] = lib.JSONS{Code: 500, Msg: "获取菜单失败"}
  153. c.ServeJSON()
  154. return
  155. }
  156. // 获取菜单基本信息
  157. menu.T_mid, _ = c.GetInt("T_mid")
  158. menu.T_name = c.GetString("T_name")
  159. menu.T_permission = c.GetString("T_permission")
  160. menu.T_icon = c.GetString("T_icon")
  161. menu.T_type = c.GetString("T_type")
  162. menu.T_sort, _ = c.GetInt("T_sort")
  163. menu.T_pid, _ = c.GetInt("T_pid")
  164. // 验证必填字段
  165. if len(menu.T_name) == 0 {
  166. c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单名称不能为空"}
  167. c.ServeJSON()
  168. return
  169. }
  170. if len(menu.T_permission) == 0 {
  171. c.Data["json"] = lib.JSONS{Code: 400, Msg: "权限标识不能为空"}
  172. c.ServeJSON()
  173. return
  174. }
  175. // 更新菜单信息
  176. success := Account.Update_Menu(menu)
  177. if !success {
  178. logs.Error("更新菜单失败")
  179. c.Data["json"] = lib.JSONS{Code: 500, Msg: "更新菜单失败"}
  180. c.ServeJSON()
  181. return
  182. }
  183. // 处理API绑定
  184. apisJsonStr := c.GetString("apis")
  185. if len(apisJsonStr) > 0 {
  186. var apis []Account.API
  187. err = json.Unmarshal([]byte(apisJsonStr), &apis)
  188. if err != nil {
  189. logs.Error("解析API JSON字符串失败:", err)
  190. c.Data["json"] = lib.JSONS{Code: 400, Msg: "API JSON格式错误"}
  191. c.ServeJSON()
  192. return
  193. }
  194. // 先删除该菜单下的所有API
  195. existingAPIs := Account.Read_API_List_ByMenuId(menu.Id)
  196. for _, api := range existingAPIs {
  197. Account.Delete_API(api)
  198. }
  199. // 添加新的API绑定
  200. for _, api := range apis {
  201. api.T_Menu_Id = menu.Id
  202. if len(api.T_method) == 0 {
  203. api.T_method = "POST" // 默认GET方法
  204. }
  205. _, err := Account.Add_API(api)
  206. if err != nil {
  207. logs.Error("添加API绑定失败:", err)
  208. // 继续处理其他API,不中断整个流程
  209. }
  210. }
  211. }
  212. // 清空所有菜单和API缓存
  213. go func() {
  214. Account.Redis_Menu_ClearAll()
  215. Account.Redis_API_ClearAll()
  216. }()
  217. c.Data["json"] = lib.JSONS{Code: 200, Msg: "更新成功!", Data: id}
  218. c.ServeJSON()
  219. return
  220. }
  221. // Menu_Del 删除菜单
  222. func (c *MenuController) Menu_Del() {
  223. // 获取菜单ID(必填)
  224. id, _ := c.GetInt("T_id")
  225. if id == 0 {
  226. c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单ID不能为空"}
  227. c.ServeJSON()
  228. return
  229. }
  230. // 检查是否有子菜单
  231. maps, _ := Account.Read_Menu_List_All()
  232. hasChildren := false
  233. for _, menu := range maps {
  234. if menu.T_mid == id {
  235. hasChildren = true
  236. break
  237. }
  238. }
  239. if hasChildren {
  240. c.Data["json"] = lib.JSONS{Code: 400, Msg: "该菜单下还有子菜单,无法删除"}
  241. c.ServeJSON()
  242. return
  243. }
  244. // 删除菜单
  245. menu := Account.Menu{Id: id}
  246. success := Account.Delete_Menu(menu)
  247. if !success {
  248. logs.Error("删除菜单失败")
  249. c.Data["json"] = lib.JSONS{Code: 500, Msg: "删除菜单失败"}
  250. c.ServeJSON()
  251. return
  252. }
  253. // 删除该菜单下的所有API绑定
  254. apis := Account.Read_API_List_ByMenuId(id)
  255. for _, api := range apis {
  256. Account.Delete_API(api)
  257. }
  258. // 清空所有菜单和API缓存
  259. go func() {
  260. Account.Redis_Menu_ClearAll()
  261. Account.Redis_API_ClearAll()
  262. }()
  263. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除成功!", Data: id}
  264. c.ServeJSON()
  265. return
  266. }
  267. // Menu_Sort 更新菜单排序
  268. func (c *MenuController) Menu_Sort() {
  269. var r_jsons lib.R_JSONS
  270. var requestData struct {
  271. MenuId int `json:"menu_id"`
  272. Sort int `json:"sort"`
  273. }
  274. err := json.Unmarshal(c.Ctx.Input.RequestBody, &requestData)
  275. if err != nil {
  276. logs.Error("解析数据失败:", err)
  277. c.Data["json"] = lib.JSONS{Code: 400, Msg: "参数格式错误"}
  278. c.ServeJSON()
  279. return
  280. }
  281. if requestData.MenuId == 0 {
  282. c.Data["json"] = lib.JSONS{Code: 400, Msg: "菜单ID不能为空"}
  283. c.ServeJSON()
  284. return
  285. }
  286. // 获取菜单信息
  287. menu, err := Account.Read_Menu_ById(requestData.MenuId)
  288. if err != nil {
  289. logs.Error("获取菜单失败:", err)
  290. c.Data["json"] = lib.JSONS{Code: 500, Msg: "获取菜单失败"}
  291. c.ServeJSON()
  292. return
  293. }
  294. // 更新排序
  295. menu.T_sort = requestData.Sort
  296. success := Account.Update_Menu(menu, "T_sort")
  297. if !success {
  298. logs.Error("更新菜单排序失败")
  299. c.Data["json"] = lib.JSONS{Code: 500, Msg: "更新菜单排序失败"}
  300. c.ServeJSON()
  301. return
  302. }
  303. c.Data["json"] = lib.JSONS{Code: 200, Msg: "更新排序成功!", Data: r_jsons}
  304. c.ServeJSON()
  305. return
  306. }