122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"admin/admin"
|
|
"admin/internal/dao"
|
|
"admin/internal/model"
|
|
"admin/internal/svc"
|
|
"admin/validator"
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"pkg.local/modelbase"
|
|
"pkg.local/redis"
|
|
"pkg.local/utils"
|
|
)
|
|
|
|
type AdminLoginLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewAdminLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminLoginLogic {
|
|
return &AdminLoginLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *AdminLoginLogic) AdminLogin(in *admin.AdminLoginRequest) (*admin.Response, error) {
|
|
l.Logger.Error("u:", in.Username, "p:", in.Password, "|")
|
|
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), nil
|
|
}
|
|
|
|
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), nil
|
|
}
|
|
if info.Id < utils.NumberOne {
|
|
return l.fail(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
pwd := utils.GetSaltPassword(info.Salt, in.Password)
|
|
eq := utils.EqualsPassword(pwd, info.Password)
|
|
if !eq {
|
|
return l.fail(utils.ErrorPwdError), nil
|
|
}
|
|
|
|
data := dao.JwtInfo{
|
|
Id: info.Id,
|
|
Name: info.Name,
|
|
Email: info.Email,
|
|
Phone: info.Phone,
|
|
Avatar: info.Avatar,
|
|
LastTime: utils.Now(),
|
|
}
|
|
retData := dao.Token{
|
|
Token: utils.MD5Encrypt(data.Phone + utils.Now().String()),
|
|
Info: data,
|
|
}
|
|
retData.Refresh = utils.MD5Encrypt(data.Phone + retData.Token)
|
|
|
|
l.setLogin(retData.Token, retData.Refresh, data)
|
|
return l.ok(retData), nil
|
|
}
|
|
|
|
func (l *AdminLoginLogic) setLogin(token, refresh string, data dao.JwtInfo) {
|
|
//获取登录过期时间
|
|
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()
|
|
|
|
}
|