user.go 1.9 KB

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