123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- package Account
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- "github.com/beego/beego/v2/core/logs"
- "strconv"
- "time"
- )
- const (
- // Menu 菜单
- MenuType string = "M"
- // Button 按钮
- ButtonType string = "B"
- )
- type Menu struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_mid int `orm:"size(200);null"` // 上一级 ID
- T_name string `orm:"size(256);null"` // 菜单标题
- T_permission string `orm:"size(256);null"` // 权限表示
- T_icon string `orm:"size(256);null"` // 图标
- T_type string `orm:"size(2);null"` // 菜单类型
- T_sort int `orm:"size(11);default(0)"` // 排序 越小的越靠前,可以为负数
- T_pid int `orm:"size(2);default(0)"` // 1 进入公司后才能访问的菜单 0 公共菜单
- T_State int `orm:"size(2);default(1)"` // 0删除 1正常 2共有菜单
- Children []Menu `orm:"-"`
- }
- type Menu_Permission struct {
- Menu []Menu // 菜单树形结构
- Permission []string // 菜单权限标识
- MenuPermission []string // 菜单权限标识
- }
- func (t *Menu) TableName() string {
- return "menu" // 数据库名称 // ************** 替换 FormulaList **************
- }
- type API struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_Menu_Id int `orm:"size(11);null"` // 所属菜单id 0 第一级菜单
- T_name string `orm:"size(256);null"` // 接口名称
- T_uri string `orm:"size(256);null"` // 接口路径
- T_method string `orm:"size(256);null"` // http请求类型 GET,POST,PUT,DELETE 等
- T_enable int `orm:"size(2);default(1)"` // 是否启用 0-禁用 1-启用
- }
- func (t *API) TableName() string {
- return "api" // 数据库名称 // ************** 替换 FormulaList **************
- }
- // var redisCache_Menu cache.Cache
- var redisCache_API cache.Cache
- var redisCache_Menu cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(Menu))
- orm.RegisterModel(new(API))
- var err error
- menuConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_User_Menu", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(menuConfig)
- redisCache_Menu, err = cache.NewCache("redis", menuConfig)
- if err != nil || redisCache_Menu == nil {
- errMsg := "failed to init redis menu"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- apiConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_User_API", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(apiConfig)
- redisCache_API, err = cache.NewCache("redis", apiConfig)
- if err != nil || redisCache_API == nil {
- errMsg := "failed to init redis api"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis -------------------
- func Redis_API_Set(Power_Id int, r []API) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_API.Put(strconv.Itoa(Power_Id), str, 24*time.Hour)
- if err != nil {
- logs.Error("Redis_API_Set", "set key:", strconv.Itoa(Power_Id), ",value:", str, err)
- }
- return
- }
- func Redis_API_Get(key string) (r []API, is bool) {
- if redisCache_API.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_API.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return []API{}, false
- }
- return r, true
- }
- //println("没有 找到key:",key)
- return []API{}, false
- }
- func Redis_API_DelK(key string) (err error) {
- err = redisCache_API.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- func Redis_Munu_Set(Power_Id, T_pid int, r Menu_Permission) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(err)
- return
- }
- keys := strconv.Itoa(Power_Id) + "-Pid"
- if T_pid == 0 {
- keys = strconv.Itoa(Power_Id)
- }
- err = redisCache_Menu.Put(keys, str, 30*24*time.Hour)
- if err != nil {
- logs.Error("set key:", strconv.Itoa(Power_Id)+"|"+strconv.Itoa(T_pid), ",value:", str, err)
- }
- return
- }
- func Redis_Menu_Get(Power_Id, T_pid int) (r Menu_Permission, is bool) {
- key := strconv.Itoa(Power_Id) + "-Pid"
- if T_pid == 0 {
- key = strconv.Itoa(Power_Id)
- }
- if redisCache_Menu.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Menu.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- return r, false
- }
- func Redis_Menu_DelK(Power_Id int) (err error) {
- err = redisCache_Menu.Delete(strconv.Itoa(Power_Id))
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_Menu.Delete(strconv.Itoa(Power_Id) + "-Pid")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- // 添加
- func Add_Menu(m Menu) (id int64, err error) {
- o := orm.NewOrm()
- // 插入菜单信息
- id, err = o.Insert(&m)
- if err != nil {
- logs.Error(err)
- }
- return id, err
- }
- // 修改
- func Update_Menu(m Menu, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Info("Number of records updated in database:", num)
- return true
- }
- return false
- }
- // 删除
- func Delete_Menu(m Menu) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&m); err == nil {
- logs.Info("Number of records updated in database:", num)
- } else {
- return false
- }
- return true
- }
- // 获取 ById
- func Read_Menu_ById(Id int) (r Menu, e error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Menu))
- e = qs.Filter("Id", Id).One(&r)
- return r, e
- }
- // 获取列表
- func Read_Menu_Bind_List(Power_Id int, Bind_Menu string, T_pid int) (r Menu_Permission) {
- if len(Bind_Menu) == 0 {
- return r
- }
- if r, is := Redis_Menu_Get(Power_Id, T_pid); is {
- return r
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var maps []Menu
- qs := o.QueryTable(new(Menu))
- cond := orm.NewCondition()
- cond = cond.And("T_State__gt", 0)
- if T_pid == 0 {
- cond = cond.And("T_pid", 0)
- }
- if Bind_Menu == "*" {
- _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- m := Menu_Tree(maps, 0)
- r.Menu = m
- r.Permission = Get_Permission(maps)
- r.MenuPermission = Get_MenuPermission(maps)
- Redis_Munu_Set(Power_Id, T_pid, r)
- return r
- }
- list := lib.SplitStringToIntIds(Bind_Menu, "M")
- AllIds := list
- if err := recursiveMenu(T_pid, list, &AllIds); err != nil {
- return
- }
- cond = cond.And("Id__in", lib.IntIdsDistinct(AllIds)).Or("T_State", 2)
- _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- m := Menu_Tree(maps, 0)
- r.Menu = m
- r.Permission = Get_Permission(maps)
- r.MenuPermission = Get_MenuPermission(maps)
- Redis_Munu_Set(Power_Id, T_pid, r)
- return r
- }
- func Get_Permission(menu []Menu) (permission []string) {
- for _, v := range menu {
- permission = append(permission, v.T_permission)
- }
- return
- }
- func Get_MenuPermission(menu []Menu) (menuPermission []string) {
- for _, v := range menu {
- if v.T_type == MenuType {
- menuPermission = append(menuPermission, v.T_permission)
- }
- }
- return
- }
- // 获取MenuIds下父id
- func recursiveMenu(T_pid int, MenuIds []int, AllIds *[]int) error {
- if len(MenuIds) == 0 {
- return nil
- }
- var subMenus []Menu
- o := orm.NewOrm()
- qs := o.QueryTable(new(Menu))
- cond := orm.NewCondition()
- if T_pid == 0 {
- cond = cond.And("T_pid", 0)
- }
- cond = cond.And("Id__in", MenuIds)
- _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&subMenus)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- subIds := make([]int, 0)
- for _, menu := range subMenus {
- if menu.T_mid != 0 {
- subIds = append(subIds, menu.T_mid)
- *AllIds = append(*AllIds, menu.T_mid)
- }
- }
- return recursiveMenu(T_pid, subIds, AllIds)
- }
- func Read_Menu_List_All() (maps []Menu, menu_ids []int) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Menu))
- _, err := qs.Filter("T_State", 1).OrderBy("T_sort", "Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range maps {
- menu_ids = append(menu_ids, v.Id)
- }
- m := Menu_Call(maps, 0)
- return m, menu_ids
- }
- func Read_Menu_All() (maps []Menu) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Menu))
- _, err := qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return maps
- }
- // Menu_Call 构建菜单树
- // [{"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":[]}]}]
- func Menu_Call(list []Menu, parentId int) []Menu {
- res := make([]Menu, 0)
- for _, v := range list {
- if v.T_mid == parentId {
- v.Children = Menu_Call(list, v.Id)
- res = append(res, v)
- }
- }
- return res
- }
- func Menu_Tree(list []Menu, parentId int) []Menu {
- res := make([]Menu, 0)
- for _, v := range list {
- if v.T_type != MenuType {
- continue
- }
- if v.T_name == "流量池" {
- continue
- }
- if v.T_mid == parentId {
- v.Children = Menu_Tree(list, v.Id)
- res = append(res, v)
- }
- }
- return res
- }
- // 添加
- func Add_API(m API) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- logs.Error(err)
- }
- m.Id = int(id)
- return id, err
- }
- // 批量添加
- func Add_Multi_API(m []API) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.InsertMulti(len(m), &m)
- if err != nil {
- logs.Error(err)
- }
- return id, err
- }
- // 修改
- func Update_API(m API, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Info("Number of records updated in database:", num)
- return true
- }
- return false
- }
- // 删除
- func Delete_API(m API) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&m); err == nil {
- logs.Info("Number of records updated in database:", num)
- } else {
- return false
- }
- return true
- }
- // 批量删除
- func Delete_Multi_API(m []API) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&m); err == nil {
- logs.Info("Number of records updated in database:", num)
- } else {
- return false
- }
- return true
- }
- // 获取 ById
- func Read_API_ById(Id int) (r API, e error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(API))
- e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
- return r, e
- }
- func Read_API_List_ByMenuId(MenuId int) (r []API) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(API))
- qs.Filter("T_Menu_Id", MenuId).All(&r)
- return r
- }
- // 获取列表
- func Read_API_List_ByPower_Id(Power_Id int, Menu_Bind string) (maps []API) {
- if len(Menu_Bind) == 0 {
- return
- }
- if r, is := Redis_API_Get(strconv.Itoa(Power_Id)); is {
- return r
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(API))
- list := lib.SplitStringToIntIds(Menu_Bind, "M")
- AllIds := list
- if err := recursiveMenu(1, list, &AllIds); err != nil {
- return
- }
- _, err := qs.Filter("T_Menu_Id__in", lib.IntIdsDistinct(AllIds)).All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- Redis_API_Set(Power_Id, maps)
- return maps
- }
- func Read_API_List_By_Menu_Bind(Menu_Bind string) (maps []API) {
- if len(Menu_Bind) == 0 {
- return
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(API))
- list := lib.SplitStringIds(Menu_Bind, "M")
- _, err := qs.Filter("T_Menu_Id__in", list).All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return maps
- }
|