This commit is contained in:
jiangyong 2025-06-16 15:48:50 +08:00
parent 16fcc9a6c7
commit acc93e4a2c
9 changed files with 161 additions and 40 deletions

68
common/dao/corp_user.go Normal file
View File

@ -0,0 +1,68 @@
package dao
import (
"enterprise/common/model"
"errors"
"gorm.io/gorm"
"time"
)
type CorpUserDao struct {
}
func NewCorpUserDao() *CorpUserDao {
return &CorpUserDao{}
}
func (d *CorpUserDao) TableName() string {
return "corp_user"
}
func (d *CorpUserDao) Create(o *model.CorpUser) (int64, error) {
o.CreateTime = time.Now().Unix()
res := GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *CorpUserDao) Update(o *model.CorpUser) error {
o.UpdateTime = time.Now().Unix()
tx := GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *CorpUserDao) Delete(id int64) error {
res := GetDB().Table(d.TableName()).Delete(&model.CorpUser{}, id)
return res.Error
}
func (d *CorpUserDao) Get(id int64) (*model.CorpUser, error) {
var u model.CorpUser
tx := GetDB().Table(d.TableName())
tx = tx.Where("id = ?", id)
res := tx.First(&u)
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func (d *CorpUserDao) GetByUsername(corpId int64, username string) (*model.CorpUser, error) {
var u model.CorpUser
tx := GetDB().Table(d.TableName())
tx = tx.Where("corp_id = ?", corpId)
tx = tx.Where("username = ?", username)
res := tx.First(&u)
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}

12
common/model/corp_user.go Normal file
View File

@ -0,0 +1,12 @@
package model
type CorpUser struct {
Id int64
CorpId int64
Username string
Password string
Realname string
Config string
CreateTime int64
UpdateTime int64
}

6
server/api/corp_user.go Normal file
View File

@ -0,0 +1,6 @@
package api
type CorpLoginReq struct {
Username string `json:"username"`
Password string `json:"password"`
}

View File

@ -0,0 +1,24 @@
package controller
import (
"enterprise/server/api"
"enterprise/server/service"
"enterprise/server/session"
"github.com/gin-gonic/gin"
"net/http"
)
type CorpUser struct {
}
func NewCorpUser() *CorpUser {
return &CorpUser{}
}
func (s *CorpUser) Login(ctx *gin.Context) {
var req api.CorpLoginReq
session.CheckParamError(ctx.ShouldBindJSON(&req))
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
data := service.NewCorpUser().Login(sess, &req)
ctx.JSON(http.StatusOK, session.NewRsp(data))
}

View File

@ -20,14 +20,6 @@ func NewStaff() *Staff {
return &Staff{}
}
func (s *Staff) Login(ctx *gin.Context) {
var req api.StaffLoginReq
session.CheckParamError(ctx.ShouldBindJSON(&req))
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
data := service.NewStaffUser().Login(sess, &req)
ctx.JSON(http.StatusOK, session.NewRsp(data))
}
func (s *Staff) Suggest(ctx *gin.Context) {
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
staffs, _, err := dao.NewStaffUserDao().Query(1, -1, sess.GetCorpId(), 0, "", "", "", "")

View File

@ -29,7 +29,8 @@ func initRoutge(engine *gin.Engine) {
apiGroup.Any("/checkin/sync", controller.NewStaffCheckin().Sync)
group.GET("/staff/salary", controller.NewStaff().Salary)
noTokenGroup.POST("/staff/login", controller.NewStaff().Login)
noTokenGroup.POST("/staff/login", controller.NewCorpUser().Login) //todo delete
noTokenGroup.POST("/corp/login", controller.NewCorpUser().Login)
noTokenGroup.Any("/sync/salary", controller.NewSalary().SyncStaffSalary)
apiGroup.GET("/staff/salary", controller.NewSalary().List)

View File

@ -0,0 +1,48 @@
package service
import (
"enterprise/common/dao"
"enterprise/common/model"
"enterprise/server/api"
"enterprise/server/session"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"time"
)
type CorpUser struct {
}
func NewCorpUser() *CorpUser {
return &CorpUser{}
}
func (s *CorpUser) Login(sess *session.AdminSession, req *api.CorpLoginReq) interface{} {
host := sess.GetHeader().Host
log.Infof("login host[%s] req[%s]", host, goutil.EncodeJSON(req))
corp, err := dao.NewCorpDao().GetByHost(host)
session.CheckDBError(err)
session.CheckNilError(corp, "企业不存在的域名")
user, err := dao.NewCorpUserDao().GetByUsername(corp.Id, req.Username)
session.CheckDBError(err)
session.CheckNilError(user, "企业用户不存在")
if user.Password == "" || user.Password != req.Password {
panic("密码错误")
}
token := new(model.StaffToken)
token.UserId = user.Id
token.ExpireTime = time.Now().Unix() + 86400*30
token.Token = uuid.New().String()
_, err = dao.NewStaffTokenDao().Create(token)
session.CheckDBError(err)
data := token.ToLogin()
data["username"] = user.Username
data["realname"] = user.Realname
return data
}

View File

@ -9,11 +9,9 @@ import (
"enterprise/server/session"
CommonService "enterprise/service"
"git.u8t.cn/open/gosdk/qyweixin"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"time"
)
type StaffUser struct {
@ -23,35 +21,6 @@ func NewStaffUser() *StaffUser {
return &StaffUser{}
}
func (s *StaffUser) Login(sess *session.AdminSession, req *api.StaffLoginReq) interface{} {
host := sess.GetHeader().Host
log.Infof("login host[%s] req[%s]", host, goutil.EncodeJSON(req))
corp, err := dao.NewCorpDao().GetByHost(host)
session.CheckDBError(err)
session.CheckNilError(corp, "企业不存在的域名")
user, err := dao.NewStaffUserDao().GetByUsername(corp.Id, req.Username)
session.CheckDBError(err)
session.CheckNilError(user, "企业用户不存在")
if user.Password == "" || user.Password != req.Password {
panic("密码错误")
}
token := new(model.StaffToken)
token.UserId = user.Id
token.ExpireTime = time.Now().Unix() + 86400*30
token.Token = uuid.New().String()
_, err = dao.NewStaffTokenDao().Create(token)
session.CheckDBError(err)
data := token.ToLogin()
data["username"] = user.Username
data["realname"] = user.Realname
return data
}
func (s *StaffUser) List(sess *session.AdminSession, req *api.StaffListReq) (int64, interface{}) {
staffs, total, err := dao.NewStaffUserDao().Query(req.Page, req.Size, sess.GetCorpId(), cast.ToInt(req.Status), req.Username, req.Realname, req.Phone, req.Idno)

View File

@ -58,6 +58,7 @@ func InitCorp1002(cron *gocron.Scheduler) {
staff.PayStaffSalary(corpId, time.Now().AddDate(0, -1, 0).Format("200601"))
staff.SendStaffSalaryBill(corpId, time.Now().AddDate(0, -1, 0).Format("200601"))
})
}
func InitCorp1000(cron *gocron.Scheduler) {