mirror of
https://github.com/yv1ing/gin-admin.git
synced 2025-10-24 10:12:05 +08:00
完成基本框架搭建及系统用户登录流程
This commit is contained in:
40
internal/service/system/user_service.go
Normal file
40
internal/service/system/user_service.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gin-admin/internal/auth"
|
||||
"gin-admin/internal/core/config"
|
||||
"gin-admin/internal/repository"
|
||||
"gin-admin/pkg/encrypt"
|
||||
"gorm.io/gorm"
|
||||
|
||||
systemrepository "gin-admin/internal/repository/system"
|
||||
)
|
||||
|
||||
// @Author: yv1ing
|
||||
// @Author: me@yvling.cn
|
||||
// @Date: 2025/8/28 16:30
|
||||
// @Desc: 系统用户服务实现
|
||||
|
||||
// SysUserLogin 系统用户登录
|
||||
func SysUserLogin(username, password string) (string, error) {
|
||||
user, err := systemrepository.GetUserByUsername(repository.Repo.DB, username)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return "", errors.New("系统内部错误")
|
||||
}
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) || encrypt.Sha256String(password, config.Config.SecretKey) != user.Password {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if !user.IsActive {
|
||||
return "", errors.New("用户已被禁用")
|
||||
}
|
||||
|
||||
jwtToken, err := auth.CreateAccessToken(user.ID, user.Username, 3600)
|
||||
if err != nil {
|
||||
return "", errors.New("系统内部错误")
|
||||
}
|
||||
|
||||
return jwtToken, nil
|
||||
}
|
||||
Reference in New Issue
Block a user