- Add phone field to user model with index and helper methods - Implement SMS provider interface with UniSMS (合一短信) implementation - Add SMS verification code sending endpoint with rate limiting (1/60s) - Support SMS registration in Register() (mutually exclusive with email) - Add SMS configuration to admin settings (provider, keys, signature, template) - Display phone number in admin user list contact column - Add i18n translations for all SMS-related messages (zh-CN, en, zh-TW) - Add Claude Code skills: sync-upstream, migrate-server - Update CLAUDE.md with git conventions and deployment guide Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
38 lines
952 B
Go
38 lines
952 B
Go
package common
|
|
|
|
import (
|
|
"errors"
|
|
"regexp"
|
|
)
|
|
|
|
// SmsProvider defines the interface for SMS providers
|
|
type SmsProvider interface {
|
|
SendCode(phone string, code string) error
|
|
}
|
|
|
|
var smsProviders = map[string]SmsProvider{}
|
|
|
|
// RegisterSmsProvider registers an SMS provider by name
|
|
func RegisterSmsProvider(name string, provider SmsProvider) {
|
|
smsProviders[name] = provider
|
|
}
|
|
|
|
// SendSMS sends a verification code via the configured SMS provider
|
|
func SendSMS(phone string, code string) error {
|
|
if SMSProvider == "" {
|
|
return errors.New("SMS provider not configured")
|
|
}
|
|
provider, ok := smsProviders[SMSProvider]
|
|
if !ok {
|
|
return errors.New("unknown SMS provider: " + SMSProvider)
|
|
}
|
|
return provider.SendCode(phone, code)
|
|
}
|
|
|
|
var chinesePhoneRegex = regexp.MustCompile(`^1[3-9]\d{9}$`)
|
|
|
|
// IsValidPhone checks if the phone number is a valid Chinese mobile number
|
|
func IsValidPhone(phone string) bool {
|
|
return chinesePhoneRegex.MatchString(phone)
|
|
}
|