2025-04-30 15:46:14 +08:00

65 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package domain
import (
"admin/apps/user/domain/entity"
"admin/apps/user/model"
"admin/internal/errcode"
"admin/lib/passlib"
"time"
)
func (svc *CommonResourceService) EnsureInitDB() error {
return svc.userRepo.CreateAdminUsers()
}
func (svc *CommonResourceService) Login(user, pwd string) (*entity.User, error) {
userEt, find, err := svc.userRepo.GetByUser(user)
if err != nil {
return nil, err
}
if !find {
return nil, errcode.New(errcode.UserOrPassInValid, "")
}
if !passlib.ComparePassword(userEt.Po.UserPass, pwd) {
return nil, errcode.New(errcode.UserOrPassInValid, "")
}
return userEt, nil
}
func (svc *CommonResourceService) GetUserById(userId int) (*entity.User, bool, error) {
et, find, err := svc.userRepo.GetById(userId)
if err != nil {
return et, false, err
}
return et, find, nil
}
func (svc *CommonResourceService) GetOrCreateToken(userId int) (*model.Token, error) {
tokenInfo, find, err := svc.tokenRepo.GetTokenByUserId(userId)
if err != nil {
return nil, err
}
if !find {
tokenInfo, err = svc.tokenRepo.CreateToken(userId, time.Hour*24*15) // 15天过期的token可以使用中加上token刷新功能
if err != nil {
return nil, err
}
}
return tokenInfo, nil
}
func (svc *CommonResourceService) GetToken(userId int) (*model.Token, error) {
tokenInfo, find, err := svc.tokenRepo.GetTokenByUserId(userId)
if err != nil {
return nil, err
}
if !find {
return tokenInfo, errcode.New(errcode.TokenInvalid, "user %v token not found", userId)
}
return tokenInfo, nil
}