admin.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_mobile_admin/models/request/admin"
  5. admin3 "hongze/hongze_mobile_admin/models/response/admin"
  6. "hongze/hongze_mobile_admin/models/tables/h5_admin_session"
  7. "hongze/hongze_mobile_admin/services"
  8. "hongze/hongze_mobile_admin/utils"
  9. "time"
  10. )
  11. type AdminCommon struct {
  12. BaseAuth
  13. }
  14. // Login
  15. // @Title 用户账号、密码登录接口
  16. // @Description 用户账号、密码登录接口
  17. // @Param request body admin.LoginReq true "type json string"
  18. // @Success 200 {object} admin.LoginResp
  19. // @router /login [post]
  20. func (c *AdminCommon) Login() {
  21. var req admin.LoginReq
  22. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  23. if err != nil {
  24. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  25. return
  26. }
  27. if req.Username == "" {
  28. c.FailWithMessage("请输入账号", "请输入账号")
  29. return
  30. }
  31. if req.Password == "" {
  32. c.FailWithMessage("请输入密码", "请输入密码")
  33. return
  34. }
  35. adminWx, err, errMsg := services.BindWxUser(c.Session.OpenId, req.Username, req.Password, utils.WxPlatform)
  36. if err != nil {
  37. if err.Error() == utils.ErrNoRow() {
  38. c.FailWithMessage("登录失败,账号或密码错误", "登录失败,账号或密码错误")
  39. return
  40. }
  41. msg := ``
  42. if errMsg != `` {
  43. msg = errMsg
  44. }
  45. c.FailWithMessage(msg, "登录失败,Err:"+err.Error())
  46. return
  47. }
  48. if adminWx.Enabled != 1 {
  49. c.FailWithMessage("账户信息异常!", "账户状态异常,不允许登录")
  50. return
  51. }
  52. err = h5_admin_session.UpdateSession(c.Session.SessionId, adminWx.AdminId, time.Now().AddDate(0, 0, 90))
  53. if err != nil {
  54. c.FailWithMessage("登录失败", "变更session信息失败,Err:"+err.Error())
  55. return
  56. }
  57. resp := admin3.LoginResp{
  58. RealName: adminWx.RealName,
  59. AdminName: adminWx.AdminName,
  60. RoleName: adminWx.RoleName,
  61. RoleTypeCode: adminWx.RoleTypeCode,
  62. AdminId: adminWx.AdminId,
  63. DepartmentName: adminWx.DepartmentName,
  64. GroupName: adminWx.GroupName,
  65. }
  66. resp.AdminId = adminWx.AdminId
  67. var productName string
  68. productId := services.GetProductId(adminWx.RoleTypeCode)
  69. if productId == 1 {
  70. productName = utils.COMPANY_PRODUCT_FICC_NAME
  71. } else if productId == 2 {
  72. productName = utils.COMPANY_PRODUCT_RAI_NAME
  73. } else {
  74. productName = "admin"
  75. }
  76. resp.ProductName = productName
  77. resp.Authority = adminWx.Authority
  78. resp.Headimgurl = adminWx.Headimgurl
  79. if adminWx.AdminId == utils.ApproveUserId {
  80. resp.IsBusinessTrip = true
  81. }
  82. if adminWx.RoleTypeCode == "rai_seller" || adminWx.RoleTypeCode == "rai_group" || adminWx.RoleTypeCode == "rai_admin" {
  83. resp.IsRai = true
  84. }
  85. c.OkDetailed(resp, "登录成功")
  86. }
  87. // Logout
  88. // @Title 用户退出登录接口
  89. // @Description 用户退出登录接口
  90. // @Success Ret=200 退出成功
  91. // @router /logout [post]
  92. func (c *AdminCommon) Logout() {
  93. adminWx := c.AdminWx
  94. err := services.UnbindWxUser(adminWx.OpenId)
  95. if err != nil {
  96. c.FailWithMessage("退出失败", "退出失败,Err:"+err.Error())
  97. return
  98. }
  99. err = h5_admin_session.UpdateSession(c.Session.SessionId, 0, time.Now().AddDate(0, 0, 10))
  100. if err != nil {
  101. c.FailWithMessage("退出失败", "变更session信息失败,Err:"+err.Error())
  102. return
  103. }
  104. c.OkWithMessage("退出成功")
  105. }