You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.6 KiB
81 lines
2.6 KiB
package login
|
|
|
|
import (
|
|
"epur-pay/pkg/aliyun"
|
|
"epur-pay/pkg/dapi"
|
|
"epur-pay/pkg/logger"
|
|
"epur-pay/pkg/redis"
|
|
"errors"
|
|
"fmt"
|
|
redigo "github.com/gomodule/redigo/redis"
|
|
)
|
|
|
|
type SsoSendSmsCodeParams struct {
|
|
Mobile string `json:"mobile"`
|
|
}
|
|
|
|
// SsoSendSmsCode 发送短信验证码接口
|
|
// @Summary 发送短信验证码
|
|
// @Description 该接口用于发送短信验证码,限制验证码的发送频率和每天发送的次数。
|
|
// @Tags sms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body SsoSendSmsCodeParams true "发送短信验证码的请求参数"
|
|
// @Success 200 {object} dapi.ResponseCommon "短信验证码发送成功"
|
|
// @Router /api/v1/login/captcha [post]
|
|
func SsoSendSmsCode(a *dapi.ApiBase, params *SsoSendSmsCodeParams) error {
|
|
redisCodeKey := fmt.Sprintf("smscode:%s", params.Mobile)
|
|
redisDailyKey := fmt.Sprintf("smscode_daily:%s", params.Mobile)
|
|
|
|
conn := redis.RPool.Get()
|
|
defer func() {
|
|
if err := conn.Close(); err != nil {
|
|
logger.ErrorLogger.Infoln(err.Error())
|
|
}
|
|
}()
|
|
|
|
code, err := conn.Do("GET", redisCodeKey)
|
|
if err == nil && code != nil {
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_sent_frequently"))
|
|
}
|
|
|
|
dailyCount, err := redigo.Int(conn.Do("GET", redisDailyKey))
|
|
if err != nil && !errors.Is(err, redigo.ErrNil) {
|
|
logger.ErrorLogger.Errorln("获取验证码次数失败:", err.Error())
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_fetch_limit_failed"))
|
|
}
|
|
|
|
if errors.Is(err, redigo.ErrNil) {
|
|
dailyCount = 0
|
|
}
|
|
|
|
if dailyCount >= 10 {
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_daily_limit"))
|
|
}
|
|
|
|
captcha, err := aliyun.SendSmsLogin(params.Mobile)
|
|
if err != nil {
|
|
logger.ErrorLogger.Errorln("发送短信失败:", err.Error())
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_send_failed"))
|
|
}
|
|
|
|
if _, err := conn.Do("SETEX", redisCodeKey, 300, captcha); err != nil {
|
|
logger.ErrorLogger.Errorln("设置验证码60s时效失败:", err.Error())
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_cache_failed"))
|
|
}
|
|
|
|
if dailyCount == 0 {
|
|
if _, err := conn.Do("SETEX", redisDailyKey, 86400, 1); err != nil {
|
|
logger.ErrorLogger.Errorln("设置一天最大发送验证码次数失败:", err.Error())
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_daily_record_failed"))
|
|
}
|
|
} else {
|
|
if _, err := conn.Do("INCR", redisDailyKey); err != nil {
|
|
logger.ErrorLogger.Errorln("验证码发送次数记录失败:", err.Error())
|
|
return a.ReturnPublicErrorResponse(a.Translate("sms_code_daily_record_failed"))
|
|
}
|
|
}
|
|
|
|
return a.ReturnSuccessCustomResponse(a.NewSuccessResponseCommon())
|
|
}
|