Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalid 验证码不存在或已失效 ErrInvalid = errors.New("code is invalid") // ErrWrong 验证码错误 ErrWrong = errors.New("code is wrong") )
Functions ¶
This section is empty.
Types ¶
type Agency ¶
type Agency interface {
// Send 发送短信
// 无论是否发送成功,SMSResult都有值,成功是error是nil,否则有值
Send(number, content string) (*Result, error)
}
Agency 表示一个短信中介
type AliyunSender ¶
type AliyunSender struct {
// contains filtered or unexported fields
}
AliyunSender 是阿里云短信实现
func NewAliyunSender ¶
func NewAliyunSender(regionID, accessKeyID, accessSecret, signName string) (*AliyunSender, error)
NewAliyunSender 构造AliyunSender
func (*AliyunSender) Send ¶
func (s *AliyunSender) Send(number, tplCode, tplParam string) (*dysmsapi.SendSmsResponse, error)
Send 发送短信
type Code ¶
type Code struct {
// contains filtered or unexported fields
}
Code 是验证码
func (Code) MarshalBinary ¶
MarshalBinary 实现encoding.BinaryMarshaler
func (*Code) UnmarshalBinary ¶
UnmarshalBinary 实现encoding.BinaryUnmarshaler
type FetchScene ¶
FetchScene 提取场景信息
func NewDBFetchScene ¶
func NewDBFetchScene(db *gorm.DB) FetchScene
NewDBFetchScene 返回数据库实现的FetchScene
type GenCodeReplaceble ¶
type GenCodeReplaceble interface {
SetGenCode(genCode GenCode)
}
GenCodeReplaceble 可以设置生成验证码方法
type Scene ¶
type Scene struct {
// 验证码内容模板
Templdate string
// 有效期 秒数
ValidDuration int64
// 可重发的间隔 秒数
ReproduceInterval int64
// 验证码位数
CodeLen int
}
Scene 是场景信息
type SceneModel ¶
type SceneModel struct {
// 场景ID
SceneID string `gorm:"type:varchar(30);primay_key"`
// 模板 格式:您好,您的验证码是{{code}} 当前预设的变量只有code
Template string `gorm:"type:varchar(100);not null"`
// �有效时长,单位秒
ValidDuration int64 `gorm:"not null"`
// 可重发的时间间隔,单位秒
ReproduceInterval int64 `gorm:"not null"`
// 验证码的位数
CodeLen int `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
SceneModel 是数据库中存放短信验证码场景模型
type SendQueue ¶
type SendQueue struct {
ID uint `gorm:"primaryKey" json:"id" field:"id"`
CreatedAt time.Time `json:"createdAt" field:"created_at"`
UpdatedAt time.Time `json:"updatedAt" field:"updated_at"`
DeletedAt *time.Time `sql:"index" json:"deletedAt" field:"deleted_at"`
Number string `gorm:"type:varchar(20)" json:"number" field:"number"`
Content string `gorm:"type:varchar(255)" json:"content" field:"content"`
// 发送状态 0:待发送 1:发送成功 2:发送失败
Status byte `json:"status" field:"status"`
}
SendQueue 是短信发送队列
type Storage ¶
type Storage interface {
// 获取 bool表示是否取到
Get(key string) (*Code, bool)
// 保存
Set(key string, code *Code) error
// 删除
Delete(key string)
}
Storage 保存验证码数据
func NewRedisStorage ¶
NewRedisStorage 构造一个redis实现的Storage
type Validator ¶
type Validator interface {
// 生成验证码
// sceneID 代表一个场景
Produce(sceneID string, number string) (*Code, error)
// 校验验证码
// 成功校验后验证码即失效
Validate(sceneID string, number string, code string) error
}
Validator 是验证码校验器
func NewValidator ¶
func NewValidator(storage Storage, agency Agency, fetchScene FetchScene) Validator
NewValidator 构造Validator
Click to show internal directories.
Click to hide internal directories.