login
This commit is contained in:
parent
f7698ca4f3
commit
2947647c07
|
@ -49,3 +49,18 @@ func (d *CorpDao) Get(id int64) (*model.Corp, error) {
|
|||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (d *CorpDao) GetByHost(host string) (*model.Corp, error) {
|
||||
var u model.Corp
|
||||
tx := GetDB().Table(d.TableName())
|
||||
tx = tx.Where("host = ?", host)
|
||||
res := tx.First(&u)
|
||||
if res.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if res.Error != nil {
|
||||
return nil, res.Error
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ type CorpConfig struct {
|
|||
type Corp struct {
|
||||
Id int64
|
||||
Name string
|
||||
Host string
|
||||
Config string
|
||||
CreateTime int64
|
||||
UpdateTime int64
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package model
|
||||
|
||||
import "github.com/spf13/cast"
|
||||
|
||||
type StaffToken struct {
|
||||
Id int64
|
||||
UserId int64
|
||||
|
@ -7,3 +9,11 @@ type StaffToken struct {
|
|||
CreateTime int64
|
||||
ExpireTime int64
|
||||
}
|
||||
|
||||
func (t *StaffToken) ToLogin() map[string]interface{} {
|
||||
data := make(map[string]interface{})
|
||||
data["token"] = t.Token
|
||||
data["expire_time"] = cast.ToString(t.ExpireTime)
|
||||
data["user_id"] = cast.ToString(t.UserId)
|
||||
return data
|
||||
}
|
||||
|
|
|
@ -29,7 +29,9 @@ type UserConfig struct {
|
|||
type StaffUser struct {
|
||||
Id int64
|
||||
CorpId int64
|
||||
RoleId int64
|
||||
Username string
|
||||
Password string
|
||||
Realname string
|
||||
Phone string
|
||||
Idno string
|
||||
|
|
|
@ -1,111 +0,0 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"enterprise/common/config"
|
||||
"github.com/smbrave/goutil"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type BaseResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type CommonResponse struct {
|
||||
BaseResponse
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type RawResponse struct {
|
||||
BaseResponse
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
BaseResponse
|
||||
Data struct {
|
||||
Total int64 `json:"total"`
|
||||
TotalAmount string `json:"total_amount,omitempty"`
|
||||
TotalSize string `json:"total_size,omitempty"`
|
||||
Items interface{} `json:"items"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
rspOk *BaseResponse
|
||||
)
|
||||
|
||||
func init() {
|
||||
rspOk = new(BaseResponse)
|
||||
rspOk.Message = "OK"
|
||||
rspOk.Code = 0
|
||||
}
|
||||
|
||||
func NewRspOk() *BaseResponse {
|
||||
return rspOk
|
||||
}
|
||||
|
||||
func NewRawRsp(raw []byte) *RawResponse {
|
||||
rsp := &RawResponse{
|
||||
BaseResponse: BaseResponse{
|
||||
Code: rspOk.Code,
|
||||
Message: rspOk.Message,
|
||||
},
|
||||
}
|
||||
rsp.Data = raw
|
||||
return rsp
|
||||
}
|
||||
func NewListRsp(total int64, items interface{}) *ListResponse {
|
||||
rsp := &ListResponse{
|
||||
BaseResponse: BaseResponse{
|
||||
Code: rspOk.Code,
|
||||
Message: rspOk.Message,
|
||||
},
|
||||
}
|
||||
rsp.Data.Total = total
|
||||
rsp.Data.Items = items
|
||||
return rsp
|
||||
}
|
||||
|
||||
func NewListSizeRsp(total int64, totalSize int64, items interface{}) *ListResponse {
|
||||
rsp := &ListResponse{
|
||||
BaseResponse: BaseResponse{
|
||||
Code: rspOk.Code,
|
||||
Message: rspOk.Message,
|
||||
},
|
||||
}
|
||||
rsp.Data.Total = total
|
||||
rsp.Data.TotalSize = goutil.FormatBytes(totalSize)
|
||||
rsp.Data.Items = items
|
||||
return rsp
|
||||
}
|
||||
|
||||
func NewRsp(data interface{}) *CommonResponse {
|
||||
return &CommonResponse{
|
||||
BaseResponse: BaseResponse{
|
||||
Code: rspOk.Code,
|
||||
Message: rspOk.Message,
|
||||
},
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func CheckDBError(err error) {
|
||||
if err != nil {
|
||||
panic(config.ErrDb.New().Append(err))
|
||||
}
|
||||
}
|
||||
|
||||
func CheckParamError(err error) {
|
||||
if err != nil {
|
||||
panic(config.ErrParam.New().Append(err))
|
||||
}
|
||||
}
|
||||
|
||||
func CheckNilError(data interface{}, args ...string) {
|
||||
val := reflect.ValueOf(data)
|
||||
if val.IsNil() {
|
||||
panic(config.ErrNoData.New().Append(args))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package api
|
||||
|
||||
type StaffLoginReq struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
|
@ -4,7 +4,7 @@ import (
|
|||
"enterprise/common/config"
|
||||
"enterprise/common/dao"
|
||||
"enterprise/common/model"
|
||||
"enterprise/common/session"
|
||||
"enterprise/server/session"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gogap/errors"
|
||||
|
@ -37,6 +37,7 @@ func (c *Base) Before(ctx *gin.Context) {
|
|||
}
|
||||
|
||||
if header.Token == "" { // 新用户没有extra所以extra中的配置不能对新用户生效
|
||||
ctx.Keys[session.ContextSession] = session.NewAdminSession(&header, nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,9 @@ package controller
|
|||
import (
|
||||
"enterprise/common/config"
|
||||
"enterprise/common/dao"
|
||||
"enterprise/common/session"
|
||||
"enterprise/server/api"
|
||||
"enterprise/server/service"
|
||||
"enterprise/server/session"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cast"
|
||||
"net/http"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"enterprise/server/api"
|
||||
"enterprise/server/service"
|
||||
"enterprise/server/session"
|
||||
"enterprise/worker"
|
||||
|
@ -14,6 +15,13 @@ import (
|
|||
type Staff struct {
|
||||
}
|
||||
|
||||
func (s *Staff) Login(ctx *gin.Context) {
|
||||
var req api.StaffLoginReq
|
||||
session.CheckParamError(ctx.ShouldBindJSON(&req))
|
||||
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
|
||||
new(service.StaffUser).Login(sess, &req)
|
||||
}
|
||||
|
||||
func (s *Staff) Salary(ctx *gin.Context) {
|
||||
month := ctx.Query("month")
|
||||
tp := ctx.Query("type")
|
||||
|
|
|
@ -13,11 +13,10 @@ func initRoutge(engine *gin.Engine) {
|
|||
checkin := new(controller.StaffCheckin)
|
||||
apiGroup := engine.Group("/api")
|
||||
noTokenGroup := engine.Group("/api")
|
||||
group := engine.Group("/").Use(base.Before).Use(base.Recovery)
|
||||
|
||||
apiGroup.Use(base.Before).Use(base.Token)
|
||||
group := engine.Group("/")
|
||||
apiGroup.Use(base.Recovery)
|
||||
group.Use(base.Recovery)
|
||||
apiGroup.Use(base.Before).Use(base.Token).Use(base.Recovery)
|
||||
noTokenGroup.Use(base.Before).Use(base.Recovery)
|
||||
|
||||
noTokenGroup.Any("/qyweixin/approve/:cid", qyweixin.Approve)
|
||||
noTokenGroup.Any("/qyweixin/pay/:cid", qyweixin.Pay)
|
||||
|
@ -29,6 +28,7 @@ func initRoutge(engine *gin.Engine) {
|
|||
apiGroup.GET("/checkin", checkin.List)
|
||||
apiGroup.DELETE("/checkin", checkin.Delete)
|
||||
apiGroup.Any("/checkin/sync", checkin.Sync)
|
||||
noTokenGroup.POST("/login", staff.Login)
|
||||
|
||||
engine.LoadHTMLGlob("conf/template/*")
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@ package service
|
|||
import (
|
||||
"enterprise/common/config"
|
||||
"enterprise/common/dao"
|
||||
"enterprise/common/session"
|
||||
"enterprise/server/api"
|
||||
"enterprise/server/session"
|
||||
CS "enterprise/service"
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
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 StaffUser struct {
|
||||
}
|
||||
|
||||
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 != req.Password {
|
||||
panic("密码错误")
|
||||
}
|
||||
|
||||
token := new(model.StaffToken)
|
||||
token.UserId = user.Id
|
||||
token.ExpireTime = time.Now().Unix() + 86400
|
||||
token.Token = uuid.New().String()
|
||||
_, err = dao.NewStaffTokenDao().Create(token)
|
||||
session.CheckDBError(err)
|
||||
|
||||
return token.ToLogin()
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"enterprise/common/config"
|
||||
"enterprise/common/model"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -15,6 +17,7 @@ var (
|
|||
|
||||
type AdminHeader struct {
|
||||
Token string `header:"x-token" json:"-"`
|
||||
Host string `header:"x-host" json:"-"`
|
||||
RemoteIp string `header:"x-remote-ip" json:"public_ip"`
|
||||
}
|
||||
|
||||
|
@ -48,3 +51,22 @@ func (s *AdminSession) GetUsername() string {
|
|||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func CheckDBError(err error) {
|
||||
if err != nil {
|
||||
panic(config.ErrDb.New().Append(err))
|
||||
}
|
||||
}
|
||||
|
||||
func CheckParamError(err error) {
|
||||
if err != nil {
|
||||
panic(config.ErrParam.New().Append(err))
|
||||
}
|
||||
}
|
||||
|
||||
func CheckNilError(data interface{}, args ...string) {
|
||||
val := reflect.ValueOf(data)
|
||||
if val.IsNil() {
|
||||
panic(config.ErrNoData.New().Append(args))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue