feat: 添加短信验证码发送记录功能

This commit is contained in:
2025-12-23 23:47:39 +08:00
parent a125f15f58
commit 5709255e39
19 changed files with 668 additions and 14 deletions

View File

@@ -50,12 +50,20 @@ func Provide(opts ...opt.Option) error {
}); err != nil {
return err
}
if err := container.Container.Provide(func() (*smsCodeSends, error) {
obj := &smsCodeSends{}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func(
auth *auth,
medias *medias,
middlewares *middlewares.Middlewares,
orders *orders,
posts *posts,
smsCodeSends *smsCodeSends,
statistics *statistics,
uploads *uploads,
users *users,
@@ -66,6 +74,7 @@ func Provide(opts ...opt.Option) error {
middlewares: middlewares,
orders: orders,
posts: posts,
smsCodeSends: smsCodeSends,
statistics: statistics,
uploads: uploads,
users: users,

View File

@@ -30,6 +30,7 @@ type Routes struct {
medias *medias
orders *orders
posts *posts
smsCodeSends *smsCodeSends
statistics *statistics
uploads *uploads
users *users
@@ -149,6 +150,12 @@ func (r *Routes) Register(router fiber.Router) {
},
Body[PostForm]("form"),
))
// Register routes for controller: smsCodeSends
r.log.Debugf("Registering route: Get /admin/v1/sms-code-sends -> smsCodeSends.List")
router.Get("/admin/v1/sms-code-sends"[len(r.Path()):], DataFunc1(
r.smsCodeSends.List,
Query[dto.SmsCodeSendListQuery]("query"),
))
// Register routes for controller: statistics
r.log.Debugf("Registering route: Get /admin/v1/statistics -> statistics.statistics")
router.Get("/admin/v1/statistics"[len(r.Path()):], DataFunc0(

View File

@@ -0,0 +1,61 @@
package admin
import (
"quyun/v2/app/http/dto"
"quyun/v2/app/requests"
"quyun/v2/app/services"
"quyun/v2/database"
"quyun/v2/database/models"
"github.com/gofiber/fiber/v3"
)
// @provider
type smsCodeSends struct{}
// List
//
// @Summary 短信验证码发送记录
// @Tags Admin SMS
// @Produce json
// @Param query query dto.SmsCodeSendListQuery false "筛选条件"
// @Success 200 {object} requests.Pager{items=models.SmsCodeSend} "成功"
// @Router /admin/v1/sms-code-sends [get]
// @Bind query query
func (ctl *smsCodeSends) List(ctx fiber.Ctx, query *dto.SmsCodeSendListQuery) (*requests.Pager, error) {
if query.Pagination == nil {
query.Pagination = &requests.Pagination{}
}
query.Pagination.Format()
db := services.DB()
if db == nil {
return &requests.Pager{Pagination: *query.Pagination, Total: 0, Items: []*models.SmsCodeSend{}}, nil
}
q := db.WithContext(ctx.Context()).Model(&models.SmsCodeSend{})
if query.Phone != nil && *query.Phone != "" {
q = q.Where("phone LIKE ?", database.WrapLike(*query.Phone))
}
var total int64
if err := q.Count(&total).Error; err != nil {
return nil, err
}
var items []*models.SmsCodeSend
if err := q.
Order("sent_at desc").
Limit(int(query.Pagination.Limit)).
Offset(int(query.Pagination.Offset())).
Find(&items).Error; err != nil {
return nil, err
}
return &requests.Pager{
Pagination: *query.Pagination,
Total: total,
Items: items,
}, nil
}

View File

@@ -0,0 +1,10 @@
package dto
import "quyun/v2/app/requests"
type SmsCodeSendListQuery struct {
*requests.Pagination
Phone *string `query:"phone"`
}

View File

@@ -0,0 +1,6 @@
package services
import "gorm.io/gorm"
func DB() *gorm.DB { return _db }

View File

@@ -392,14 +392,25 @@ func (m *users) SendPhoneCode(ctx context.Context, phone string) error {
}
// 生成/覆盖验证码:同一手机号再次发送时以最新验证码为准
expiresAt := now.Add(5 * time.Minute)
m.codeByPhone[phone] = phoneCodeEntry{
code: code,
expiresAt: now.Add(5 * time.Minute),
expiresAt: expiresAt,
}
m.lastSentAtByPhone[phone] = now
// log phone and code
log.Infof("SendPhoneCode to %s: code=%s", phone, code)
if _db != nil {
// 记录短信验证码发送日志(用于后台审计与排查)。
_ = _db.WithContext(ctx).Create(&models.SmsCodeSend{
Phone: phone,
Code: code,
SentAt: now,
ExpiresAt: expiresAt,
}).Error
}
return nil
}

View File

@@ -0,0 +1,18 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE sms_code_sends(
id SERIAL8 PRIMARY KEY,
phone varchar(20) NOT NULL,
code varchar(20) NOT NULL,
sent_at timestamp NOT NULL DEFAULT now(),
expires_at timestamp NOT NULL
);
CREATE INDEX idx_sms_code_sends_phone ON sms_code_sends(phone);
CREATE INDEX idx_sms_code_sends_sent_at ON sms_code_sends(sent_at);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE sms_code_sends;
-- +goose StatementEnd

View File

@@ -0,0 +1,17 @@
package models
import "time"
const TableNameSmsCodeSend = "sms_code_sends"
// SmsCodeSend mapped from table <sms_code_sends>
type SmsCodeSend struct {
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id"`
Phone string `gorm:"column:phone;type:character varying(20);not null" json:"phone"`
Code string `gorm:"column:code;type:character varying(20);not null" json:"code"`
SentAt time.Time `gorm:"column:sent_at;type:timestamp without time zone;not null;default:now()" json:"sent_at"`
ExpiresAt time.Time `gorm:"column:expires_at;type:timestamp without time zone;not null" json:"expires_at"`
}
func (*SmsCodeSend) TableName() string { return TableNameSmsCodeSend }