|
|
@@ -1,6 +1,7 @@
|
|
|
package verifycode
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
|
|
|
@@ -8,7 +9,8 @@ import (
|
|
|
"github.com/runningwater/gohub/pkg/config"
|
|
|
"github.com/runningwater/gohub/pkg/helpers"
|
|
|
"github.com/runningwater/gohub/pkg/logger"
|
|
|
- "github.com/runningwater/gohub/pkg/sms"
|
|
|
+ "github.com/runningwater/gohub/pkg/mail"
|
|
|
+ // "github.com/runningwater/gohub/pkg/sms"
|
|
|
)
|
|
|
|
|
|
type VerifyCode struct {
|
|
|
@@ -41,11 +43,42 @@ func (v *VerifyCode) SendSMS(phone string) bool {
|
|
|
// 测试环境,直接返回成功
|
|
|
return true
|
|
|
}
|
|
|
+ logger.DebugJSON("验证码", "发送验证码", map[string]string{phone: code})
|
|
|
+ return true
|
|
|
// 发送验证码
|
|
|
- return sms.NewSMS().Send(phone, sms.Message{
|
|
|
- Template: config.GetString("sms.aliyun.template_code"),
|
|
|
- Data: map[string]string{"code": code},
|
|
|
+ // return sms.NewSMS().Send(phone, sms.Message{
|
|
|
+ // Template: config.GetString("sms.aliyun.template_code"),
|
|
|
+ // Data: map[string]string{"code": code},
|
|
|
+ // })
|
|
|
+}
|
|
|
+
|
|
|
+// SendEmail 发送验证码
|
|
|
+// 发送验证码到邮箱, 调用示例
|
|
|
+//
|
|
|
+// verifycode.NewVerifyCode().SendEmail(request.Email)
|
|
|
+func (v *VerifyCode) SendEmail(email string) error {
|
|
|
+ // 生成验证码
|
|
|
+ code := v.generateVerifyCode(email)
|
|
|
+
|
|
|
+ // 方便本地和 测试环境调试
|
|
|
+ if !app.IsProduction() &&
|
|
|
+ strings.HasSuffix(email, config.GetString("verifycode.debug_email_suffix")) {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ content := fmt.Sprintf("<p>您的验证码是:%v</p>", code)
|
|
|
+ // 发送邮件
|
|
|
+ mail.NewMailer().Send(mail.Email{
|
|
|
+ From: mail.From{
|
|
|
+ Address: config.GetString("mail.from.address"),
|
|
|
+ Name: config.GetString("mail.from.name"),
|
|
|
+ },
|
|
|
+ To: []string{email},
|
|
|
+ Subject: "Email 验证码",
|
|
|
+ HTML: []byte(content),
|
|
|
})
|
|
|
+
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// CheckAnswer 检查验证码是否正确
|