From b575e5e760680c7da58c7caa7a18f87dff36c75d Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Sat, 29 Mar 2025 16:58:05 +0800 Subject: [PATCH] usrrole --- common/model/staff_user.go | 40 ++++++++++++++++++++++++++- conf/corp/1000.json | 15 ++++++++++ conf/corp/1002.json | 16 ++--------- server/controller/calculator.go | 12 ++++++++ server/controller/salary.go | 21 ++++++++++++++ server/controller/staff.go | 49 ++++++++++++++++++--------------- server/server.go | 1 - 7 files changed, 116 insertions(+), 38 deletions(-) diff --git a/common/model/staff_user.go b/common/model/staff_user.go index 06d290c..ed408a2 100644 --- a/common/model/staff_user.go +++ b/common/model/staff_user.go @@ -1,11 +1,18 @@ package model -import "encoding/json" +import ( + "encoding/json" + "github.com/spf13/cast" + "strings" +) var ( StaffUserStatusOnline = 1 //在职 StaffUserStatusOffline = 2 //离职 StaffUserStatusAttach = 3 //挂靠 + StaffUserRoleAdmin = 1 //管理员 + StaffUserRoleFinance = 2 //财务 + StaffUserRoleHr = 3 //人事 ) type UserSalary struct { @@ -25,6 +32,7 @@ type UserConfig struct { HouseDeduct string `json:"house_deduct"` //公积金扣除 SalaryCalcutor string `json:"salary_calcutor"` //工资计算器 PerftTarget string `json:"perft_target"` //绩效目标 + UserRole string `json:"user_role"` //员工角色 } type StaffUser struct { @@ -63,3 +71,33 @@ func (u *StaffUser) GetPayee() *UserPayee { json.Unmarshal([]byte(u.Payee), &salary) return &salary } + +func (u *UserConfig) IsAdmin() bool { + arrs := strings.Split(u.UserRole, ",") + for _, a := range arrs { + if cast.ToInt(a) == StaffUserRoleAdmin { + return true + } + } + return false +} + +func (u *UserConfig) 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 *UserConfig) 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/conf/corp/1000.json b/conf/corp/1000.json index a35184a..e0d3714 100644 --- a/conf/corp/1000.json +++ b/conf/corp/1000.json @@ -1,6 +1,21 @@ { "salary_latest": 9, "staff_config": [ + { + "name": "员工角色", + "key": "user_role", + "type": "checkbox", + "option": [{ + "name": "管理员", + "value": "1" + },{ + "name": "财务", + "value": "2" + },{ + "name": "人事", + "value": "3" + }] + }, { "name": "目标绩效", "key": "perft_target" diff --git a/conf/corp/1002.json b/conf/corp/1002.json index 227f3b4..cc63b48 100644 --- a/conf/corp/1002.json +++ b/conf/corp/1002.json @@ -2,7 +2,7 @@ "salary_latest": 14, "staff_config": [ { - "name": "用户角色", + "name": "员工角色", "key": "user_role", "type": "checkbox", "option": [{ @@ -12,22 +12,10 @@ "name": "财务", "value": "2" },{ - "name": "后勤", + "name": "人事", "value": "3" }] }, - { - "name": "性别", - "key": "user_sex", - "type": "radio", - "option": [{ - "name": "男", - "value": "1" - },{ - "name": "女", - "value": "2" - }] - }, { "name": "目标绩效", "key": "perft_target" diff --git a/server/controller/calculator.go b/server/controller/calculator.go index 30c96e2..c3d2c46 100644 --- a/server/controller/calculator.go +++ b/server/controller/calculator.go @@ -1,6 +1,7 @@ package controller import ( + "enterprise/common/config" "enterprise/common/dao" "enterprise/common/model" "enterprise/server/api" @@ -25,6 +26,9 @@ func (c *Calculator) Create(ctx *gin.Context) { session.CheckParamError(ctx.ShouldBindJSON(&req)) ctx.Keys[session.ContextRequest] = req + if !sess.GetAdmin().GetConfig().IsAdmin() { + panic(config.ErrParam.New()) + } calculator := new(model.SalaryCalculator) calculator.CorpId = sess.GetAdmin().CorpId calculator.Name = req.Name @@ -36,9 +40,13 @@ func (c *Calculator) Create(ctx *gin.Context) { // Update 更新已有的计算项 func (c *Calculator) Update(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) var req api.UpdateCalculatorReq session.CheckParamError(ctx.ShouldBindJSON(&req)) ctx.Keys[session.ContextRequest] = req + if !sess.GetAdmin().GetConfig().IsAdmin() { + panic(config.ErrParam.New()) + } calculator, err := dao.NewSalaryCalculatorDao().Get(cast.ToInt64(req.Id)) session.CheckDBError(err) session.CheckNilError(calculator, "计算项不存在") @@ -53,6 +61,10 @@ func (c *Calculator) Update(ctx *gin.Context) { // Delete 删除指定的计算项 func (c *Calculator) Delete(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if !sess.GetAdmin().GetConfig().IsAdmin() { + panic(config.ErrParam.New()) + } id := cast.ToInt64(ctx.Query("id")) session.CheckDBError(dao.NewSalaryCalculatorDao().Delete(id)) ctx.JSON(http.StatusOK, session.NewRspOk()) diff --git a/server/controller/salary.go b/server/controller/salary.go index ff6675e..720ebed 100644 --- a/server/controller/salary.go +++ b/server/controller/salary.go @@ -1,6 +1,7 @@ package controller import ( + "enterprise/common/config" "enterprise/server/api" "enterprise/server/service" "enterprise/server/session" @@ -17,6 +18,10 @@ func NewSalary() *Salary { } func (s *Salary) List(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsFinance() { + panic(config.ErrPriv.New()) + } var req api.ListSalaryReq session.CheckParamError(ctx.ShouldBind(&req)) total, summary, items := service.NewStaffSalary().List(ctx.Keys[session.ContextSession].(*session.AdminSession), &req) @@ -24,6 +29,10 @@ func (s *Salary) List(ctx *gin.Context) { } func (s *Salary) Create(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsFinance() { + panic(config.ErrPriv.New()) + } var req api.CreateSalaryReq session.CheckParamError(ctx.ShouldBind(&req)) ctx.Keys[session.ContextRequest] = req @@ -32,6 +41,10 @@ func (s *Salary) Create(ctx *gin.Context) { } func (s *Salary) Update(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsFinance() { + panic(config.ErrPriv.New()) + } var req api.UpdateSalaryReq session.CheckParamError(ctx.ShouldBind(&req)) ctx.Keys[session.ContextRequest] = req @@ -40,6 +53,10 @@ func (s *Salary) Update(ctx *gin.Context) { } func (s *Salary) Pay(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsFinance() { + panic(config.ErrPriv.New()) + } var req api.PaySalaryReq session.CheckParamError(ctx.ShouldBind(&req)) ctx.Keys[session.ContextRequest] = req @@ -48,6 +65,10 @@ func (s *Salary) Pay(ctx *gin.Context) { } func (s *Salary) Delete(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsFinance() { + panic(config.ErrPriv.New()) + } id := cast.ToInt64(ctx.Query("id")) service.NewStaffSalary().Delete(ctx.Keys[session.ContextSession].(*session.AdminSession), id) ctx.JSON(http.StatusOK, session.NewRspOk()) diff --git a/server/controller/staff.go b/server/controller/staff.go index 2af43fd..8d89b27 100644 --- a/server/controller/staff.go +++ b/server/controller/staff.go @@ -1,11 +1,11 @@ package controller import ( + "enterprise/common/config" "enterprise/common/dao" "enterprise/server/api" "enterprise/server/service" "enterprise/server/session" - "enterprise/worker" "github.com/gin-gonic/gin" "github.com/spf13/cast" "net/http" @@ -54,6 +54,10 @@ func (s *Staff) List(ctx *gin.Context) { func (s *Staff) Create(ctx *gin.Context) { sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsHr() { + panic(config.ErrPriv.New()) + } + var req api.StaffCreateReq session.CheckParamError(ctx.ShouldBindJSON(&req)) ctx.Keys[session.ContextRequest] = req @@ -61,18 +65,13 @@ func (s *Staff) Create(ctx *gin.Context) { ctx.JSON(http.StatusOK, session.NewRspOk()) } -func (s *Staff) Pay(ctx *gin.Context) { - sess := ctx.Keys[session.ContextSession].(*session.AdminSession) - var req api.StaffPayReq - session.CheckParamError(ctx.ShouldBindJSON(&req)) - ctx.Keys[session.ContextRequest] = req - service.NewStaffUser().Pay(sess, &req) - ctx.JSON(http.StatusOK, session.NewRspOk()) -} - func (s *Staff) Update(ctx *gin.Context) { - var req api.StaffUpdateReq sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsHr() { + panic(config.ErrPriv.New()) + } + var req api.StaffUpdateReq + session.CheckParamError(ctx.ShouldBindJSON(&req)) ctx.Keys[session.ContextRequest] = req service.NewStaffUser().Update(sess, &req) @@ -80,15 +79,30 @@ func (s *Staff) Update(ctx *gin.Context) { } func (s *Staff) Delete(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsHr() { + panic(config.ErrPriv.New()) + } + id := cast.ToInt64(ctx.Query("id")) session.CheckDBError(dao.NewStaffUserDao().Delete(id)) ctx.JSON(http.StatusOK, session.NewRspOk()) } -func (s *Staff) Salary(ctx *gin.Context) { - +func (s *Staff) Pay(ctx *gin.Context) { sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetAdmin().GetConfig().IsFinance() { + panic(config.ErrPriv.New()) + } + var req api.StaffPayReq + session.CheckParamError(ctx.ShouldBindJSON(&req)) + ctx.Keys[session.ContextRequest] = req + service.NewStaffUser().Pay(sess, &req) + ctx.JSON(http.StatusOK, session.NewRspOk()) +} +func (s *Staff) Salary(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) month := ctx.Query("month") tp := ctx.Query("type") @@ -113,12 +127,3 @@ func (s *Staff) Salary(ctx *gin.Context) { serv.Summary(corp.Id, month, ctx) } } - -func (s *Staff) SyncStaffSalary(ctx *gin.Context) { - corpId := cast.ToInt64(ctx.Query("cid")) - if corpId == 0 { - corpId = 1000 - } - go new(worker.Staff).SyncStaffSalary(corpId, "") - ctx.JSON(http.StatusOK, session.NewRspOk()) -} diff --git a/server/server.go b/server/server.go index 58e55ad..335451f 100644 --- a/server/server.go +++ b/server/server.go @@ -29,7 +29,6 @@ func initRoutge(engine *gin.Engine) { apiGroup.Any("/checkin/sync", controller.NewStaffCheckin().Sync) group.GET("/staff/salary", controller.NewStaff().Salary) - group.GET("/staff/sync/salary", controller.NewStaff().SyncStaffSalary) noTokenGroup.POST("/staff/login", controller.NewStaff().Login) apiGroup.GET("/staff/salary", controller.NewSalary().List)