admin.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_hub/models"
  5. "eta/eta_forum_hub/models/system"
  6. "eta/eta_forum_hub/services"
  7. "eta/eta_forum_hub/utils"
  8. )
  9. type AdminController struct {
  10. BaseAuthController
  11. }
  12. // Save
  13. // @Title 新增或者编辑系统用户
  14. // @Description 新增系统用户接口
  15. // @Param request body system.SysuserAddReq true "type json string"
  16. // @Success 200 新增成功
  17. // @router /save [post]
  18. func (this *AdminController) Save() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. var req system.AddAdminReq
  25. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  26. if err != nil {
  27. br.Msg = "参数解析异常!"
  28. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  29. return
  30. }
  31. if req.AdminName == "" {
  32. br.Msg = "请输入用户名"
  33. br.IsSendEmail = false
  34. return
  35. }
  36. if req.BusinessCode == "" {
  37. br.Msg = "请输入商户编码"
  38. br.IsSendEmail = false
  39. return
  40. }
  41. err, isSendEmail := services.AddOrUpdateAdmin(&req)
  42. if err != nil {
  43. br.Msg = "新增或者更新用户失败"
  44. br.ErrMsg = "新增或者更新用户失败, Err:" + err.Error()
  45. br.IsSendEmail = isSendEmail
  46. return
  47. }
  48. br.Ret = 200
  49. br.Success = true
  50. br.Msg = "新增成功"
  51. }
  52. // Delete
  53. // @Title 删除系统用户
  54. // @Description 删除系统用户接口
  55. // @Param request body system.AdminDeleteReq true "type json string"
  56. // @Success 200 删除成功
  57. // @router /delete [post]
  58. func (this *AdminController) Delete() {
  59. br := new(models.BaseResponse).Init()
  60. defer func() {
  61. this.Data["json"] = br
  62. this.ServeJSON()
  63. }()
  64. var req system.AdminDeleteReq
  65. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  66. if err != nil {
  67. br.Msg = "参数解析异常!"
  68. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  69. return
  70. }
  71. if req.AdminName == "" {
  72. br.Msg = "请选择用户"
  73. return
  74. }
  75. if req.BusinessCode == "" {
  76. br.Msg = "请选择商户"
  77. return
  78. }
  79. adminInfo, err := system.GetAdminByAdminNameAndBusinessCode(req.AdminName, req.BusinessCode)
  80. if err != nil {
  81. if err.Error() == utils.ErrNoRow() {
  82. br.Msg = "用户不存在"
  83. return
  84. }
  85. br.Msg = "获取用户信息失败"
  86. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  87. return
  88. }
  89. err = system.DeleteSysuser(adminInfo.AdminId)
  90. if err != nil {
  91. br.Msg = "删除失败"
  92. br.ErrMsg = "删除失败,Err:" + err.Error()
  93. return
  94. }
  95. br.Ret = 200
  96. br.Success = true
  97. br.Msg = "删除成功"
  98. }