package controllers import ( "ERP_user/Nats/NatsServer" "ERP_user/conf" "ERP_user/models/Account" "ERP_user/models/System" "fmt" "github.com/astaxie/beego/logs" "github.com/beego/beego/v2/adapter/orm" beego "github.com/beego/beego/v2/server/web" menulibs "gogs.baozhida.cn/zoie/ERP_libs/Menu" powerlibs "gogs.baozhida.cn/zoie/ERP_libs/Power" "gogs.baozhida.cn/zoie/ERP_libs/lib" "math" "strings" ) type PowerController struct { beego.Controller User Account.User } func (c *PowerController) Prepare() { c.User = *Account.User_r } // 列表 - func (c *PowerController) List() { // 分页参数 初始化 page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } // 查询 T_name := c.GetString("T_name") R_List, R_cnt := Account.Read_Power_List(T_name, page, page_z) var r_jsons lib.R_JSONS r_jsons.Num = R_cnt r_jsons.Data = R_List r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *PowerController) Get() { type Data struct { Power powerlibs.Power_R Menu []menulibs.Menu Menu_checked []int } var data Data T_id := c.GetString("T_id") T_code := c.GetString("T_code") if len(T_code) == 0 { T_code = conf.ERP_ACCOUNT_Sys } if len(T_id) < 1 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } sysName := Account.Get_Sys_Name(T_code) var menu []menulibs.Menu var power powerlibs.Power var err error power, err = Account.Read_Power_ByT_id(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } switch T_code { case conf.ERP_ACCOUNT_Sys: menu, err = Account.Read_Menu_List() if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取用户系统菜单失败!"} c.ServeJSON() return } default: power, err = NatsServer.ERP_Read_Power_ByT_id(T_code, T_id) if err != nil { if err.Error() == orm.ErrNoRows.Error() { var_ := powerlibs.Power{ T_id: power.T_id, T_name: power.T_name, } _, err = NatsServer.ERP_Add_Power(T_code, var_) if err != nil { logs.Error(lib.FuncName(), err) } } else { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("获取%s权限失败!", sysName)} c.ServeJSON() return } } menu, err = NatsServer.ERP_Read_Menu_List(T_code) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("获取%s菜单失败!", sysName)} c.ServeJSON() return } } data.Power = powerlibs.PowerToPower_R(power) data.Menu = menu if power.T_menu != "*" { data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M") } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data} c.ServeJSON() return } func (c *PowerController) Add() { T_name := c.GetString("T_name") rand_x := 0 T_id := "" for true { T_id = lib.GetRandstring(8, "", int64(rand_x)) _, err := Account.Read_Power_ByT_id(T_id) if err != nil { break } rand_x += 1 } var_ := powerlibs.Power{ T_id: T_id, T_name: T_name, } id, err := Account.Add_Power(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"} c.ServeJSON() return } for _, prefix := range Account.ERP_Sys_prefix { i := 3 for { // Nats 发送失败 重试3次 _, err = NatsServer.ERP_Add_Power(prefix, var_) if err == nil || i == 0 { break } i-- } if err != nil { sysName := Account.Get_Sys_Name(prefix) logs.Error(lib.FuncName(), sysName+"添加失败: ", err) c.Data["json"] = lib.JSONS{Code: 202, Msg: sysName + "添加失败!"} c.ServeJSON() return } } System.Add_UserLogs_T(c.User.T_uuid, "权限", "新增", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return } func (c *PowerController) Edit() { T_id := c.GetString("T_id") T_name := c.GetString("T_name") if len(T_id) == 0 { c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_id Err!"} c.ServeJSON() return } userPower, err := Account.Read_Power_ByT_id(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } userPower.T_name = T_name id, err := Account.Update_Power(userPower) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } for _, prefix := range Account.ERP_Sys_prefix { go func(sys string) { // Nats 发送失败 重试10次 var power powerlibs.Power i := 10 for { power, err = NatsServer.ERP_Read_Power_ByT_id(sys, T_id) if err != nil { if err.Error() == orm.ErrNoRows.Error() { var_ := powerlibs.Power{ T_id: userPower.T_id, T_name: T_name, } _, err = NatsServer.ERP_Add_Power(sys, var_) if err == nil || i == 0 { break } } else { continue } } if power.Id > 0 { power.T_name = T_name _, err = NatsServer.ERP_Update_Power(sys, power) if err == nil || i == 0 { break } } i-- } }(prefix) } System.Add_UserLogs_T(c.User.T_uuid, "权限", "修改", userPower) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return } func (c *PowerController) Sys_List() { type Sys struct { T_sys string T_name string } var syslist []Sys list := strings.Split(conf.ERP_Sys, ",") for _, v := range list { sys, name := strings.Split(v, "|")[0], strings.Split(v, "|")[1] syslist = append(syslist, Sys{ T_sys: sys, T_name: name, }) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: syslist} c.ServeJSON() return } func (c *PowerController) Edit_Menu() { T_id := c.GetString("T_id") T_code := c.GetString("T_code") T_menu := c.GetString("T_menu") if len(T_id) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } sysName := Account.Get_Sys_Name(T_code) if sysName == T_code { // 不存在这个code c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_code Err!"} c.ServeJSON() return } power, err := Account.Read_Power_ByT_id(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } var id int switch T_code { case conf.ERP_ACCOUNT_Sys: power.T_menu = T_menu id, err = Account.Update_Power(power) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } default: power, err = NatsServer.ERP_Read_Power_ByT_id(T_code, T_id) if err != nil { if err.Error() == orm.ErrNoRows.Error() { var_ := powerlibs.Power{ T_id: power.T_id, T_name: power.T_name, T_menu: T_menu, } _, err = NatsServer.ERP_Add_Power(T_code, var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s 修改失败!", sysName)} c.ServeJSON() return } } else { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } } if power.Id > 0 { power.T_menu = T_menu id, err = NatsServer.ERP_Update_Power(T_code, power) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s 修改失败!", sysName)} c.ServeJSON() return } } } Account.Redis_Menu_DelK(T_id) System.Add_UserLogs_T(c.User.T_uuid, "权限", sysName+" 修改菜单", power) c.Data["json"] = lib.JSONS{Code: 200, Msg: sysName + " ok!", Data: id} c.ServeJSON() return } func (c *PowerController) Del() { T_id := c.GetString("T_id") if len(T_id) == 0 { c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_id Err!"} c.ServeJSON() return } R, err := Account.Read_Power_ByT_id(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if R.Id == 1 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除系统角色!"} c.ServeJSON() return } id, err := Account.Delete_Power(R) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } //for _, prefix := range Account.ERP_Sys_prefix { // i := 3 // for { // R, err = NatsServer.ERP_Read_Power_ByT_id(prefix, T_id) // if err != nil { // if err.Error() == orm.ErrNoRows.Error() { // break // } // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} // c.ServeJSON() // return // } // // Nats 发送失败 重试3次 // _, err = NatsServer.ERP_Delete_Power(prefix, R) // if err == nil || i == 0 { // break // } // i-- // } // // if err != nil { // name := Account.Get_Sys_Name(prefix) // c.Data["json"] = lib.JSONS{Code: 202, Msg: name + " 修改失败!"} // c.ServeJSON() // return // } //} for _, prefix := range Account.ERP_Sys_prefix { go func(sys string) { var power powerlibs.Power i := 10 for { power, err = NatsServer.ERP_Read_Power_ByT_id(sys, T_id) if err != nil { if err.Error() == orm.ErrNoRows.Error() { break } continue } // Nats 发送失败 重试3次 _, err = NatsServer.ERP_Delete_Power(sys, power) if err == nil || i == 0 { break } i-- } }(prefix) } System.Add_UserLogs_T(c.User.T_uuid, "权限", "删除", T_id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return }