From 722647dc0391fe95d4093d07e5efe3e38d8cd919 Mon Sep 17 00:00:00 2001 From: jiangyong Date: Mon, 16 Jun 2025 16:33:33 +0800 Subject: [PATCH] corpconfig --- common/model/corp_user.go | 46 +++++++++++++++++++++++++++++++++++++++ server/controller/base.go | 6 ++--- server/session/session.go | 6 ++--- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/common/model/corp_user.go b/common/model/corp_user.go index d52e7dd..123eb73 100644 --- a/common/model/corp_user.go +++ b/common/model/corp_user.go @@ -1,5 +1,11 @@ package model +import ( + "encoding/json" + "github.com/spf13/cast" + "strings" +) + type CorpUser struct { Id int64 CorpId int64 @@ -10,3 +16,43 @@ type CorpUser struct { CreateTime int64 UpdateTime int64 } + +type CorpUserConfig struct { + UserRole string `json:"user_role"` //员工角色 +} + +func (u *CorpUser) GetConfig() *CorpUserConfig { + var salary CorpUserConfig + json.Unmarshal([]byte(u.Config), &salary) + return &salary +} + +func (u *CorpUserConfig) IsAdmin() bool { + arrs := strings.Split(u.UserRole, ",") + for _, a := range arrs { + if cast.ToInt(a) == StaffUserRoleAdmin { + return true + } + } + return false +} + +func (u *CorpUserConfig) IsFinance() bool { + arrs := strings.Split(u.UserRole, ",") + for _, a := range arrs { + if cast.ToInt(a) == StaffUserRoleFinance || cast.ToInt(a) == StaffUserRoleAdmin { + return true + } + } + return false +} + +func (u *CorpUserConfig) IsHr() bool { + arrs := strings.Split(u.UserRole, ",") + for _, a := range arrs { + if cast.ToInt(a) == StaffUserRoleHr || cast.ToInt(a) == StaffUserRoleAdmin { + return true + } + } + return false +} diff --git a/server/controller/base.go b/server/controller/base.go index 31aa65a..d0d850a 100644 --- a/server/controller/base.go +++ b/server/controller/base.go @@ -73,15 +73,15 @@ func (c *Base) Before(ctx *gin.Context) { if time.Now().Unix() > tk.ExpireTime { panic(config.ErrTokenExpire.New()) } - staffUser, err := dao.NewStaffUserDao().Get(tk.UserId) + corpUser, err := dao.NewCorpUserDao().Get(tk.UserId) if err != nil { panic(config.ErrDb.New().Append(err)) } - if staffUser == nil { + if corpUser == nil { panic(config.ErrTokenInvaild.New()) } - ctx.Keys[session.ContextSession] = session.NewAdminSession(&header, staffUser) + ctx.Keys[session.ContextSession] = session.NewAdminSession(&header, corpUser) } func (c *Base) Token(ctx *gin.Context) { diff --git a/server/session/session.go b/server/session/session.go index b03c707..201dc92 100644 --- a/server/session/session.go +++ b/server/session/session.go @@ -23,10 +23,10 @@ type AdminHeader struct { type AdminSession struct { adminHeader *AdminHeader - adminUser *model.StaffUser + adminUser *model.CorpUser } -func NewAdminSession(header *AdminHeader, user *model.StaffUser) *AdminSession { +func NewAdminSession(header *AdminHeader, user *model.CorpUser) *AdminSession { return &AdminSession{ adminHeader: header, adminUser: user, @@ -37,7 +37,7 @@ func (s *AdminSession) GetHeader() *AdminHeader { return s.adminHeader } -func (s *AdminSession) GetAdmin() *model.StaffUser { +func (s *AdminSession) GetAdmin() *model.CorpUser { return s.adminUser }