123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- package controllers
- import (
- "ERP_user/Nats/NatsServer"
- "ERP_user/conf"
- "ERP_user/models/Account"
- "ERP_user/models/System"
- "fmt"
- menulibs "git.baozhida.cn/ERP_libs/Menu"
- powerlibs "git.baozhida.cn/ERP_libs/Power"
- "git.baozhida.cn/ERP_libs/lib"
- "github.com/astaxie/beego/logs"
- "github.com/beego/beego/v2/adapter/orm"
- beego "github.com/beego/beego/v2/server/web"
- "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
- }
|