Feat: 登录接口实现
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-04-09 11:48:44 +08:00
parent 0df2aaa82f
commit 35801f5ee9
13 changed files with 160 additions and 39 deletions

View File

@@ -1,20 +0,0 @@
package controller
import (
"drive-linked/pkg/service"
"github.com/kataras/iris/v12"
)
func UserProfile(ctx iris.Context) {
serv := service.NewUsersService(ctx)
// 获取所有查询条件参数
var conditions map[string]interface{}
for _, field := range service.UserConditions {
if ctx.Params().GetString(field) != "" {
conditions[field] = ctx.Params().GetString(field)
}
}
serv.GetOneUser(&conditions)
ctx.Next()
}

View File

@@ -2,11 +2,14 @@ package service
import (
"database/sql"
"drive-linked/model"
"drive-linked/pkg/common"
"drive-linked/pkg/dto"
"drive-linked/pkg/model"
"drive-linked/pkg/serializer"
"drive-linked/pkg/utils"
"github.com/kataras/iris/v12"
"net/http"
"regexp"
)
type UsersService struct {
@@ -26,7 +29,7 @@ func (serv *UsersService) GetOneUser(conditions *map[string]interface{}) {
resp := dto.NewResponse(serv.Ctx)
user := &model.User{}
err = user.GetWithConditions(conditions)
err = user.GetProfileWithConditions(conditions)
switch err {
case nil:
@@ -38,7 +41,46 @@ func (serv *UsersService) GetOneUser(conditions *map[string]interface{}) {
}
resp.Success(userResult)
case sql.ErrNoRows:
resp.Error(serializer.ErrNoUser, "找不到此用户")
resp.Error(serializer.ErrNoUser, "用户不存在")
default:
resp.Error(http.StatusInternalServerError, "获取用户信息失败,数据库异常")
}
}
func (serv *UsersService) Login(loginParams dto.LoginParams) {
var err error
resp := dto.NewResponse(serv.Ctx)
// 登录逻辑
// 判断账号类型 邮箱/用户名
var method string
emailExp := regexp.MustCompile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
if emailExp.Match([]byte(loginParams.Account)) {
method = model.LoginMethodEmail
} else {
method = model.LoginMethodName
}
// 从数据库取出原密码
userLogin := model.Login{}
err = userLogin.GetLoginInfo(loginParams.Account, method)
switch err {
case nil:
// 检查密码
match, _ := utils.CheckPasswd(loginParams.Password, userLogin.Password)
if match {
// 登录成功签发token
sToken, _ := common.NewShortToken(userLogin.Name)
rToken, _ := common.NewRefreshToken(userLogin.Name)
resp.Success(serializer.LoginResponse{
Token: sToken,
RefreshToken: rToken,
})
} else {
resp.ErrBadAccPasswd()
}
case sql.ErrNoRows:
resp.Error(serializer.ErrNoUser, "用户不存在")
default:
resp.Error(http.StatusInternalServerError, "获取用户信息失败,数据库异常")
}