46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
package handle
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/cloudwego/hertz/pkg/app"
|
||
)
|
||
|
||
type LoginRequest struct {
|
||
WXCode string `json:"wx_code,omitempty"`
|
||
Phone string `json:"phone"`
|
||
Captcha string `json:"captcha,omitempty"`
|
||
Password string `json:"password,omitempty"`
|
||
}
|
||
|
||
type Response[T any] struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
Data T `json:"data"`
|
||
}
|
||
type LoginResponse struct {
|
||
Token string `json:"token"`
|
||
Expire string `json:"expire"`
|
||
Message string `json:"message"`
|
||
Code int `json:"code"`
|
||
}
|
||
|
||
// Authenticator 登录
|
||
// @Tags 用户模块
|
||
// @Summary 登录接口
|
||
// @Description 如果是微信登录,需要传递 WXcode和手机号,然后调用微信接口获取 openid 和 unionid。
|
||
// 如果 unionid 已经存在,就直接登录, 如果不存在,就注册。
|
||
// 如果是手机号登录,需要传递 phone 和 password, 如果手机号没注册,就直接注册
|
||
// @Param request body LoginRequest true "请求参数"
|
||
// @Success 200 {object} LoginResponse "成功响应"
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Router /api/v1/login [post]
|
||
func (h *UserHandler) Authenticator(ctx context.Context, c *app.RequestContext) (interface{}, error) {
|
||
var req LoginRequest
|
||
if err := c.BindJSON(&req); err != nil {
|
||
return nil, err
|
||
}
|
||
return h.userService.Login(ctx, req.Phone, req.Password, req.WXCode, req.Captcha)
|
||
}
|