2025-12-13 17:44:18 +08:00
|
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
|
|
"qnc-server/app/main/api/internal/logic/auth"
|
|
|
|
|
|
"qnc-server/app/main/api/internal/svc"
|
|
|
|
|
|
"qnc-server/app/main/api/internal/types"
|
|
|
|
|
|
"qnc-server/common/result"
|
|
|
|
|
|
"qnc-server/pkg/lzkit/validator"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func SendSmsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req types.SendSmsReq
|
|
|
|
|
|
if err := httpx.Parse(r, &req); err != nil {
|
|
|
|
|
|
result.ParamErrorResult(r, w, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := validator.Validate(req); err != nil {
|
|
|
|
|
|
result.ParamValidateErrorResult(r, w, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-02-28 11:35:55 +08:00
|
|
|
|
// 获取客户端真实 IP
|
|
|
|
|
|
clientIP := getClientIP(r)
|
2026-02-28 14:15:07 +08:00
|
|
|
|
// 获取 User-Agent 用于判断微信环境
|
|
|
|
|
|
userAgent := r.Header.Get("User-Agent")
|
2025-12-13 17:44:18 +08:00
|
|
|
|
l := auth.NewSendSmsLogic(r.Context(), svcCtx)
|
2026-02-28 14:15:07 +08:00
|
|
|
|
err := l.SendSms(&req, clientIP, userAgent)
|
2025-12-13 17:44:18 +08:00
|
|
|
|
result.HttpResult(r, w, nil, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-28 11:35:55 +08:00
|
|
|
|
|
|
|
|
|
|
// getClientIP 获取客户端真实 IP
|
|
|
|
|
|
func getClientIP(r *http.Request) string {
|
|
|
|
|
|
// 优先从 X-Forwarded-For 获取
|
|
|
|
|
|
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
|
|
|
|
|
// X-Forwarded-For 可能包含多个 IP,取第一个
|
|
|
|
|
|
return xff
|
|
|
|
|
|
}
|
|
|
|
|
|
// 其次从 X-Real-IP 获取
|
|
|
|
|
|
if xri := r.Header.Get("X-Real-IP"); xri != "" {
|
|
|
|
|
|
return xri
|
|
|
|
|
|
}
|
|
|
|
|
|
// 最后使用 RemoteAddr
|
|
|
|
|
|
return r.RemoteAddr
|
|
|
|
|
|
}
|