package utils import ( "crypto/rand" "math/big" "regexp" "strconv" "sync" ) // Utils 工具单例 type Utils struct{} var ( instance *Utils once sync.Once ) // GetUtils 获取单例实例 func GetUtils() *Utils { once.Do(func() { instance = &Utils{} }) return instance } // VerifyPhone 验证手机号是否正确 func (u *Utils) VerifyPhone(phone string) bool { // 示例:中国大陆手机号(1开头,11位) reg := regexp.MustCompile(`^1[3-9]\d{9}$`) return reg.MatchString(phone) } // GetFourRandomDigits 获取4位随机数字 func (u *Utils) GetFourRandomDigits() string { // 生成 [0, 8999] 的安全随机数 n, err := rand.Int(rand.Reader, big.NewInt(9000)) if err != nil { return "" } // 转换为 1000~9999 范围 randomNum := n.Int64() + 1000 return strconv.FormatInt(randomNum, 10) }