mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: User login
This commit is contained in:
22
service/user/common.go
Normal file
22
service/user/common.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SetSession 设置session
|
||||
func SetSession(c *gin.Context, list map[string]interface{}) {
|
||||
s := sessions.Default(c)
|
||||
for key, value := range list {
|
||||
s.Set(key, value)
|
||||
}
|
||||
s.Save()
|
||||
}
|
||||
|
||||
// ClearSession 清空session
|
||||
func ClearSession(c *gin.Context) {
|
||||
s := sessions.Default(c)
|
||||
s.Clear()
|
||||
s.Save()
|
||||
}
|
||||
55
service/user/login.go
Normal file
55
service/user/login.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"cloudreve/models"
|
||||
"cloudreve/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UserLoginService 管理用户登录的服务
|
||||
type UserLoginService struct {
|
||||
//TODO 细致调整验证规则
|
||||
UserName string `form:"userName" json:"userName" binding:"required,email"`
|
||||
Password string `form:"Password" json:"Password" binding:"required,min=4,max=64"`
|
||||
CaptchaCode string `form:"captchaCode" json:"captchaCode"`
|
||||
}
|
||||
|
||||
// Login 用户登录函数
|
||||
func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
|
||||
isCaptchaRequired := model.GetSettingByName("login_captcha")
|
||||
expectedUser, err := model.GetUserByEmail(service.UserName)
|
||||
|
||||
if model.IsTrueVal(isCaptchaRequired) {
|
||||
// TODO 验证码校验
|
||||
}
|
||||
|
||||
// 一系列校验
|
||||
if err != nil {
|
||||
return serializer.Err(401, "用户邮箱或密码错误", err)
|
||||
}
|
||||
if authOK, _ := expectedUser.CheckPassword(service.Password); !authOK {
|
||||
return serializer.Err(401, "用户邮箱或密码错误", nil)
|
||||
}
|
||||
if expectedUser.Status == model.Baned {
|
||||
return serializer.Err(403, "该账号已被封禁", nil)
|
||||
}
|
||||
if expectedUser.Status == model.NotActivicated {
|
||||
return serializer.Err(403, "该账号未激活", nil)
|
||||
}
|
||||
|
||||
if expectedUser.TwoFactor != "" {
|
||||
//TODO 二步验证处理
|
||||
}
|
||||
|
||||
//登陆成功,清空并设置session
|
||||
ClearSession(c)
|
||||
SetSession(c, map[string]interface{}{
|
||||
"user_id": expectedUser.ID,
|
||||
})
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Msg: "",
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"cloudreve/models"
|
||||
"cloudreve/pkg/serializer"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UserLoginService 管理用户登录的服务
|
||||
type UserLoginService struct {
|
||||
//TODO 细致调整验证规则
|
||||
UserName string `form:"userName" json:"userName" binding:"required,min=5,max=30"`
|
||||
Password string `form:"Password" json:"Password" binding:"required,min=8,max=40"`
|
||||
CaptchaCode string `form:"captchaCode" json:"captchaCode"`
|
||||
}
|
||||
|
||||
// Login 用户登录函数
|
||||
func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
|
||||
siteName := model.GetSettingByName("siteName")
|
||||
basic := model.GetSettingByNames([]string{"siteDes", "siteKeywords"})
|
||||
fmt.Println(basic)
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Msg: siteName,
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user