用户信息传到service
This commit is contained in:
+10
-14
@@ -18,28 +18,24 @@ type Admin struct {
|
||||
FailNumber int `gorm:"column:fail_number;type:tinyint(2);default:0;comment:连续登录失败次数" json:"fail_number"`
|
||||
Reason string `gorm:"column:reason;type:varchar(255);comment:原因" json:"reason"`
|
||||
AdminId int `gorm:"column:admin_id;type:int(11);default:0;comment:操作人;NOT NULL" json:"admin_id"`
|
||||
CreateTime utils.CustomTime `gorm:"column:create_time;type:datetime;default:NULL;comment:添加时间" json:"create_time"`
|
||||
UpdateTime utils.CustomTime `gorm:"column:update_time;type:datetime;default:NULL;comment:更新时间" json:"update_time"`
|
||||
}
|
||||
|
||||
// AdminPassword 修改管理员密码
|
||||
type AdminPassword struct {
|
||||
Id int `json:"id"`
|
||||
Password string `json:"password"`
|
||||
Salt string `json:"salt"`
|
||||
AdminId int `json:"admin_id"`
|
||||
AdminName string `gorm:"column:admin_name;type:varchar(100);comment:操作人姓名" json:"admin_name"`
|
||||
UpdateTime utils.CustomTime `gorm:"column:update_time;type:datetime;default:NULL;comment:更新时间" json:"update_time"`
|
||||
Id int `json:"id"`
|
||||
Password string `json:"password"`
|
||||
Salt string `json:"salt"`
|
||||
AdminId int `json:"admin_id"`
|
||||
AdminName string `gorm:"column:admin_name;type:varchar(100);comment:操作人姓名" json:"admin_name"`
|
||||
}
|
||||
|
||||
// AdminStatus 修改管理员状态
|
||||
type AdminStatus struct {
|
||||
Id int `json:"id"`
|
||||
Status uint8 `json:"status"`
|
||||
Reason string `json:"reason"`
|
||||
AdminId int `json:"admin_id"`
|
||||
AdminName string `gorm:"column:admin_name;type:varchar(100);comment:操作人姓名" json:"admin_name"`
|
||||
UpdateTime utils.CustomTime `gorm:"column:update_time;type:datetime;default:NULL;comment:更新时间" json:"update_time"`
|
||||
Id int `json:"id"`
|
||||
Status uint8 `json:"status"`
|
||||
Reason string `json:"reason"`
|
||||
AdminId int `json:"admin_id"`
|
||||
AdminName string `gorm:"column:admin_name;type:varchar(100);comment:操作人姓名" json:"admin_name"`
|
||||
}
|
||||
|
||||
// AdminInfo 管理员详情
|
||||
|
||||
+14
-10
@@ -1,22 +1,26 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"pkg.local/utils"
|
||||
)
|
||||
|
||||
type JwtInfo struct {
|
||||
Id int64 `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
|
||||
Name string `gorm:"column:name;type:varchar(30);comment:真实姓名;NOT NULL" json:"name"`
|
||||
Phone string `gorm:"column:phone;type:char(12);comment:电话" json:"phone"`
|
||||
Email string `gorm:"column:email;type:varchar(255);comment:邮箱" json:"email"`
|
||||
Roles string `gorm:"column:roles;type:varchar(255);comment:角色" json:"roles"`
|
||||
LastTime utils.CustomTime `gorm:"column:last_time;type:datetime;comment:最后一次登录时间" json:"last_time"`
|
||||
Time int `json:"time"`
|
||||
jwt.RegisteredClaims
|
||||
Id int64 `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
|
||||
Name string `gorm:"column:name;type:varchar(30);comment:真实姓名;NOT NULL" json:"name"`
|
||||
Phone string `gorm:"column:phone;type:char(12);comment:电话" json:"phone"`
|
||||
Email string `gorm:"column:email;type:varchar(255);comment:邮箱" json:"email"`
|
||||
LastTime utils.CustomTime `gorm:"column:last_time;type:datetime;comment:最后一次登录时间" json:"last_time"`
|
||||
Time int `json:"time"`
|
||||
Roles []JwtInfoServices `json:"roles"`
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type JwtInfoServices struct {
|
||||
Id int `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
|
||||
Name string `gorm:"column:name;type:varchar(255);comment:项目描述;NOT NULL" json:"name"`
|
||||
Items map[string]string `json:"items"`
|
||||
}
|
||||
|
||||
@@ -33,5 +33,4 @@ type ServicesStatus struct {
|
||||
type ServicesNameItems struct {
|
||||
Id int `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
|
||||
Name string `gorm:"column:name;type:varchar(255);comment:项目描述;NOT NULL" json:"name"`
|
||||
Logo string `gorm:"column:logo;type:varchar(255);default:'';comment:logo;NOT NULL" json:"logo"`
|
||||
}
|
||||
|
||||
@@ -36,7 +36,10 @@ func (l *AdminEditLogic) AdminEdit(in *admin.AdminEditRequest) (*admin.Response,
|
||||
if fail := l.checkParams(in, &v); fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
|
||||
adminInfo := utils.GetUserFromCtx(l.ctx)
|
||||
if adminInfo.ID < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
encryPhone, cErr := utils.EncryptPhone(in.Phone)
|
||||
if cErr != nil {
|
||||
l.Logger.Error(cErr)
|
||||
@@ -78,8 +81,9 @@ func (l *AdminEditLogic) AdminEdit(in *admin.AdminEditRequest) (*admin.Response,
|
||||
return l.fail(utils.ErrorExistUser), nil
|
||||
}
|
||||
}
|
||||
//adminId, _ := c.Get(utils.USERID)
|
||||
//adminName, _ := c.Get(utils.USERNAME)
|
||||
|
||||
info.AdminId = int(adminInfo.ID)
|
||||
info.AdminName = adminInfo.Name
|
||||
var roles []string
|
||||
if len(in.Roles) > utils.NumberZero {
|
||||
for _, role := range in.Roles {
|
||||
|
||||
@@ -7,9 +7,13 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -29,12 +33,13 @@ func NewAdminLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminL
|
||||
}
|
||||
|
||||
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.DecryptPhone(in.Username)
|
||||
phone, dErr := utils.EncryptPhone(in.Username)
|
||||
if dErr != nil {
|
||||
l.Logger.Error(dErr)
|
||||
return l.fail(utils.Fail), nil
|
||||
@@ -59,12 +64,12 @@ func (l *AdminLoginLogic) AdminLogin(in *admin.AdminLoginRequest) (*admin.Respon
|
||||
}
|
||||
|
||||
data := dao.JwtInfo{
|
||||
Id: info.Id,
|
||||
Name: info.Name,
|
||||
Email: info.Email,
|
||||
Phone: info.Phone,
|
||||
Avatar: info.Avatar,
|
||||
Roles: info.Roles,
|
||||
Id: info.Id,
|
||||
Name: info.Name,
|
||||
Email: info.Email,
|
||||
Phone: info.Phone,
|
||||
Avatar: info.Avatar,
|
||||
//Roles: info.Roles,
|
||||
LastTime: utils.Now(),
|
||||
}
|
||||
retData := dao.Token{
|
||||
@@ -77,16 +82,24 @@ func (l *AdminLoginLogic) AdminLogin(in *admin.AdminLoginRequest) (*admin.Respon
|
||||
}
|
||||
|
||||
func (l *AdminLoginLogic) setLogin(token string, data dao.JwtInfo) {
|
||||
//duration := utils.GetConfigInt("base.login_out_time")
|
||||
//if data.Time > utils.NumberZero {
|
||||
// duration = int(time.Minute * time.Duration(int64(data.Time)))
|
||||
//}
|
||||
//redisKey := utils.GetLoginKey(utils.LoginUser, token)
|
||||
//userStr, _ := jsoniter.Marshal(data)
|
||||
//redis.New().Set(redisKey, string(userStr), duration).Result()
|
||||
//oldAuth, oldErr := utils.Redis.HGet(utils.GetRedisKey(utils.LoginUserKey), strconv.Itoa(data.Id)).Result()
|
||||
//if oldErr == nil {
|
||||
// utils.Redis.Del(utils.GetRedisKey(utils.LoginUser + oldAuth)).Result()
|
||||
//}
|
||||
//utils.Redis.HSet(utils.GetRedisKey(utils.LoginUserKey), strconv.Itoa(data.Id), token).Result()
|
||||
expireMin := utils.GetConfigInt("base.login_out_time")
|
||||
var expire time.Duration
|
||||
// 默认使用配置时长
|
||||
expire = time.Duration(expireMin) * time.Minute
|
||||
// 如果用户自定义了过期时间则覆盖
|
||||
if data.Time > utils.NumberZero {
|
||||
expire = time.Duration(data.Time) * time.Minute
|
||||
}
|
||||
|
||||
redisKey := utils.GetLoginKey(utils.LoginTypeAdmin, token)
|
||||
l.Logger.Error(redisKey)
|
||||
userStr, _ := jsoniter.Marshal(data)
|
||||
ctx := context.Background()
|
||||
redis.Client.Set(ctx, redisKey, userStr, expire).Result()
|
||||
redisKeysKey := utils.GetLoginKeysKey(utils.LoginTypeAdmin)
|
||||
oldAuth, oldErr := redis.Client.HGet(ctx, redisKeysKey, strconv.Itoa(int(data.Id))).Result()
|
||||
if oldErr == nil {
|
||||
redis.Client.Del(ctx, utils.GetLoginKey(utils.LoginTypeAdmin, oldAuth)).Result()
|
||||
}
|
||||
redis.Client.HSet(ctx, redisKeysKey, strconv.Itoa(int(data.Id)), token).Result()
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ func (l *AdminStatusLogic) AdminStatus(in *admin.StatusRequest) (*admin.Response
|
||||
if fail := l.checkParams(in, &v); fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
adminInfo := utils.GetUserFromCtx(l.ctx)
|
||||
if adminInfo.ID < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
var info dao.AdminStatus
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.Itoa(int(in.Id))}}
|
||||
modelObj := model.AdminModel{}.Init()
|
||||
@@ -50,11 +54,8 @@ func (l *AdminStatusLogic) AdminStatus(in *admin.StatusRequest) (*admin.Response
|
||||
|
||||
info.Status = uint8(in.Status)
|
||||
info.Reason = in.Reason
|
||||
info.UpdateTime = utils.Now()
|
||||
//adminId, _ := c.Get(utils.USERID)
|
||||
//adminName, _ := c.Get(utils.USERNAME)
|
||||
//info.AdminId = adminId.(int)
|
||||
//info.AdminName = adminName.(string)
|
||||
info.AdminId = int(adminInfo.ID)
|
||||
info.AdminName = adminInfo.Name
|
||||
_, editErr := modelObj.Edit(w, info)
|
||||
if editErr != nil {
|
||||
l.Logger.Error(editErr)
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"admin/internal/dao"
|
||||
"admin/internal/model"
|
||||
"admin/validator"
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/utils"
|
||||
)
|
||||
|
||||
type OwnPasswordLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
BaseLogic
|
||||
}
|
||||
|
||||
func NewOwnPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OwnPasswordLogic {
|
||||
@@ -24,7 +32,36 @@ func NewOwnPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OwnPa
|
||||
}
|
||||
|
||||
func (l *OwnPasswordLogic) OwnPassword(in *admin.OwnPasswordRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
var v validator.AdminOwnPasswordValidator
|
||||
if fail := l.checkParams(in, &v); fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
var info dao.AdminPassword
|
||||
md, exist := metadata.FromIncomingContext(l.ctx)
|
||||
l.Logger.Info("收到完整metadata", exist, md)
|
||||
adminInfo := utils.GetUserFromCtx(l.ctx)
|
||||
if adminInfo.ID < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
l.Logger.Info("解析出用户信息", adminInfo.ID, adminInfo.Name)
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.Itoa(int(adminInfo.ID))}}
|
||||
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
|
||||
}
|
||||
|
||||
return &admin.Response{}, nil
|
||||
pwd := utils.GetSaltPassword(info.Salt, in.Password)
|
||||
pwd, _ = utils.EncryptPassword(pwd)
|
||||
info.Password = pwd
|
||||
_, editErr := modelObj.Edit(w, info)
|
||||
if editErr != nil {
|
||||
l.Logger.Error(err)
|
||||
return l.fail(utils.Fail), nil
|
||||
}
|
||||
return l.ok(utils.NumberOne), nil
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ func (l *PasswordLogic) Password(in *admin.PasswordRequest) (*admin.Response, er
|
||||
if fail := l.checkParams(in, &v); fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
adminInfo := utils.GetUserFromCtx(l.ctx)
|
||||
if adminInfo.ID < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
var info dao.AdminPassword
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.Itoa(int(in.Id))}}
|
||||
modelObj := model.AdminModel{}.Init()
|
||||
@@ -50,11 +54,8 @@ func (l *PasswordLogic) Password(in *admin.PasswordRequest) (*admin.Response, er
|
||||
pwd := utils.GetSaltPassword(info.Salt, in.Password)
|
||||
pwd, _ = utils.EncryptPassword(pwd)
|
||||
info.Password = pwd
|
||||
info.UpdateTime = utils.Now()
|
||||
//adminId, _ := c.Get(utils.USERID)
|
||||
//adminName, _ := c.Get(utils.USERNAME)
|
||||
//info.AdminId = adminId.(int)
|
||||
//info.AdminName = adminName.(string)
|
||||
info.AdminId = int(adminInfo.ID)
|
||||
info.AdminName = adminInfo.Name
|
||||
_, editErr := modelObj.Edit(w, info)
|
||||
if editErr != nil {
|
||||
l.Logger.Error(err)
|
||||
|
||||
Reference in New Issue
Block a user