user.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package controller
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/crypto/gmd5"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/genv"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "github.com/tiger1103/gfast/v3/api/v1/system"
  10. commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
  11. "github.com/tiger1103/gfast/v3/internal/app/system/model"
  12. "github.com/tiger1103/gfast/v3/internal/app/system/service"
  13. "github.com/tiger1103/gfast/v3/library/libUtils"
  14. )
  15. var (
  16. User = UserController{}
  17. )
  18. type UserController struct {
  19. baseController
  20. }
  21. func (c *UserController) Login(ctx context.Context, req *system.UserLoginReq) (res *system.UserLoginRes, err error) {
  22. var (
  23. user *model.LoginUserRes
  24. token string
  25. )
  26. //判断验证码是否正确
  27. debug := genv.GetWithCmd("gf.debug")
  28. if debug.Int() != 1 {
  29. if !commonService.Captcha().VerifyString(req.VerifyKey, req.VerifyCode) {
  30. err = gerror.New("验证码输入错误")
  31. return
  32. }
  33. }
  34. ip := libUtils.GetClientIp(ctx)
  35. userAgent := libUtils.GetUserAgent(ctx)
  36. user, err = service.User().GetAdminUserByUsernamePassword(ctx, req)
  37. if err != nil {
  38. // 保存登录失败的日志信息
  39. service.SysLoginLog().Invoke(ctx, &model.LoginLogParams{
  40. Status: 0,
  41. Username: req.Username,
  42. Ip: ip,
  43. UserAgent: userAgent,
  44. Msg: err.Error(),
  45. Module: "系统后台",
  46. })
  47. return
  48. }
  49. err = service.User().UpdateLoginInfo(ctx, user.Id, ip)
  50. if err != nil {
  51. return
  52. }
  53. // 报存登录成功的日志信息
  54. service.SysLoginLog().Invoke(ctx, &model.LoginLogParams{
  55. Status: 1,
  56. Username: req.Username,
  57. Ip: ip,
  58. UserAgent: userAgent,
  59. Msg: "登录成功",
  60. Module: "系统后台",
  61. })
  62. key := gconv.String(user.Id) + "-" + gmd5.MustEncryptString(user.UserName) + gmd5.MustEncryptString(user.UserPassword)
  63. if g.Cfg().MustGet(ctx, "gfToken.multiLogin").Bool() {
  64. key = gconv.String(user.Id) + "-" + gmd5.MustEncryptString(user.UserName) + gmd5.MustEncryptString(user.UserPassword+ip+userAgent)
  65. }
  66. token, err = service.GfToken(ctx).GenerateToken(ctx, key, user)
  67. if err != nil {
  68. return
  69. }
  70. //获取用户菜单数据
  71. menuList, err := service.User().GetAdminRules(ctx, user.Id)
  72. if err != nil {
  73. return
  74. }
  75. res = &system.UserLoginRes{
  76. UserInfo: user,
  77. Token: token,
  78. MenuList: menuList,
  79. }
  80. return
  81. }