Эх сурвалжийг харах

system: 修改系统登录用户问题

Yi 1 жил өмнө
parent
commit
ab5b882939

+ 1 - 0
go.mod

@@ -61,6 +61,7 @@ require (
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
+	github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
 	github.com/pelletier/go-toml/v2 v2.0.8 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect

+ 2 - 0
go.sum

@@ -260,6 +260,8 @@ github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
 github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
 github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
 github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
+github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
+github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
 github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
 github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
 github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=

+ 3 - 6
module/backend/feed_service.go

@@ -122,7 +122,7 @@ func (s *StoreEntry) addFeedFormulaDetailAddRecode(ctx context.Context, req *ope
 	editRecord, _ := s.GetEditRecordLastGroupId(ctx)
 	editRecordList := make([]*model.FeedFormulaEditRecord, 0)
 
-	operationName, _ := s.GetCurrentUserName(ctx)
+	operationName := s.GetCurrentUserName(ctx)
 	for _, v := range req.List {
 		editRecordList = append(editRecordList, &model.FeedFormulaEditRecord{
 			FeedFormulaId: int64(req.FeedFormulaId),
@@ -183,11 +183,8 @@ func (s *StoreEntry) editFeedFormulaDetailAddRecode(ctx context.Context, req *op
 		Status:        operationPb.FeedFormulaEditRecordType_UPDATE,
 	}
 
-	if operationName, err := s.GetCurrentUserName(ctx); err != nil {
-		zaplog.Error("EditFeedByFeedFormula", zap.Any("GetCurrentUserName", err))
-	} else {
-		editRecordData.OperationName = operationName
-	}
+	operationName := s.GetCurrentUserName(ctx)
+	editRecordData.OperationName = operationName
 
 	lastGroupIdData := &model.FeedFormulaEditRecord{}
 	if err := s.DB.Model(new(model.FeedFormulaEditRecord)).

+ 3 - 3
module/backend/interface.go

@@ -117,8 +117,8 @@ type PastureService interface {
 type SystemService interface {
 	// Auth 系统用户相关
 	Auth(ctx context.Context, auth *operationPb.UserAuthData) (*operationPb.SystemToken, error)
-	GetCurrentUserName(ctx context.Context) (string, error)
-	GetUserInfo(ctx context.Context, token string) (*operationPb.UserAuth, error)
+	GetCurrentUserName(ctx context.Context) string
+	GetUserInfo(ctx context.Context) (*operationPb.UserAuth, error)
 	CreateSystemUser(ctx context.Context, req *operationPb.AddSystemUser) error
 	SearchSystemUserList(ctx context.Context, req *operationPb.SearchUserRequest) (*operationPb.SearchUserResponse, error)
 	EditSystemUser(ctx context.Context, req *operationPb.AddSystemUser) error
@@ -126,7 +126,7 @@ type SystemService interface {
 	ResetPasswordSystemUser(ctx context.Context, userId int64) error
 	DetailsSystemUser(ctx context.Context, userId int64) (*operationPb.UserDetails, error)
 	IsShowSystemUser(ctx context.Context, req *operationPb.IsShowSystemUserRequest) error
-	GetSystemUserPermissions(ctx context.Context, token string) (*operationPb.SystemUserMenuPermissions, error)
+	GetSystemUserPermissions(ctx context.Context) (*operationPb.SystemUserMenuPermissions, error)
 
 	// CreateSystemRole 系统角色相关
 	CreateSystemRole(ctx context.Context, req *operationPb.AddRoleRequest) error

+ 22 - 25
module/backend/system_service.go

@@ -4,15 +4,16 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"kpt-tmr-group/model"
+	"net/http"
+	"strconv"
+	"strings"
+
 	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 	"gitee.com/xuyiping_admin/pkg/jwt"
 	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 	"gitee.com/xuyiping_admin/pkg/tool"
 	"gitee.com/xuyiping_admin/pkg/xerr"
-	"kpt-tmr-group/model"
-	"net/http"
-	"strconv"
-	"strings"
 
 	"go.uber.org/zap"
 
@@ -51,28 +52,27 @@ func (s *StoreEntry) Auth(ctx context.Context, auth *operationPb.UserAuthData) (
 	}, nil
 }
 
-func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
+func (s *StoreEntry) GetCurrentUserName(ctx context.Context) string {
 	userNameInter := ctx.Value(CurrentUserName)
 	if userNameInter == nil {
-		return "", xerr.Customf("cannot userName")
+		zaplog.Error("GetCurrentUserName", zap.String("userNameInter", "cannot userName"))
+		return ""
 	}
 
 	if userName, ok := userNameInter.(string); ok {
-		return userName, nil
+		return userName
 	} else {
-		return "", xerr.Customf("waring userName")
+		zaplog.Error("GetCurrentUserName", zap.String("userNameInter", "waring userName"))
+		return ""
 	}
 }
 
 // GetUserInfo 获取用户信息
-func (s *StoreEntry) GetUserInfo(ctx context.Context, token string) (*operationPb.UserAuth, error) {
+func (s *StoreEntry) GetUserInfo(ctx context.Context) (*operationPb.UserAuth, error) {
 	systemUser := &model.SystemUser{}
-	userName, err := s.GetCurrentUserName(ctx)
-	if err != nil {
-		return nil, xerr.WithStack(err)
-	}
+	userName := s.GetCurrentUserName(ctx)
 
-	if err = s.DB.Where("name = ?", userName).First(systemUser).Error; err != nil {
+	if err := s.DB.Where("name = ?", userName).First(systemUser).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
@@ -86,12 +86,12 @@ func (s *StoreEntry) GetUserInfo(ctx context.Context, token string) (*operationP
 			roleIds = append(roleIds, roleId)
 		}
 
-		if err = s.DB.Model(new(model.SystemGroupPasturePermissions)).Select("DISTINCT(group_pasture.id) AS did, group_pasture.id as id,group_pasture.name as name").
+		if err := s.DB.Model(new(model.SystemGroupPasturePermissions)).Select("DISTINCT(group_pasture.id) AS did, group_pasture.id as id,group_pasture.name as name").
 			Where("system_group_pasture_permissions.role_id IN ?", roleIds).Where("system_group_pasture_permissions.is_show = ?", operationPb.IsShow_OK).
 			Joins("right join group_pasture  on system_group_pasture_permissions.pasture_id = group_pasture.id").Find(&pastureList).Debug().Error; err != nil {
 		}
 
-		if err = s.DB.Find(&systemRole, roleIds).Error; err != nil {
+		if err := s.DB.Find(&systemRole, roleIds).Error; err != nil {
 			return nil, xerr.WithStack(err)
 		}
 	}
@@ -101,13 +101,12 @@ func (s *StoreEntry) GetUserInfo(ctx context.Context, token string) (*operationP
 
 // CreateSystemUser 创建系统用户
 func (s *StoreEntry) CreateSystemUser(ctx context.Context, req *operationPb.AddSystemUser) error {
-
 	systemUsers := &model.SystemUser{
 		Name:         req.Name,
 		EmployeeName: req.EmployeeName,
 		Phone:        req.Phone,
 		Password:     tool.Md5String(model.InitManagerPassword),
-		CreateUser:   req.CreateUser,
+		CreateUser:   s.GetCurrentUserName(ctx),
 		IsShow:       operationPb.IsShow_OK,
 		IsDelete:     operationPb.IsShow_OK,
 	}
@@ -262,15 +261,12 @@ func (s *StoreEntry) IsShowSystemUser(ctx context.Context, req *operationPb.IsSh
 }
 
 // GetSystemUserPermissions 返回系统用户相关菜单权限
-func (s *StoreEntry) GetSystemUserPermissions(ctx context.Context, token string) (*operationPb.SystemUserMenuPermissions, error) {
+func (s *StoreEntry) GetSystemUserPermissions(ctx context.Context) (*operationPb.SystemUserMenuPermissions, error) {
 	// 解析token
-	userName, err := s.GetCurrentUserName(ctx)
-	if err != nil {
-		return nil, xerr.WithStack(err)
-	}
+	userName := s.GetCurrentUserName(ctx)
 	// 根据用户token获取用户数据
 	systemUser := &model.SystemUser{Name: userName}
-	if err = s.DB.Where("name = ?", userName).First(systemUser).Error; err != nil {
+	if err := s.DB.Where("name = ?", userName).First(systemUser).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
 			return nil, xerr.Custom("该用户数据不存在")
 		}
@@ -280,7 +276,7 @@ func (s *StoreEntry) GetSystemUserPermissions(ctx context.Context, token string)
 
 	// 获取用户角色数据
 	systemRoles := make([]*model.SystemRole, 0)
-	if err = s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&systemRoles, roleIds).Error; err != nil {
+	if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&systemRoles, roleIds).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
@@ -321,6 +317,7 @@ func (s *StoreEntry) GetSystemUserPermissions(ctx context.Context, token string)
 func (s *StoreEntry) CreateSystemRole(ctx context.Context, req *operationPb.AddRoleRequest) error {
 	if err := s.DB.Transaction(func(tx *gorm.DB) error {
 		// 创建角色数据
+		req.CreateUser = s.GetCurrentUserName(ctx)
 		role := model.NewSystemRole(req)
 		if err := tx.Create(role).Error; err != nil {
 			return xerr.WithStack(err)

+ 22 - 0
service/cache/go-cache/cache.go

@@ -0,0 +1,22 @@
+package go_cache
+
+import (
+	"time"
+
+	"github.com/patrickmn/go-cache"
+)
+
+const (
+	DefaultExpiration time.Duration = 5 * time.Minute
+	CleanupInterval   time.Duration = 10 * time.Minute
+)
+
+type GoCache struct {
+	Cache *cache.Cache
+}
+
+func NewCache() *GoCache {
+	return &GoCache{
+		Cache: cache.New(DefaultExpiration, CleanupInterval),
+	}
+}