commit
This commit is contained in:
@@ -2,7 +2,10 @@ package controller
|
||||
|
||||
import (
|
||||
"ai_xhs/common"
|
||||
"ai_xhs/config"
|
||||
"ai_xhs/service"
|
||||
"ai_xhs/utils"
|
||||
"context"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -98,3 +101,137 @@ func (ctrl *AuthController) PhoneLogin(c *gin.Context) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// PhonePasswordLogin 手机号密码登录
|
||||
func (ctrl *AuthController) PhonePasswordLogin(c *gin.Context) {
|
||||
var req struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, common.CodeInvalidParams, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 调用手机号密码登录服务
|
||||
token, employee, err := ctrl.authService.PhonePasswordLogin(req.Phone, req.Password)
|
||||
if err != nil {
|
||||
common.Error(c, common.CodeServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户显示名称(优先使用真实姓名,其次用户名)
|
||||
displayName := employee.RealName
|
||||
if displayName == "" {
|
||||
displayName = employee.Username
|
||||
}
|
||||
|
||||
common.SuccessWithMessage(c, "登录成功", gin.H{
|
||||
"token": token,
|
||||
"employee": gin.H{
|
||||
"id": employee.ID,
|
||||
"name": displayName,
|
||||
"username": employee.Username,
|
||||
"real_name": employee.RealName,
|
||||
"phone": employee.Phone,
|
||||
"role": employee.Role,
|
||||
"enterprise_id": employee.EnterpriseID,
|
||||
"enterprise_name": employee.EnterpriseName,
|
||||
"is_bound_xhs": employee.IsBoundXHS,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// XHSPhoneCodeLogin 小红书手机号验证码登录
|
||||
func (ctrl *AuthController) XHSPhoneCodeLogin(c *gin.Context) {
|
||||
var req struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, common.CodeInvalidParams, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 调用手机号验证码登录服务
|
||||
token, employee, err := ctrl.authService.XHSPhoneCodeLogin(req.Phone, req.Code)
|
||||
if err != nil {
|
||||
common.Error(c, common.CodeServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户显示名称(优先使用真实姓名,其次用户名)
|
||||
displayName := employee.RealName
|
||||
if displayName == "" {
|
||||
displayName = employee.Username
|
||||
}
|
||||
|
||||
common.SuccessWithMessage(c, "登录成功", gin.H{
|
||||
"token": token,
|
||||
"employee": gin.H{
|
||||
"id": employee.ID,
|
||||
"name": displayName,
|
||||
"username": employee.Username,
|
||||
"real_name": employee.RealName,
|
||||
"phone": employee.Phone,
|
||||
"role": employee.Role,
|
||||
"enterprise_id": employee.EnterpriseID,
|
||||
"enterprise_name": employee.EnterpriseName,
|
||||
"is_bound_xhs": employee.IsBoundXHS,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// SendXHSVerificationCode 发送小红书手机号验证码
|
||||
func (ctrl *AuthController) SendXHSVerificationCode(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
|
||||
}
|
||||
|
||||
// 开发环境返回验证码,生产环境不返回
|
||||
response := gin.H{
|
||||
"message": "验证码已发送,5分钟内有效",
|
||||
}
|
||||
|
||||
if config.AppConfig.Server.Mode == "debug" {
|
||||
response["code"] = code // 仅开发环境返回
|
||||
}
|
||||
|
||||
common.SuccessWithMessage(c, "验证码已发送", response)
|
||||
}
|
||||
|
||||
// Logout 退出登录(删除Redis中的Token)
|
||||
func (ctrl *AuthController) Logout(c *gin.Context) {
|
||||
employeeID := c.GetInt("employee_id")
|
||||
|
||||
// 从Redis删除token
|
||||
ctx := context.Background()
|
||||
if err := utils.RevokeToken(ctx, employeeID); err != nil {
|
||||
// 即使删除失败也返回成功,因为token有过期时间
|
||||
common.SuccessWithMessage(c, "退出成功", nil)
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessWithMessage(c, "退出成功", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user