sms

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2021 License: MIT Imports: 12 Imported by: 0

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 表示一个短信中介

func NewAgency

func NewAgency(sender Sender) (Agency, error)

NewAgency 构造Sender

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) AsSender

func (s *AliyunSender) AsSender() Sender

AsSender 包装成Sender

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) IsValid

func (c Code) IsValid() bool

IsValid 验证码是否还有效

func (Code) MarshalBinary

func (c Code) MarshalBinary() (data []byte, err error)

MarshalBinary 实现encoding.BinaryMarshaler

func (Code) ReproduceRemain

func (c Code) ReproduceRemain() int64

ReproduceRemain 可再次生产需要的秒数

func (*Code) UnmarshalBinary

func (c *Code) UnmarshalBinary(data []byte) error

UnmarshalBinary 实现encoding.BinaryUnmarshaler

type ErrReproduceRemain

type ErrReproduceRemain int64

ErrReproduceRemain 表明重发需要等待

func (ErrReproduceRemain) Error

func (e ErrReproduceRemain) Error() string

Error 实现error

func (ErrReproduceRemain) Seconds

func (e ErrReproduceRemain) Seconds() int64

Seconds 返回需要等待的秒数

type FetchScene

type FetchScene func(sceneID string) (*Scene, error)

FetchScene 提取场景信息

func NewDBFetchScene

func NewDBFetchScene(db *gorm.DB) FetchScene

NewDBFetchScene 返回数据库实现的FetchScene

type FnSkip

type FnSkip func(sceneID string, number string, code string) bool

FnSkip 返回true时跳过检查

type GenCode

type GenCode func(codeLen int) string

GenCode 生成验证码

type GenCodeReplaceble

type GenCodeReplaceble interface {
	SetGenCode(genCode GenCode)
}

GenCodeReplaceble 可以设置生成验证码方法

type RawResult

type RawResult map[string]string

RawResult 是发送器未经加工的发送结果

type Result

type Result struct {
	Raw RawResult
}

Result 是发送结果

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 是数据库中存放短信验证码场景模型

func (SceneModel) TableName

func (SceneModel) TableName() string

TableName 指定表名

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 是短信发送队列

func (SendQueue) TableName

func (SendQueue) TableName() string

TableName 定义表名

type Sender

type Sender func(number, content string) (RawResult, error)

Sender 表示一个短信发送器

func NewDBSender

func NewDBSender(db *gorm.DB) Sender

NewDBSender 构造DB实现的Sender

type Skippable

type Skippable interface {
	SetFnSkip(fnSkip FnSkip)
}

Skippable 可以设置跳过验证的方法

type Storage

type Storage interface {
	// 获取 bool表示是否取到
	Get(key string) (*Code, bool)
	// 保存
	Set(key string, code *Code) error
	// 删除
	Delete(key string)
}

Storage 保存验证码数据

func NewRedisStorage

func NewRedisStorage(client *redis.Client) Storage

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL