后端: Go(Gin+GORM+MySQL+Redis) - 17张业务表(yz_pw_前缀), 自动迁移 - JWT认证(3小时) + 盐+MD5密码 + 图形验证码 - 班级CRUD/加入(8人姓名验证/邀请码)/审核/30天自动清理 - 系统配置(16项)/菜单管理(动态路由)/数据统计 - 文件MD5去重/数据隔离/账号封禁/敏感词DFA检测 前端: Vue3+Vite+Element Plus+Less+ECharts+FontAwesome - 动态路由(数据库菜单驱动) - 蓝白配色, H5响应式 - 登录/注册/忘记密码/个人中心 - 班级创建向导/详情/加入/列表 - 管理后台: 审核/配置/菜单/统计/用户/敏感词 数据库: MySQL 10.31.100.3:3306/photowall 管理员: hero920103 / 920103
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package sms
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"photowall/internal/config"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// 短信验证码封装
|
|
// 参数未配置时返回 ErrNotConfigured,开发阶段可在日志中输出验证码
|
|
|
|
var ErrNotConfigured = errors.New("短信服务未配置,请联系管理员")
|
|
|
|
type codeItem struct {
|
|
code string
|
|
expireAt time.Time
|
|
}
|
|
|
|
var (
|
|
store = sync.Map{} // phone -> codeItem
|
|
)
|
|
|
|
// SendCode 发送短信验证码
|
|
// 开发阶段(未配置短信服务商):验证码输出到日志,方便测试
|
|
func SendCode(phone string) (string, error) {
|
|
cfg := config.C
|
|
code := generateCode()
|
|
|
|
if cfg.SMSAccessKey == "" || cfg.SMSSecretKey == "" || cfg.SMSSignName == "" || cfg.SMSTemplateCode == "" {
|
|
// 未配置短信服务商,开发模式:日志输出验证码
|
|
fmt.Printf("[SMS-DEV] 手机号 %s 的验证码: %s (5分钟有效)\n", phone, code)
|
|
store.Store(phone, codeItem{code: code, expireAt: time.Now().Add(5 * time.Minute)})
|
|
return code, nil
|
|
}
|
|
|
|
// TODO: 接入阿里云/腾讯云短信 SDK
|
|
// 阿里云:dysmsapi.Client + SendSmsRequest
|
|
// 腾讯云:sms.Client + SendSms
|
|
store.Store(phone, codeItem{code: code, expireAt: time.Now().Add(5 * time.Minute)})
|
|
return code, nil
|
|
}
|
|
|
|
// VerifyCode 校验短信验证码(一次性)
|
|
func VerifyCode(phone, code string) bool {
|
|
if phone == "" || code == "" {
|
|
return false
|
|
}
|
|
v, ok := store.Load(phone)
|
|
if !ok {
|
|
return false
|
|
}
|
|
item := v.(codeItem)
|
|
store.Delete(phone)
|
|
if time.Now().After(item.expireAt) {
|
|
return false
|
|
}
|
|
return item.code == code
|
|
}
|
|
|
|
func generateCode() string {
|
|
b := make([]byte, 6)
|
|
for i := range b {
|
|
n, _ := rand.Int(rand.Reader, big.NewInt(10))
|
|
b[i] = byte('0' + n.Int64())
|
|
}
|
|
return string(b)
|
|
}
|