commit
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"ai_xhs/service"
|
||||
"ai_xhs/utils"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -184,6 +185,42 @@ func (ctrl *AuthController) XHSPhoneCodeLogin(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// SendSmsCode 发送阿里云短信验证码
|
||||
func (ctrl *AuthController) SendSmsCode(c *gin.Context) {
|
||||
var req struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, common.CodeInvalidParams, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 预检查:验证手机号是否存在于user表中
|
||||
if err := ctrl.authService.CheckPhoneExists(req.Phone); err != nil {
|
||||
common.Error(c, common.CodeServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 调用短信服务发送验证码
|
||||
smsService := service.GetSmsService()
|
||||
code, err := smsService.SendVerificationCode(req.Phone)
|
||||
if err != nil {
|
||||
common.Error(c, common.CodeServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 开发环境返回验证码,生产环境不返回
|
||||
responseData := gin.H{
|
||||
"sent_at": time.Now().Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
if config.AppConfig.Server.Mode == "dev" || config.AppConfig.Server.Mode == "debug" {
|
||||
responseData["code"] = code
|
||||
}
|
||||
|
||||
common.SuccessWithMessage(c, "验证码发送成功", responseData)
|
||||
}
|
||||
|
||||
// SendXHSVerificationCode 发送小红书手机号验证码
|
||||
func (ctrl *AuthController) SendXHSVerificationCode(c *gin.Context) {
|
||||
var req struct {
|
||||
|
||||
@@ -31,6 +31,7 @@ func SetupRouter(r *gin.Engine) {
|
||||
api.POST("/login/phone", authCtrl.PhoneLogin) // 手机号登录(测试用)
|
||||
api.POST("/login/phone-password", authCtrl.PhonePasswordLogin) // 手机号密码登录
|
||||
api.POST("/login/xhs-phone-code", authCtrl.XHSPhoneCodeLogin) // 小红书手机号验证码登录
|
||||
api.POST("/login/send-sms-code", authCtrl.SendSmsCode) // 发送阿里云短信验证码
|
||||
api.POST("/xhs/send-verification-code", authCtrl.SendXHSVerificationCode) // 发送小红书验证码
|
||||
api.POST("/logout", middleware.AuthMiddleware(), authCtrl.Logout) // 退出登录(需要认证)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user