Files

82 lines
1.8 KiB
Go
Raw Normal View History

2021-09-29 16:43:40 +08:00
package controller
import (
"SciencesServer/app/handle"
"SciencesServer/utils"
"errors"
2022-02-11 11:02:29 +08:00
"fmt"
2021-09-29 16:43:40 +08:00
)
type Sms struct{}
type SmsHandle func() *Sms
2022-02-11 11:02:29 +08:00
type (
CaptchaCallback struct {
Token string `json:"token"`
EffectTime int `json:"effect_time"`
}
)
// tokenEffectTime Token有效期单位s
var tokenEffectTime = 5 * 60
func (c *Sms) captchaCallback(mobile, code string, callback func() error) error {
content := fmt.Sprintf("【中科云】您的验证码是%s三分钟内有效", code)
fmt.Println(content)
2021-09-29 16:43:40 +08:00
// 执行保存数据库
//mSysSmsLogs := model.NewSysSmsLogs()
//mSysSmsLogs.Mobile = mobile
//mSysSmsLogs.Content = content
//mSysSmsLogs.Usage = model2.SysSmsLogsUsageForCaptcha
//
//return model2.Create(mSysSmsLogs.SysSmsLogs)
2022-02-11 11:02:29 +08:00
// 发送短信
//_ = platform.NewSms().Send()(platform.SmsSendModeForGroup, &platform.SmsParam{
// Mobile: []string{mobile}, Content: content,
//})
if callback != nil {
//_ = callback()
}
2021-09-29 16:43:40 +08:00
return nil
}
// Captcha 验证码事件
func (c *Sms) Captcha(mobile string) error {
if !utils.ValidateMobile(mobile) {
return errors.New("手机格式不正确")
}
if err := handle.NewCaptcha().Sms(6, mobile, c.captchaCallback); err != nil {
2022-02-11 11:02:29 +08:00
return err
2021-09-29 16:43:40 +08:00
}
return nil
}
2022-02-11 11:02:29 +08:00
func (c *Sms) CaptchaValidate(mobile, captcha string) (*CaptchaCallback, error) {
if !utils.ValidateMobile(mobile) {
return nil, errors.New("手机格式不正确")
}
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: mobile,
Captcha: captcha,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("手机号或验证码错误")
}
return &CaptchaCallback{
Token: utils.JWTEncrypt(tokenEffectTime, map[string]interface{}{
"mobile": mobile,
}),
EffectTime: tokenEffectTime,
}, nil
}
2021-09-29 16:43:40 +08:00
func NewSms() SmsHandle {
return func() *Sms {
return &Sms{}
}
}