mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-24 15:52:15 +08:00
43 lines
737 B
Go
43 lines
737 B
Go
package auth
|
|
|
|
import (
|
|
"errors"
|
|
"gofiber.study.skcks.cn/common/errorx"
|
|
"gofiber.study.skcks.cn/global"
|
|
"gofiber.study.skcks.cn/model/dto"
|
|
"gofiber.study.skcks.cn/model/generic/models"
|
|
)
|
|
|
|
var (
|
|
Failed = errors.New("账号或密码错误")
|
|
)
|
|
|
|
type Service struct {
|
|
}
|
|
|
|
var Services *Service
|
|
|
|
func InitService() {
|
|
Services = &Service{}
|
|
}
|
|
|
|
func (s *Service) Login(login *dto.Login) (token string, err error) {
|
|
user := &models.User{Account: login.Account, Password: login.Password}
|
|
exist, err := global.DataSources.Get(user)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if !exist {
|
|
return token, Failed
|
|
}
|
|
|
|
token, err = global.GetToken(global.UserClaims{
|
|
Id: user.Id,
|
|
Account: user.Account,
|
|
})
|
|
|
|
err = errorx.ParseError(err)
|
|
return
|
|
}
|