Files
lone-services/services/admin/internal/logic/loginlogic.go
T
gjs 23ea6659c2
CI / changes (push) Successful in 12s
CI / docker-bff (push) Has been skipped
CI / docker-product (push) Has been skipped
CI / docker-admin (push) Successful in 30s
CI / docker-user (push) Has been skipped
CI / docker-ad (push) Has been skipped
CI / docker-chore (push) Has been skipped
CI / docker-express (push) Has been skipped
CI / docker-sale (push) Has been skipped
更新登录接口
2026-08-28 16:34:16 +08:00

156 lines
4.5 KiB
Go

package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/redis"
"lone-services/pkg/utils"
admin "lone-services/rpc/admin/pb"
sale "lone-services/rpc/sale/pb"
"lone-services/services/admin/internal/dao"
"lone-services/services/admin/internal/model"
"lone-services/services/admin/internal/svc"
"lone-services/services/admin/validator"
"strconv"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/zeromicro/go-zero/core/logx"
)
type LoginLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
BaseLogic
}
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
return &LoginLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *LoginLogic) Login(in *admin.LoginRequest) (*admin.Response, error) {
var v validator.LoginValidator
if fail := l.checkParams(in, &v); fail != nil {
return fail, nil
}
phone, dErr := utils.EncryptPhone(in.Username)
if dErr != nil {
l.Logger.Error(dErr)
return l.fail(utils.Fail)
}
adminInfo := utils.GetUserFromCtx(l.ctx)
adminInfo.Name = in.Username
var info dao.Admin
w := modelbase.Params{Eq: map[string]string{"phone": phone}}
modelObj := model.AdminModel{}.Init()
err := modelObj.GetOne(w, &info)
if err != nil {
l.Logger.Error(err)
return l.fail(utils.Fail)
}
if info.Id < utils.NumberOne {
utils.SetLoginLog(adminInfo, false, utils.ErrorNotFund.Msg)
return l.fail(utils.ErrorNotFund)
}
pwd := utils.GetSaltPassword(info.Salt, in.Password)
eq := utils.EqualsPassword(pwd, info.Password)
if !eq {
utils.SetLoginLog(adminInfo, false, utils.ErrorPwdError.Msg)
return l.fail(utils.ErrorPwdError)
}
data := dao.JwtInfo{
Id: info.Id,
Name: info.Name,
Email: info.Email,
Type: info.Type,
Avatar: info.Avatar,
LastTime: utils.Now(),
}
if info.Type == dao.AdminTypeSale {
cli, err := svc.GetRpcClient(l.svcCtx.SaleSvcName)
if err != nil {
logx.Errorf("get rpc client err: %v", err)
return l.fail(utils.ErrorInternalServer)
}
saleClient := sale.NewSaleClient(cli.Conn())
saleInfo, err := saleClient.InfoInternal(context.Background(), &sale.InfoReq{Id: info.SaleId})
if err != nil {
logx.Errorf("get sale err: %v", err)
return l.fail(utils.ErrorInternalServer)
}
data.GroupName = saleInfo.Group.Name
data.GroupId = saleInfo.GroupId
data.SaleId = saleInfo.Id
data.SaleName = saleInfo.Name
data.SaleMobile = saleInfo.OriginMobile
}
data.Phone, _ = utils.DecryptPhone(info.Phone)
retData := dao.Token{
Token: utils.MD5Encrypt(data.Phone + utils.Now().String()),
Info: dao.JwtOutInfo{
Avatar: data.Avatar,
Name: data.Name,
Phone: data.Phone,
Email: data.Email,
},
}
retData.Refresh = utils.MD5Encrypt(data.Phone + retData.Token)
l.setLogin(retData.Token, retData.Refresh, data)
adminInfo.ID = data.Id
utils.SetLoginLog(adminInfo, true, utils.StringEmpty)
return l.ok(retData)
}
func (l *LoginLogic) setLogin(token, refresh string, data dao.JwtInfo) {
data.Refresh = refresh
//获取登录过期时间
expireMin := utils.GetConfigInt("base.login_out_time")
expire := time.Duration(expireMin) * time.Minute
//获取刷新登录过期时间
refreshMin := utils.GetConfigInt("base.login_refresh_out_time")
refreshExpire := time.Duration(refreshMin) * time.Minute
//登录信息写入redis
redisKey := utils.GetLoginKey(utils.LoginTypeAdmin, token)
userStr, _ := jsoniter.Marshal(data)
ctx := context.Background()
redis.Client.Set(ctx, redisKey, userStr, expire).Result()
//获取原来的token与refresh
redisKeysKey := utils.GetLoginKeysKey(utils.LoginTypeAdmin, strconv.FormatInt(data.Id, utils.NumberTen))
var LoginInfo utils.LoginRedis
oldAuth, oldErr := redis.Client.Get(ctx, redisKeysKey).Result()
//如果获取到了数据情况之前的
if oldErr == nil {
_ = jsoniter.Unmarshal([]byte(oldAuth), &LoginInfo)
if len(LoginInfo.Token) > utils.NumberOne {
redis.Client.Del(ctx, utils.GetLoginKey(utils.LoginTypeAdmin, LoginInfo.Token)).Result()
}
if len(LoginInfo.Refresh) > utils.NumberOne {
redis.Client.Del(ctx, utils.GetLoginRefreshKey(utils.LoginTypeAdmin, LoginInfo.Refresh)).Result()
}
}
//更新新的数据
LoginInfo.Token = token
LoginInfo.Refresh = refresh
LoginInfo.Info = userStr
newStr, _ := jsoniter.Marshal(LoginInfo)
redis.Client.Set(ctx, redisKeysKey, newStr, refreshExpire).Result()
//登录刷新信息写入redis
refreshKey := utils.GetLoginRefreshKey(utils.LoginTypeAdmin, refresh)
redis.Client.Set(ctx, refreshKey, strconv.FormatInt(data.Id, utils.NumberTen), refreshExpire).Result()
}