GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
11 KiB

  1. package models
  2. import (
  3. "LAPP_GAAS_GFrame_BACKEND/db"
  4. "LAPP_GAAS_GFrame_BACKEND/utils"
  5. "errors"
  6. )
  7. /***
  8. *CreateBy:lou
  9. *Date:2020/5/15
  10. *TableName:菜单表
  11. **********/
  12. type Menu struct {
  13. MenuId int `xorm:"not null pk comment('菜单ID') int(0)" form:"menu_id" json:"menu_id"`
  14. MenuName string `xorm:"comment('菜单标题') VARCHAR(40)" form:"menu_name" json:"menu_name"`
  15. Title string `xorm:"comment('菜单标题') VARCHAR(40)" form:"title" json:"title"`
  16. Icon string `xorm:"comment('菜单标题') VARCHAR(40)" form:"icon" json:"icon"`
  17. Path string `xorm:"comment('菜单标题') VARCHAR(128)" form:"path" json:"path"`
  18. Paths string `xorm:"comment('菜单标题') VARCHAR(128)" form:"paths" json:"paths"`
  19. MenuType string `xorm:"comment('菜单标题') VARCHAR(1)" form:"menu_type" json:"menu_type"`
  20. Action string `xorm:"comment('菜单标题') VARCHAR(16)" form:"action" json:"action"`
  21. Permission string `xorm:"comment('菜单标题') VARCHAR(32)" form:"permission" json:"permission"`
  22. ParentId int `xorm:"comment('菜单ID') int(0)" form:"parent_id" json:"parent_id"`
  23. NoCache string `xorm:"comment('菜单标题') VARCHAR(1)" form:"no_cache" json:"no_cache"`
  24. Breadcrumb string `xorm:"comment('菜单标题') VARCHAR(32)" form:"breadcrumb" json:"breadcrumb"`
  25. Component string `xorm:"comment('菜单标题') VARCHAR(32)" form:"component" json:"component"`
  26. Sort int `xorm:"comment('菜单标题') int(4)" form:"sort" json:"sort"`
  27. Visible string `xorm:"comment('菜单标题') VARCHAR(1)" form:"visible" json:"visible"`
  28. CreateBy string `xorm:"comment('菜单标题') VARCHAR(128)" form:"create_by" json:"create_by"`
  29. UpdateBy string `xorm:"comment('菜单标题') VARCHAR(128)" form:"update_by" json:"update_by"`
  30. IsFrame string `xorm:"comment('菜单ID') int(0)" form:"is_frame" json:"is_frame"`
  31. DataScope string `json:"dataScope" xorm:"-"`
  32. Params string `json:"params" xorm:"-"`
  33. RoleId int `xorm:"-" form:"role_id" json:"role_id"`
  34. Children []Menu `json:"children" xorm:"-"`
  35. IsSelect bool `json:"is_select" xorm:"-"`
  36. }
  37. type MenuB struct {
  38. MenuName string `form:"menu_name" json:"menu_name"`
  39. Title string `form:"title" json:"title"`
  40. Icon string `form:"icon" json:"icon"`
  41. Path string `form:"path" json:"path"`
  42. Paths string `form:"paths" json:"paths"`
  43. MenuType string `form:"menu_type" json:"menu_type"`
  44. Action string `form:"action" json:"action"`
  45. Permission string `form:"permission" json:"permission"`
  46. ParentId int `form:"parent_id" json:"parent_id"`
  47. NoCache string `form:"no_cache" json:"no_cache"`
  48. Breadcrumb string `form:"breadcrumb" json:"breadcrumb"`
  49. Component string `form:"component" json:"component"`
  50. Sort int `form:"sort" json:"sort"`
  51. Visible string `form:"visible" json:"visible"`
  52. CreateBy string `form:"create_by" json:"create_by"`
  53. UpdateBy string `form:"update_by" json:"update_by"`
  54. IsFrame string `form:"is_frame" json:"is_frame"`
  55. }
  56. type MenuLable struct {
  57. Id int `json:"id" xorm:"-"`
  58. Label string `json:"label" xorm:"-"`
  59. Children []MenuLable `json:"children" xorm:"-"`
  60. }
  61. type Menus struct {
  62. MenuId int `json:"menu_id" xorm:"column:menu_id"`
  63. MenuName string `json:"menu_name" xorm:"column:menu_name"`
  64. Title string `json:"title" xorm:"column:title"`
  65. Icon string `json:"icon" xorm:"column:icon"`
  66. Path string `json:"path" xorm:"column:path"`
  67. MenuType string `json:"menu_type" xorm:"column:menu_type"`
  68. Action string `json:"action" xorm:"column:action"`
  69. Permission string `json:"permission" xorm:"column:permission"`
  70. ParentId int `json:"parent_id" xorm:"column:parent_id"`
  71. NoCache bool `json:"no_cache" xorm:"column:no_cache"`
  72. Breadcrumb string `json:"breadcrumb" xorm:"column:breadcrumb"`
  73. Component string `json:"component" xorm:"column:component"`
  74. Sort int `json:"sort" xorm:"column:sort"`
  75. Visible string `json:"visible" xorm:"column:visible"`
  76. Children []Menu `json:"children" xorm:"-"`
  77. CreateBy string `json:"create_by" xorm:"column:create_by"`
  78. UpdateBy string `json:"update_by" xorm:"column:update_by"`
  79. DataScope string `json:"dataScope" xorm:"-"`
  80. Params string `json:"params" xorm:"-"`
  81. }
  82. func (t *Menu) TableName() string {
  83. return "menu"
  84. }
  85. type MenuRole struct {
  86. Menus
  87. IsSelect bool `json:"is_select" gorm:"-"`
  88. }
  89. type MS []Menu
  90. // 清除string字段的右侧空格
  91. func (t *Menu) Clipped() {
  92. utils.TrimStruct(t, *t)
  93. }
  94. //根据菜单id,查询菜单
  95. func (t *Menu) GetByMenuId() (Menu Menu, err error) {
  96. e := db.Eloquent.Master()
  97. _, err = e.Table(t.TableName()).Where("menu_id = ? and visible = 0", t.MenuId).Get(&Menu)
  98. if err != nil {
  99. return
  100. }
  101. return
  102. }
  103. func (t *Menu) SetMenu() (m []Menu, err error) {
  104. menulist, err := t.GetPage()
  105. m = make([]Menu, 0)
  106. for i := 0; i < len(menulist); i++ {
  107. menulist[i].Clipped()
  108. if menulist[i].ParentId != 0 {
  109. continue
  110. }
  111. menusInfo := DiguiMenu(&menulist, menulist[i])
  112. m = append(m, menusInfo)
  113. }
  114. return
  115. }
  116. func DiguiMenu(menulist *[]Menu, menu Menu) Menu {
  117. list := *menulist
  118. min := make([]Menu, 0)
  119. for j := 0; j < len(list); j++ {
  120. if menu.MenuId != list[j].ParentId {
  121. continue
  122. }
  123. mi := Menu{}
  124. mi.MenuId = list[j].MenuId
  125. mi.MenuName = list[j].MenuName
  126. mi.Title = list[j].Title
  127. mi.Icon = list[j].Icon
  128. mi.Path = list[j].Path
  129. mi.MenuType = list[j].MenuType
  130. mi.Action = list[j].Action
  131. mi.Permission = list[j].Permission
  132. mi.ParentId = list[j].ParentId
  133. mi.NoCache = list[j].NoCache
  134. mi.Breadcrumb = list[j].Breadcrumb
  135. mi.Component = list[j].Component
  136. mi.Sort = list[j].Sort
  137. mi.Visible = list[j].Visible
  138. mi.Children = []Menu{}
  139. utils.TrimStruct(&mi, mi)
  140. if mi.MenuType != "F" {
  141. ms := DiguiMenu(menulist, mi)
  142. utils.TrimStruct(&ms, ms)
  143. min = append(min, ms)
  144. } else {
  145. min = append(min, mi)
  146. }
  147. }
  148. menu.Children = min
  149. return menu
  150. }
  151. func (t *Menu) SetMenuRole(rolename string) (m []Menu, err error) {
  152. menulist := t.GetByRoleName(rolename)
  153. for i := 0; i < len(menulist); i++ {
  154. if menulist[i].ParentId != 0 {
  155. continue
  156. }
  157. menusInfo := DiguiMenu(&menulist, menulist[i])
  158. utils.TrimStruct(&menusInfo, menusInfo)
  159. m = append(m, menusInfo)
  160. }
  161. return
  162. }
  163. func (t *Menu) GetByRoleName(rolename string) []Menu {
  164. menus := make([]Menu, 0)
  165. e := db.Eloquent.Master()
  166. err := e.SQL("select menu.* from menu left join role_menu on role_menu.menu_id=menu.menu_id where role_menu.role_name=? and menu.menu_type in ('M','C') and menu.visible = 0", rolename).Find(&menus)
  167. if err != nil {
  168. return nil
  169. }
  170. return menus
  171. }
  172. func (t *Menu) SetMenuLable() (m []MenuLable, err error) {
  173. menulist, err := t.Get()
  174. m = make([]MenuLable, 0)
  175. for i := 0; i < len(menulist); i++ {
  176. menulist[i].Clipped()
  177. if menulist[i].ParentId != 0 {
  178. continue
  179. }
  180. e := MenuLable{}
  181. e.Id = menulist[i].MenuId
  182. e.Label = menulist[i].Title
  183. menusInfo := DiguiMenuLable(&menulist, e)
  184. m = append(m, menusInfo)
  185. }
  186. return
  187. }
  188. func DiguiMenuLable(menulist *[]Menu, menu MenuLable) MenuLable {
  189. list := *menulist
  190. min := make([]MenuLable, 0)
  191. for j := 0; j < len(list); j++ {
  192. list[j].Clipped()
  193. if menu.Id != list[j].ParentId {
  194. continue
  195. }
  196. mi := MenuLable{}
  197. mi.Id = list[j].MenuId
  198. mi.Label = list[j].Title
  199. mi.Children = []MenuLable{}
  200. if list[j].MenuType != "F" {
  201. ms := DiguiMenuLable(menulist, mi)
  202. min = append(min, ms)
  203. } else {
  204. min = append(min, mi)
  205. }
  206. }
  207. menu.Children = min
  208. return menu
  209. }
  210. func (t *MenuRole) Get() (Menus []MenuRole, err error) {
  211. e := db.Eloquent.Master()
  212. table := e.Table("menu")
  213. if t.MenuName != "" {
  214. table = table.Where("menu_name = ?", t.MenuName)
  215. }
  216. if err = table.OrderBy("sort").Find(&Menus); err != nil {
  217. return
  218. }
  219. return
  220. }
  221. func (t *Menu) Get() (Menus []Menu, err error) {
  222. e := db.Eloquent.Master()
  223. table := e.Table(t.TableName()).Where("1=1").And("menu.visible = ?",0)
  224. if t.MenuName != "" {
  225. table = table.And("menu_name = ?", t.MenuName)
  226. }
  227. if t.Path != "" {
  228. table = table.And("path = ?", t.Path)
  229. }
  230. if t.Action != "" {
  231. table = table.And("action = ?", t.Action)
  232. }
  233. if t.MenuType != "" {
  234. table = table.And("menu_type = ?", t.MenuType)
  235. }
  236. if err = table.OrderBy("sort").Find(&Menus); err != nil {
  237. return
  238. }
  239. return
  240. }
  241. func (t *Menu) GetPage() (Menus []Menu, err error) {
  242. e := db.Eloquent.Master()
  243. table := e.Table(t.TableName()).Where("1=1").And("visible = ?", 0)
  244. if t.MenuName != "" {
  245. table = table.And("menu_name = ?", t.MenuName)
  246. }
  247. if t.Title != "" {
  248. table = table.And("title = ?", t.Title)
  249. }
  250. table = table.And("menu_type in ('M','C','F')")
  251. if err = table.OrderBy("sort").Find(&Menus); err != nil {
  252. return
  253. }
  254. return
  255. }
  256. func (t *Menu) Create() (id int, err error) {
  257. e := db.Eloquent.Master()
  258. data := MenuB{}
  259. data.Sort = t.Sort
  260. data.Visible = t.Visible
  261. data.Breadcrumb = t.Breadcrumb
  262. data.Action = t.Action
  263. data.NoCache = t.NoCache
  264. data.Component = t.Component
  265. data.UpdateBy = t.UpdateBy
  266. data.CreateBy = t.CreateBy
  267. data.Icon = t.Icon
  268. data.MenuName = t.MenuName
  269. data.MenuType = t.MenuType
  270. data.Path = t.Path
  271. data.Permission = t.Permission
  272. data.ParentId = t.ParentId
  273. data.Title = t.Title
  274. _, err = e.Table(t.TableName()).Insert(data)
  275. if err != nil {
  276. return
  277. }
  278. //err = InitPaths(t)
  279. //if err != nil {
  280. // return
  281. //}
  282. id = t.MenuId
  283. return
  284. }
  285. func InitPaths(menu *Menu) (err error) {
  286. e := db.Eloquent.Master()
  287. parentMenu := new(Menu)
  288. if int(menu.ParentId) != 0 {
  289. e.Table("menu").Where("menu_id = ?", menu.ParentId).Get(parentMenu)
  290. if parentMenu.Paths == "" {
  291. err = errors.New("父级paths异常,请尝试对当前节点父级菜单进行更新操作!")
  292. return
  293. }
  294. menu.Paths = parentMenu.Paths + "/" + utils.ValueToString(menu.MenuId, "")
  295. } else {
  296. menu.Paths = "/0/" + utils.ValueToString(menu.MenuId, "")
  297. }
  298. e.Table("menu").Where("menu_id = ?", menu.MenuId).Update("paths", menu.Paths)
  299. return
  300. }
  301. func (t *Menu) Update(id int) (err error) {
  302. e := db.Eloquent.Master()
  303. var upmenu Menu
  304. upmenu.Permission = t.Permission
  305. upmenu.ParentId = t.ParentId
  306. upmenu.Title = t.Title
  307. upmenu.Paths = t.Paths
  308. upmenu.Path = t.Path
  309. upmenu.MenuType = t.MenuType
  310. upmenu.MenuName = t.MenuName
  311. upmenu.IsFrame = t.IsFrame
  312. upmenu.Icon = t.Icon
  313. upmenu.CreateBy = t.CreateBy
  314. upmenu.UpdateBy = t.UpdateBy
  315. upmenu.Component = t.Component
  316. upmenu.Action = t.Action
  317. upmenu.Breadcrumb = t.Breadcrumb
  318. upmenu.Visible = t.Visible
  319. upmenu.Sort = t.Sort
  320. //参数1:是要修改的数据
  321. columns := []string{"permission", "parent_id", "title",
  322. "paths", "path", "menu_type", "menu_name", "is_frame", "icon", "breadcrumb", "visible", "sort",
  323. "create_by", "update_by", "component", "action"}
  324. //参数2:是修改的数据
  325. if _, err = e.Table(t.TableName()).ID(id).MustCols(columns...).Update(&upmenu); err != nil {
  326. return
  327. }
  328. //err = InitPaths(t)
  329. //if err != nil {
  330. // return
  331. //}
  332. return
  333. }
  334. func (t *Menu) Delete(id int) (success bool, err error) {
  335. e := db.Eloquent.Master()
  336. if _, err = e.Table(t.TableName()).Where("menu_id = ?", id).Delete(&Menu{}); err != nil {
  337. success = false
  338. return
  339. }
  340. success = true
  341. return
  342. }