admin.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 := 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. c.FailWithMessage("登录失败", "登录失败,Err:"+err.Error())
  42. return
  43. }
  44. err = h5_admin_session.UpdateSession(c.Session.SessionId, adminWx.AdminId, time.Now().AddDate(0, 0, 90))
  45. if err != nil {
  46. c.FailWithMessage("登录失败", "变更session信息失败,Err:"+err.Error())
  47. return
  48. }
  49. resp := admin3.LoginResp{
  50. RealName: adminWx.RealName,
  51. AdminName: adminWx.AdminName,
  52. RoleName: adminWx.RoleName,
  53. RoleTypeCode: adminWx.RoleTypeCode,
  54. AdminId: adminWx.AdminId,
  55. DepartmentName: adminWx.DepartmentName,
  56. GroupName: adminWx.GroupName,
  57. }
  58. resp.AdminId = adminWx.AdminId
  59. var productName string
  60. productId := services.GetProductId(adminWx.RoleTypeCode)
  61. if productId == 1 {
  62. productName = utils.COMPANY_PRODUCT_FICC_NAME
  63. } else if productId == 2 {
  64. productName = utils.COMPANY_PRODUCT_RAI_NAME
  65. } else {
  66. productName = "admin"
  67. }
  68. resp.ProductName = productName
  69. resp.Authority = adminWx.Authority
  70. resp.Headimgurl = adminWx.Headimgurl
  71. c.OkDetailed(resp, "登录成功")
  72. }
  73. // Logout
  74. // @Title 用户退出登录接口
  75. // @Description 用户退出登录接口
  76. // @Success Ret=200 退出成功
  77. // @router /logout [post]
  78. func (c *AdminCommon) Logout() {
  79. adminWx := c.AdminWx
  80. err := services.UnbindWxUser(adminWx.OpenId)
  81. if err != nil {
  82. c.FailWithMessage("退出失败", "退出失败,Err:"+err.Error())
  83. return
  84. }
  85. err = h5_admin_session.UpdateSession(c.Session.SessionId, 0, time.Now().AddDate(0, 0, 10))
  86. if err != nil {
  87. c.FailWithMessage("退出失败", "变更session信息失败,Err:"+err.Error())
  88. return
  89. }
  90. c.OkWithMessage("退出成功")
  91. }