admin_modify.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package admin
  2. import (
  3. "context"
  4. "gd_admin/apis"
  5. "gd_admin/common.in/config"
  6. "gd_admin/common.in/jsonrpc2"
  7. "gd_admin/common.in/storage"
  8. "gd_admin/common.in/utils"
  9. "gd_admin/errors"
  10. "gd_admin/impl/rbac"
  11. "encoding/base64"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/astaxie/beego/orm"
  15. "go.uber.org/zap"
  16. "time"
  17. )
  18. func AdminModify(ctx context.Context, req *apis.AdminModifyReq, reply *apis.AdminModifyReply) error {
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err := fmt.Errorf("%+v", r)
  23. e := &jsonrpc2.Error{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. l.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if req.AdminId == 0 {
  32. return errors.ArgsError
  33. }
  34. updateAdminInfo := func(db orm.Ormer) error {
  35. adminInfo := apis.AdminInfo{}
  36. err := orm.NewOrm().QueryTable("t_gd_admin").Filter("id", req.AdminId).One(&adminInfo)
  37. if err != nil {
  38. if err == orm.ErrNoRows {
  39. return errors.AdminsNotExist
  40. }
  41. l.Error("mysql",
  42. zap.String("sql", "SELECT * FROM t_gd_admin"),
  43. zap.String("fields", utils.MarshalJsonString(req)),
  44. zap.String("error", err.Error()))
  45. return errors.DataBaseError
  46. }
  47. if req.AdminPasswd != "" {
  48. cryPasswd, err := config.AesEncrypt(req.AdminPasswd, CRYPTO_KEY)
  49. if err != nil {
  50. return errors.ArgsError
  51. }
  52. adminInfo.AdminPassword = base64.StdEncoding.EncodeToString(cryPasswd)
  53. }
  54. adminInfo.AdminPhone = req.AdminPhone
  55. adminInfo.AdminEmail = req.AdminEmail
  56. adminInfo.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
  57. adminInfo.Status = 0
  58. _, err = orm.NewOrm().Update(&adminInfo)
  59. if err != nil {
  60. l.Error("mysql",
  61. zap.String("sql", "SELECT * FROM t_gd_admin"),
  62. zap.String("fields", utils.MarshalJsonString(adminInfo)),
  63. zap.String("error", err.Error()))
  64. return errors.DataBaseError
  65. }
  66. return nil
  67. }
  68. updateGroupInfo := func(db orm.Ormer) error {
  69. if req.GroupId == 0 {
  70. return nil
  71. }
  72. //// 查看是否需要新增分组
  73. //groupId, err := rbac.AddCostomGroup(db, req.NodeId)
  74. //if err != nil {
  75. // return err
  76. //}
  77. // 修改访问权限分组
  78. err := rbac.UpdateAccess(db, req.AdminId, int64(req.GroupId))
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. //处理事务
  85. tasks := []storage.DbaTasker{}
  86. tasks = append(tasks, storage.GenerateDbaTask(updateAdminInfo))
  87. tasks = append(tasks, storage.GenerateDbaTask(updateGroupInfo))
  88. storage.ExecTrans(tasks...)
  89. return nil
  90. }
  91. func UpdateStatus(ctx context.Context, req *apis.UpdateStatusReq, reply *apis.UpdateStatusReply) error {
  92. // 捕获各个task中的异常并返回给调用者
  93. defer func() {
  94. if r := recover(); r != nil {
  95. err := fmt.Errorf("%+v", r)
  96. e := &jsonrpc2.Error{}
  97. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  98. l.Error("err",
  99. zap.String("system_err", err.Error()),
  100. zap.Stack("stacktrace"))
  101. }
  102. }
  103. }()
  104. if req.AdminId <= 0 {
  105. return errors.ArgsError
  106. }
  107. // 判断用户是否存在
  108. adminInfo := apis.AdminInfo{}
  109. err := orm.NewOrm().QueryTable("t_gd_admin").Filter("id", req.AdminId).One(&adminInfo)
  110. if err != nil {
  111. if err == orm.ErrNoRows {
  112. return errors.AdminsNotExist
  113. }
  114. l.Error("mysql",
  115. zap.String("sql", "SELECT * FROM t_gd_admin"),
  116. zap.String("fields", utils.MarshalJsonString(req)),
  117. zap.String("error", err.Error()))
  118. return errors.DataBaseError
  119. }
  120. // 判断状态
  121. if adminInfo.Status == 0 {
  122. reply.Status = 1
  123. } else {
  124. reply.Status = 0
  125. }
  126. // value
  127. value := orm.Params{
  128. "status": reply.Status,
  129. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  130. }
  131. if _, err := orm.NewOrm().QueryTable("t_gd_admin").Filter("id", req.AdminId).Update(value); err != nil {
  132. l.Error("mysql",
  133. zap.String("sql", "Update t_gd_admin"),
  134. zap.String("fields", utils.MarshalJsonString(req, value)),
  135. zap.String("error", err.Error()))
  136. return errors.DataBaseError
  137. }
  138. return nil
  139. }