| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package controller
- import (
- "context"
- "github.com/gogf/gf/v2/crypto/gmd5"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/genv"
- "github.com/gogf/gf/v2/util/gconv"
- "github.com/tiger1103/gfast/v3/api/v1/system"
- commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
- "github.com/tiger1103/gfast/v3/internal/app/system/model"
- "github.com/tiger1103/gfast/v3/internal/app/system/service"
- "github.com/tiger1103/gfast/v3/library/libUtils"
- )
- var (
- User = UserController{}
- )
- type UserController struct {
- baseController
- }
- func (c *UserController) Login(ctx context.Context, req *system.UserLoginReq) (res *system.UserLoginRes, err error) {
- var (
- user *model.LoginUserRes
- token string
- )
- //判断验证码是否正确
- debug := genv.GetWithCmd("gf.debug")
- if debug.Int() != 1 {
- if !commonService.Captcha().VerifyString(req.VerifyKey, req.VerifyCode) {
- err = gerror.New("验证码输入错误")
- return
- }
- }
- ip := libUtils.GetClientIp(ctx)
- userAgent := libUtils.GetUserAgent(ctx)
- user, err = service.User().GetAdminUserByUsernamePassword(ctx, req)
- if err != nil {
- // 保存登录失败的日志信息
- service.SysLoginLog().Invoke(ctx, &model.LoginLogParams{
- Status: 0,
- Username: req.Username,
- Ip: ip,
- UserAgent: userAgent,
- Msg: err.Error(),
- Module: "系统后台",
- })
- return
- }
- err = service.User().UpdateLoginInfo(ctx, user.Id, ip)
- if err != nil {
- return
- }
- // 报存登录成功的日志信息
- service.SysLoginLog().Invoke(ctx, &model.LoginLogParams{
- Status: 1,
- Username: req.Username,
- Ip: ip,
- UserAgent: userAgent,
- Msg: "登录成功",
- Module: "系统后台",
- })
- key := gconv.String(user.Id) + "-" + gmd5.MustEncryptString(user.UserName) + gmd5.MustEncryptString(user.UserPassword)
- if g.Cfg().MustGet(ctx, "gfToken.multiLogin").Bool() {
- key = gconv.String(user.Id) + "-" + gmd5.MustEncryptString(user.UserName) + gmd5.MustEncryptString(user.UserPassword+ip+userAgent)
- }
- token, err = service.GfToken(ctx).GenerateToken(ctx, key, user)
- if err != nil {
- return
- }
- //获取用户菜单数据
- menuList, err := service.User().GetAdminRules(ctx, user.Id)
- if err != nil {
- return
- }
- res = &system.UserLoginRes{
- UserInfo: user,
- Token: token,
- MenuList: menuList,
- }
- return
- }
|