Menu.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. "github.com/beego/beego/v2/core/logs"
  12. "strconv"
  13. "time"
  14. )
  15. const (
  16. // Menu 菜单
  17. MenuType string = "M"
  18. // Button 按钮
  19. ButtonType string = "B"
  20. )
  21. type Menu struct {
  22. Id int `orm:"column(ID);size(11);auto;pk"`
  23. T_mid int `orm:"size(200);null"` // 上一级 ID
  24. T_name string `orm:"size(256);null"` // 菜单标题
  25. T_permission string `orm:"size(256);null"` // 权限表示
  26. T_icon string `orm:"size(256);null"` // 图标
  27. T_type string `orm:"size(2);null"` // 菜单类型
  28. T_sort int `orm:"size(11);default(0)"` // 排序 越小的越靠前,可以为负数
  29. T_pid int `orm:"size(2);default(0)"` // 1 进入公司后才能访问的菜单 0 公共菜单
  30. T_State int `orm:"size(2);default(1)"` // 0删除 1正常 2共有菜单
  31. Children []Menu `orm:"-"`
  32. }
  33. type Menu_Permission struct {
  34. Menu []Menu // 菜单树形结构
  35. Permission []string // 菜单权限标识
  36. MenuPermission []string // 菜单权限标识
  37. }
  38. func (t *Menu) TableName() string {
  39. return "menu" // 数据库名称 // ************** 替换 FormulaList **************
  40. }
  41. type API struct {
  42. Id int `orm:"column(ID);size(11);auto;pk"`
  43. T_Menu_Id int `orm:"size(11);null"` // 所属菜单id 0 第一级菜单
  44. T_name string `orm:"size(256);null"` // 接口名称
  45. T_uri string `orm:"size(256);null"` // 接口路径
  46. T_method string `orm:"size(256);null"` // http请求类型 GET,POST,PUT,DELETE 等
  47. T_enable int `orm:"size(2);default(1)"` // 是否启用 0-禁用 1-启用
  48. }
  49. func (t *API) TableName() string {
  50. return "api" // 数据库名称 // ************** 替换 FormulaList **************
  51. }
  52. // var redisCache_Menu cache.Cache
  53. var redisCache_API cache.Cache
  54. var redisCache_Menu cache.Cache
  55. func init() {
  56. //注册模型
  57. orm.RegisterModel(new(Menu))
  58. orm.RegisterModel(new(API))
  59. var err error
  60. menuConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  61. "redis_User_Menu", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  62. fmt.Println(menuConfig)
  63. redisCache_Menu, err = cache.NewCache("redis", menuConfig)
  64. if err != nil || redisCache_Menu == nil {
  65. errMsg := "failed to init redis menu"
  66. logs.Error(errMsg, err)
  67. panic(errMsg)
  68. }
  69. apiConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  70. "redis_User_API", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  71. fmt.Println(apiConfig)
  72. redisCache_API, err = cache.NewCache("redis", apiConfig)
  73. if err != nil || redisCache_API == nil {
  74. errMsg := "failed to init redis api"
  75. logs.Error(errMsg, err)
  76. panic(errMsg)
  77. }
  78. }
  79. // ---------------- Redis -------------------
  80. func Redis_API_Set(Power_Id int, r []API) (err error) {
  81. //json序列化
  82. str, err := json.Marshal(r)
  83. if err != nil {
  84. logs.Error(lib.FuncName(), err)
  85. return
  86. }
  87. err = redisCache_API.Put(strconv.Itoa(Power_Id), str, 24*time.Hour)
  88. if err != nil {
  89. logs.Error("Redis_API_Set", "set key:", strconv.Itoa(Power_Id), ",value:", str, err)
  90. }
  91. return
  92. }
  93. func Redis_API_Get(key string) (r []API, is bool) {
  94. if redisCache_API.IsExist(key) {
  95. //println("找到key:",key)
  96. v := redisCache_API.Get(key)
  97. err := json.Unmarshal(v.([]byte), &r)
  98. if err != nil {
  99. logs.Error(lib.FuncName(), err)
  100. return []API{}, false
  101. }
  102. return r, true
  103. }
  104. //println("没有 找到key:",key)
  105. return []API{}, false
  106. }
  107. func Redis_API_DelK(key string) (err error) {
  108. err = redisCache_API.Delete(key)
  109. if err != nil {
  110. logs.Error(lib.FuncName(), err)
  111. }
  112. return
  113. }
  114. func Redis_Munu_Set(Power_Id, T_pid int, r Menu_Permission) (err error) {
  115. //json序列化
  116. str, err := json.Marshal(r)
  117. if err != nil {
  118. logs.Error(err)
  119. return
  120. }
  121. keys := strconv.Itoa(Power_Id) + "-Pid"
  122. if T_pid == 0 {
  123. keys = strconv.Itoa(Power_Id)
  124. }
  125. err = redisCache_Menu.Put(keys, str, 30*24*time.Hour)
  126. if err != nil {
  127. logs.Error("set key:", strconv.Itoa(Power_Id)+"|"+strconv.Itoa(T_pid), ",value:", str, err)
  128. }
  129. return
  130. }
  131. func Redis_Menu_Get(Power_Id, T_pid int) (r Menu_Permission, is bool) {
  132. key := strconv.Itoa(Power_Id) + "-Pid"
  133. if T_pid == 0 {
  134. key = strconv.Itoa(Power_Id)
  135. }
  136. if redisCache_Menu.IsExist(key) {
  137. //println("找到key:",key)
  138. v := redisCache_Menu.Get(key)
  139. err := json.Unmarshal(v.([]byte), &r)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. return r, false
  143. }
  144. return r, true
  145. }
  146. return r, false
  147. }
  148. func Redis_Menu_DelK(Power_Id int) (err error) {
  149. err = redisCache_Menu.Delete(strconv.Itoa(Power_Id))
  150. if err != nil {
  151. logs.Error(lib.FuncName(), err)
  152. return
  153. }
  154. err = redisCache_Menu.Delete(strconv.Itoa(Power_Id) + "-Pid")
  155. if err != nil {
  156. logs.Error(lib.FuncName(), err)
  157. }
  158. return
  159. }
  160. // ---------------- 特殊方法 -------------------
  161. // 添加
  162. func Add_Menu(m Menu) (id int64, err error) {
  163. o := orm.NewOrm()
  164. // 插入菜单信息
  165. id, err = o.Insert(&m)
  166. if err != nil {
  167. logs.Error(err)
  168. }
  169. return id, err
  170. }
  171. // 修改
  172. func Update_Menu(m Menu, cols ...string) bool {
  173. o := orm.NewOrm()
  174. if num, err := o.Update(&m, cols...); err == nil {
  175. logs.Info("Number of records updated in database:", num)
  176. return true
  177. }
  178. return false
  179. }
  180. // 删除
  181. func Delete_Menu(m Menu) bool {
  182. o := orm.NewOrm()
  183. if num, err := o.Delete(&m); err == nil {
  184. logs.Info("Number of records updated in database:", num)
  185. } else {
  186. return false
  187. }
  188. return true
  189. }
  190. // 获取 ById
  191. func Read_Menu_ById(Id int) (r Menu, e error) {
  192. o := orm.NewOrm()
  193. qs := o.QueryTable(new(Menu))
  194. e = qs.Filter("Id", Id).One(&r)
  195. return r, e
  196. }
  197. // 获取列表
  198. func Read_Menu_Bind_List(Power_Id int, Bind_Menu string, T_pid int) (r Menu_Permission) {
  199. if len(Bind_Menu) == 0 {
  200. return r
  201. }
  202. if r, is := Redis_Menu_Get(Power_Id, T_pid); is {
  203. return r
  204. }
  205. o := orm.NewOrm()
  206. // 也可以直接使用 Model 结构体作为表名
  207. var maps []Menu
  208. qs := o.QueryTable(new(Menu))
  209. cond := orm.NewCondition()
  210. cond = cond.And("T_State__gt", 0)
  211. if T_pid == 0 {
  212. cond = cond.And("T_pid", 0)
  213. }
  214. if Bind_Menu == "*" {
  215. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps)
  216. if err != nil {
  217. logs.Error(lib.FuncName(), err)
  218. return
  219. }
  220. m := Menu_Tree(maps, 0)
  221. r.Menu = m
  222. r.Permission = Get_Permission(maps)
  223. r.MenuPermission = Get_MenuPermission(maps)
  224. Redis_Munu_Set(Power_Id, T_pid, r)
  225. return r
  226. }
  227. list := lib.SplitStringToIntIds(Bind_Menu, "M")
  228. AllIds := list
  229. if err := recursiveMenu(T_pid, list, &AllIds); err != nil {
  230. return
  231. }
  232. cond = cond.And("Id__in", lib.IntIdsDistinct(AllIds)).Or("T_State", 2)
  233. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps)
  234. if err != nil {
  235. logs.Error(lib.FuncName(), err)
  236. return
  237. }
  238. m := Menu_Tree(maps, 0)
  239. r.Menu = m
  240. r.Permission = Get_Permission(maps)
  241. r.MenuPermission = Get_MenuPermission(maps)
  242. Redis_Munu_Set(Power_Id, T_pid, r)
  243. return r
  244. }
  245. func Get_Permission(menu []Menu) (permission []string) {
  246. for _, v := range menu {
  247. permission = append(permission, v.T_permission)
  248. }
  249. return
  250. }
  251. func Get_MenuPermission(menu []Menu) (menuPermission []string) {
  252. for _, v := range menu {
  253. if v.T_type == MenuType {
  254. menuPermission = append(menuPermission, v.T_permission)
  255. }
  256. }
  257. return
  258. }
  259. // 获取MenuIds下父id
  260. func recursiveMenu(T_pid int, MenuIds []int, AllIds *[]int) error {
  261. if len(MenuIds) == 0 {
  262. return nil
  263. }
  264. var subMenus []Menu
  265. o := orm.NewOrm()
  266. qs := o.QueryTable(new(Menu))
  267. cond := orm.NewCondition()
  268. if T_pid == 0 {
  269. cond = cond.And("T_pid", 0)
  270. }
  271. cond = cond.And("Id__in", MenuIds)
  272. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&subMenus)
  273. if err != nil {
  274. logs.Error(lib.FuncName(), err)
  275. return err
  276. }
  277. subIds := make([]int, 0)
  278. for _, menu := range subMenus {
  279. if menu.T_mid != 0 {
  280. subIds = append(subIds, menu.T_mid)
  281. *AllIds = append(*AllIds, menu.T_mid)
  282. }
  283. }
  284. return recursiveMenu(T_pid, subIds, AllIds)
  285. }
  286. func Read_Menu_List_All() (maps []Menu, menu_ids []int) {
  287. o := orm.NewOrm()
  288. // 也可以直接使用 Model 结构体作为表名
  289. qs := o.QueryTable(new(Menu))
  290. _, err := qs.Filter("T_State", 1).OrderBy("T_sort", "Id").All(&maps)
  291. if err != nil {
  292. logs.Error(lib.FuncName(), err)
  293. return
  294. }
  295. for _, v := range maps {
  296. menu_ids = append(menu_ids, v.Id)
  297. }
  298. m := Menu_Call(maps, 0)
  299. return m, menu_ids
  300. }
  301. func Read_Menu_All() (maps []Menu) {
  302. o := orm.NewOrm()
  303. // 也可以直接使用 Model 结构体作为表名
  304. qs := o.QueryTable(new(Menu))
  305. _, err := qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
  306. if err != nil {
  307. logs.Error(lib.FuncName(), err)
  308. return
  309. }
  310. return maps
  311. }
  312. // Menu_Call 构建菜单树
  313. // [{"Id":70,"T_mid":0,"T_name":"工单管理","T_permission":"workorder","T_sort":0,"T_pid":1,"T_bind":false,"Children":[{"Id":71,"T_mid":70,"T_name":"工单列表","T_permission":"workorder:list","T_sort":0,"T_pid":1,"T_bind":false,"Children":[]}]}]
  314. func Menu_Call(list []Menu, parentId int) []Menu {
  315. res := make([]Menu, 0)
  316. for _, v := range list {
  317. if v.T_mid == parentId {
  318. v.Children = Menu_Call(list, v.Id)
  319. res = append(res, v)
  320. }
  321. }
  322. return res
  323. }
  324. func Menu_Tree(list []Menu, parentId int) []Menu {
  325. res := make([]Menu, 0)
  326. for _, v := range list {
  327. if v.T_type != MenuType {
  328. continue
  329. }
  330. if v.T_name == "流量池" {
  331. continue
  332. }
  333. if v.T_mid == parentId {
  334. v.Children = Menu_Tree(list, v.Id)
  335. res = append(res, v)
  336. }
  337. }
  338. return res
  339. }
  340. // 添加
  341. func Add_API(m API) (id int64, err error) {
  342. o := orm.NewOrm()
  343. id, err = o.Insert(&m)
  344. if err != nil {
  345. logs.Error(err)
  346. }
  347. m.Id = int(id)
  348. return id, err
  349. }
  350. // 批量添加
  351. func Add_Multi_API(m []API) (id int64, err error) {
  352. o := orm.NewOrm()
  353. id, err = o.InsertMulti(len(m), &m)
  354. if err != nil {
  355. logs.Error(err)
  356. }
  357. return id, err
  358. }
  359. // 修改
  360. func Update_API(m API, cols ...string) bool {
  361. o := orm.NewOrm()
  362. if num, err := o.Update(&m, cols...); err == nil {
  363. logs.Info("Number of records updated in database:", num)
  364. return true
  365. }
  366. return false
  367. }
  368. // 删除
  369. func Delete_API(m API) bool {
  370. o := orm.NewOrm()
  371. if num, err := o.Delete(&m); err == nil {
  372. logs.Info("Number of records updated in database:", num)
  373. } else {
  374. return false
  375. }
  376. return true
  377. }
  378. // 批量删除
  379. func Delete_Multi_API(m []API) bool {
  380. o := orm.NewOrm()
  381. if num, err := o.Delete(&m); err == nil {
  382. logs.Info("Number of records updated in database:", num)
  383. } else {
  384. return false
  385. }
  386. return true
  387. }
  388. // 获取 ById
  389. func Read_API_ById(Id int) (r API, e error) {
  390. o := orm.NewOrm()
  391. qs := o.QueryTable(new(API))
  392. e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
  393. return r, e
  394. }
  395. func Read_API_List_ByMenuId(MenuId int) (r []API) {
  396. o := orm.NewOrm()
  397. qs := o.QueryTable(new(API))
  398. qs.Filter("T_Menu_Id", MenuId).All(&r)
  399. return r
  400. }
  401. // 获取列表
  402. func Read_API_List_ByPower_Id(Power_Id int, Menu_Bind string) (maps []API) {
  403. if len(Menu_Bind) == 0 {
  404. return
  405. }
  406. if r, is := Redis_API_Get(strconv.Itoa(Power_Id)); is {
  407. return r
  408. }
  409. o := orm.NewOrm()
  410. // 也可以直接使用 Model 结构体作为表名
  411. qs := o.QueryTable(new(API))
  412. list := lib.SplitStringToIntIds(Menu_Bind, "M")
  413. AllIds := list
  414. if err := recursiveMenu(1, list, &AllIds); err != nil {
  415. return
  416. }
  417. _, err := qs.Filter("T_Menu_Id__in", lib.IntIdsDistinct(AllIds)).All(&maps)
  418. if err != nil {
  419. logs.Error(lib.FuncName(), err)
  420. return
  421. }
  422. Redis_API_Set(Power_Id, maps)
  423. return maps
  424. }
  425. func Read_API_List_By_Menu_Bind(Menu_Bind string) (maps []API) {
  426. if len(Menu_Bind) == 0 {
  427. return
  428. }
  429. o := orm.NewOrm()
  430. // 也可以直接使用 Model 结构体作为表名
  431. qs := o.QueryTable(new(API))
  432. list := lib.SplitStringIds(Menu_Bind, "M")
  433. _, err := qs.Filter("T_Menu_Id__in", list).All(&maps)
  434. if err != nil {
  435. logs.Error(lib.FuncName(), err)
  436. }
  437. return maps
  438. }