102 lines
2.6 KiB
Go
Raw Normal View History

2023-06-04 22:54:54 +08:00
package apps
import (
2024-02-21 12:04:40 +08:00
"context"
2023-06-04 22:54:54 +08:00
"github.com/labstack/echo/v4"
2024-09-29 14:08:55 +08:00
"mylomen_server/common/constant"
"mylomen_server/common/dto"
"mylomen_server/common/utils"
"mylomen_server/infrastructure/repository"
"mylomen_server/service"
2023-06-04 22:54:54 +08:00
"net/http"
)
func InitUserGroup(g *echo.Group) {
2024-04-29 19:35:18 +08:00
//注册
2024-05-15 14:24:48 +08:00
g.POST("register", func(c echo.Context) error {
2024-09-29 21:12:57 +08:00
req := new(dto.RegisterReq)
2024-04-29 19:35:18 +08:00
if err := c.Bind(req); err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
2024-09-30 22:49:21 +08:00
platform := utils.GetPlatform(&c)
req.Platform = platform
2024-04-29 19:35:18 +08:00
2024-09-30 22:49:21 +08:00
err := service.UserBiz.Register(ctx, *req)
2024-04-29 19:35:18 +08:00
if err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
2024-05-15 13:53:27 +08:00
//登录
2024-09-30 22:49:21 +08:00
token, _ := service.UserBiz.Login(ctx, dto.LoginReq{
2024-05-15 13:53:27 +08:00
Account: req.Account,
Password: req.Password,
2024-09-30 22:49:21 +08:00
Platform: platform,
2024-05-15 13:53:27 +08:00
})
return c.JSON(http.StatusOK, utils.Ok(token))
2024-04-29 19:35:18 +08:00
})
//登录
2024-05-15 14:24:48 +08:00
g.POST("login", func(c echo.Context) error {
2024-09-29 21:12:57 +08:00
req := new(dto.LoginReq)
2023-06-04 22:54:54 +08:00
if err := c.Bind(req); err != nil {
2024-04-29 19:35:18 +08:00
return c.JSON(http.StatusOK, utils.Error(err))
2023-06-04 22:54:54 +08:00
}
2024-02-21 12:04:40 +08:00
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
2024-09-30 22:49:21 +08:00
//设置platform
req.Platform = utils.GetPlatform(&c)
token, err := service.UserBiz.Login(ctx, *req)
2023-06-04 22:54:54 +08:00
if err != nil {
2024-04-29 19:35:18 +08:00
return c.JSON(http.StatusOK, utils.Error(err))
2023-06-04 22:54:54 +08:00
}
return c.JSON(http.StatusOK, utils.Ok(token))
})
2024-04-29 19:35:18 +08:00
//发送 重置密码 验证码
2024-05-15 14:24:48 +08:00
g.POST("sendResetPwdCode", func(c echo.Context) error {
2024-04-29 19:35:18 +08:00
req := new(dto.SendResetPwdCodeReq)
if err := c.Bind(req); err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
2024-09-30 22:49:21 +08:00
err := service.UserBiz.SendResetPwdCode(context.Background(), req.Email)
2024-04-29 19:35:18 +08:00
if err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
return c.JSON(http.StatusOK, utils.Ok(true))
})
//重置密码
2024-05-15 14:24:48 +08:00
g.POST("resetPwd", func(c echo.Context) error {
2024-09-29 21:12:57 +08:00
req := new(dto.ResetPwdReq)
2024-04-29 19:35:18 +08:00
if err := c.Bind(req); err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
2024-09-30 22:49:21 +08:00
err := service.UserBiz.ResetPwd(ctx, *req)
2024-04-29 19:35:18 +08:00
if err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
return c.JSON(http.StatusOK, utils.Ok(true))
})
2024-05-15 15:02:12 +08:00
//根据 token 获取 登录信息
g.POST("findLoginResult", func(c echo.Context) error {
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
2024-09-30 22:49:21 +08:00
loginResult, err := service.UserBiz.GetLoginResult(ctx, &c)
if err != nil {
return c.JSON(http.StatusOK, utils.Error(err))
}
2024-05-15 15:02:12 +08:00
return c.JSON(http.StatusOK, utils.Ok(loginResult))
})
2023-06-04 22:54:54 +08:00
}