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.
54 lines
1.6 KiB
54 lines
1.6 KiB
package user
|
|
|
|
import (
|
|
"yunyan/comm"
|
|
"yunyan/pb"
|
|
"yunyan/sys/email"
|
|
"yunyan/sys/sms"
|
|
"fmt"
|
|
"time"
|
|
|
|
"golang.org/x/exp/rand"
|
|
)
|
|
|
|
// @Summary 发送验证码
|
|
// @Description 发送验证码
|
|
// @Tags User
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param user body pb.UserVerificationReq true "发送验证吗请求"
|
|
// @Success 200 {object} comm.HttpResult{data=pb.UserVerificationResp} "成功返回"
|
|
// @Router /api/home/user_verification [post]
|
|
func (this *apiComp) Verification(session comm.IUserSession, req *pb.UserVerificationReq) (resp *pb.UserVerificationResp, errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
)
|
|
rand.Seed(uint64(time.Now().UnixNano()))
|
|
verificationCode := fmt.Sprintf("%d", rand.Intn(9000)+1000) // Generates a random 4-digit number
|
|
this.module.model.saveCode(req.Addr, verificationCode)
|
|
switch req.Vtype {
|
|
case 0: //邮箱
|
|
if err = email.SendMail("Sign-in Verification Code", fmt.Sprintf("<html><body><p>Your verification code is: <strong>%s</strong></p><p>For your account security, please do not share this code with anyone.</p></body></html>", verificationCode), req.Addr); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
case 1: //手机
|
|
if err = sms.SendCaptcha(req.Addr, verificationCode); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
default:
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Message: "vtype is no support",
|
|
}
|
|
}
|
|
resp = &pb.UserVerificationResp{}
|
|
return
|
|
}
|
|
|