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-04-29 19:35:18 +08:00
|
|
|
req := new(dto.ThirdRegisterReq)
|
|
|
|
if err := c.Bind(req); err != nil {
|
|
|
|
return c.JSON(http.StatusOK, utils.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
|
|
|
|
|
|
|
|
err := service.Login.Register(ctx, *req)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusOK, utils.Error(err))
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:53:27 +08:00
|
|
|
//登录
|
|
|
|
token, _ := service.Login.Login(ctx, dto.ThirdLoginReq{
|
|
|
|
Account: req.Account,
|
|
|
|
Password: req.Password,
|
|
|
|
})
|
|
|
|
|
|
|
|
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 {
|
2023-06-04 22:54:54 +08:00
|
|
|
req := new(dto.ThirdLoginReq)
|
|
|
|
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)
|
2023-06-04 22:54:54 +08:00
|
|
|
|
|
|
|
token, err := service.Login.Login(ctx, *req)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := service.Login.SendResetPwdCode(context.Background(), req.Account)
|
|
|
|
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-04-29 19:35:18 +08:00
|
|
|
req := new(dto.ThirdResetPwdReq)
|
|
|
|
if err := c.Bind(req); err != nil {
|
|
|
|
return c.JSON(http.StatusOK, utils.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db)
|
|
|
|
|
|
|
|
err := service.Login.ResetPwd(ctx, *req)
|
|
|
|
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)
|
|
|
|
loginResult := service.Login.GetLoginResult(ctx, &c)
|
|
|
|
return c.JSON(http.StatusOK, utils.Ok(loginResult))
|
|
|
|
})
|
2023-06-04 22:54:54 +08:00
|
|
|
}
|