change layout
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
admin "lone-services/rpc/admin/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) {
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
return l.fail(utils.ErrorNotFund)
|
||||
}
|
||||
|
||||
pwd := utils.GetSaltPassword(info.Salt, in.Password)
|
||||
eq := utils.EqualsPassword(pwd, info.Password)
|
||||
if !eq {
|
||||
return l.fail(utils.ErrorPwdError)
|
||||
}
|
||||
|
||||
data := dao.JwtInfo{
|
||||
Id: info.Id,
|
||||
Name: info.Name,
|
||||
Email: info.Email,
|
||||
Avatar: info.Avatar,
|
||||
LastTime: utils.Now(),
|
||||
}
|
||||
|
||||
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)
|
||||
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()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user