49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// Package controller 控制器层
|
|
package controller
|
|
|
|
import (
|
|
"server/common"
|
|
apiDto "server/modules/api/dto"
|
|
"server/modules/user/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type OpenAuthController struct {
|
|
userService *service.UserService
|
|
}
|
|
|
|
func NewOpenAuthController() *OpenAuthController {
|
|
return &OpenAuthController{userService: service.NewUserService()}
|
|
}
|
|
|
|
func (ctrl *OpenAuthController) RegisterRoutes(r *gin.RouterGroup) {
|
|
group := r.Group("/open")
|
|
group.POST("/user/login", ctrl.LoginByPhone)
|
|
}
|
|
|
|
// LoginByPhone 手机号密码登录
|
|
// @Summary 手机号密码登录
|
|
// @Tags 对外接口
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body dto.UserPasswordLoginRequest true "登录信息"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /open/user/login [post]
|
|
func (ctrl *OpenAuthController) LoginByPhone(c *gin.Context) {
|
|
var req apiDto.UserPasswordLoginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.Error(c, 400, "手机号和密码不能为空")
|
|
return
|
|
}
|
|
loginUser, token, err := ctrl.userService.LoginByPhonePassword(req.Phone, req.Password)
|
|
if err != nil {
|
|
common.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
common.Success(c, gin.H{
|
|
"token": token,
|
|
"user": loginUser,
|
|
})
|
|
}
|