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.

77 lines
2.1 KiB

package customer
import (
"regexp"
"time"
"shop-api/model/request"
"shop-api/model/response"
"shop-api/service"
"shop-api/utils"
"github.com/gin-gonic/gin"
)
// SendMessage
// @Tags tools
// @Summary 发送短信验证码[v1.0.0]
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SendMessage true "phone,type"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /base/sendMessage [post]
func SendMessage(c *gin.Context) {
var (
err error
params request.SendMessage
)
_ = c.ShouldBindJSON(&params)
if err = utils.Verify(params, utils.SendMessageVerity); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
//校验手机号格式
if ok, _ := regexp.MatchString(utils.RegPhoneNumber, params.Phone); !ok {
response.FailWithMessage("手机号码格式不合法", c)
return
}
if err = utils.SendSms(params.Phone, params.CountryCode, params.Type); err != nil {
response.FailWithMessage("发送失败,"+err.Error(), c)
return
}
response.OkWithMessage("发送成功", c)
}
// SendMessage
// @Tags tools
// @Summary 发送邮件验证码[v1.0.0]
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SendEmail true "phone,type"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /base/sendEmail [post]
func SendEmail(c *gin.Context) {
var (
err error
params request.SendEmail
code string
)
_ = c.ShouldBindJSON(&params)
if err = utils.Verify(params, utils.SendEmailVerify); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
//校验邮箱格式
if ok, _ := regexp.MatchString(utils.RegEmailNumber, params.Email); !ok {
response.FailWithMessage("邮箱格式不合法", c)
return
}
if code, err = utils.SendEmail(params.Email, "1"); err != nil {
response.FailWithMessage("发送失败,"+err.Error(), c)
return
}
_ = service.RedisSet("email_code:"+params.Email, code, 5*time.Minute)
response.OkWithMessage("发送成功", c)
}