🎨 新增用户&完善文章模块
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"git.echol.cn/loser/lckt/api/v1/category"
|
||||
"git.echol.cn/loser/lckt/api/v1/example"
|
||||
"git.echol.cn/loser/lckt/api/v1/system"
|
||||
"git.echol.cn/loser/lckt/api/v1/user"
|
||||
)
|
||||
|
||||
var ApiGroupApp = new(ApiGroup)
|
||||
@@ -16,4 +17,5 @@ type ApiGroup struct {
|
||||
CategoryApiGroup category.ApiGroup
|
||||
BotApiGroup bot.ApiGroup
|
||||
ArticleApiGroup article.ApiGroup
|
||||
UserApiGroup user.APPUserApi
|
||||
}
|
||||
|
7
api/v1/user/enter.go
Normal file
7
api/v1/user/enter.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package user
|
||||
|
||||
import "git.echol.cn/loser/lckt/service"
|
||||
|
||||
type ApiGroup struct{ APPUserApi }
|
||||
|
||||
var userService = service.ServiceGroupApp.UserServiceGroup.UserService
|
85
api/v1/user/user.go
Normal file
85
api/v1/user/user.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/lckt/global"
|
||||
r "git.echol.cn/loser/lckt/model/common/response"
|
||||
"git.echol.cn/loser/lckt/model/user/request"
|
||||
"git.echol.cn/loser/lckt/utils"
|
||||
"git.echol.cn/loser/lckt/utils/user_jwt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
type APPUserApi struct{}
|
||||
|
||||
// SendCode 发送验证码
|
||||
func (*APPUserApi) SendCode(ctx *gin.Context) {
|
||||
var p request.SendCodeReq
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误,发送验证码失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if err := userService.SendCode(p); err != nil {
|
||||
r.FailWithMessage("发送验证码失败", ctx)
|
||||
return
|
||||
}
|
||||
|
||||
r.OkWithMessage("发送验证码成功", ctx)
|
||||
}
|
||||
|
||||
// Login 用户登录
|
||||
func (*APPUserApi) Login(ctx *gin.Context) {
|
||||
var p request.CodeLoginReq
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误,登录失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
if result, _ := global.GVA_REDIS.Get(ctx, fmt.Sprintf("VerifyCode:%s", p.Phone)).Result(); result != p.Code {
|
||||
global.GVA_LOG.Error("验证码错误", zap.String("phone", p.Phone))
|
||||
r.FailWithMessage("验证码错误", ctx)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := userService.Login(p)
|
||||
if err != nil {
|
||||
r.FailWithMessage("登录失败", ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成token
|
||||
token, claims, err := user_jwt.LoginToken(user)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取token失败!", zap.Error(err))
|
||||
r.FailWithMessage("获取token失败", ctx)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = global.GVA_REDIS.Get(ctx, user.Phone).Result(); err == redis.Nil {
|
||||
// 此处过期时间等于jwt过期时间
|
||||
dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
timer := dr
|
||||
if err := global.GVA_REDIS.Set(ctx, user.Phone, token, timer).Err(); err != nil {
|
||||
global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
|
||||
r.FailWithMessage("设置登录状态失败", ctx)
|
||||
return
|
||||
}
|
||||
user_jwt.SetToken(ctx, token, int(claims.RegisteredClaims.ExpiresAt.Unix()-time.Now().Unix()))
|
||||
r.OkWithDetailed(gin.H{
|
||||
"User": user,
|
||||
"Token": token,
|
||||
"ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
|
||||
}, "登录成功", ctx)
|
||||
} else if err != nil {
|
||||
global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
|
||||
r.FailWithMessage("设置登录状态失败", ctx)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user