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("

Your verification code is: %s

For your account security, please do not share this code with anyone.

", 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 }