| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package utils
- import (
- "crypto/md5"
- "crypto/rand"
- "fmt"
- "math/big"
- "net/url"
- "regexp"
- "strconv"
- )
- // VerifyPhone 验证手机号是否正确
- func VerifyPhone(phone string) bool {
- // 示例:中国大陆手机号(1开头,11位)
- reg := regexp.MustCompile(`^1[3-9]\d{9}$`)
- return reg.MatchString(phone)
- }
- // GetFourRandomDigits 获取4位随机数字
- func 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)
- }
- // Md5Sign 使用MD5加密
- func Md5Sign(str string) string {
- data := []byte(str) //切片
- has := md5.Sum(data)
- md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制
- return md5str
- }
- // UrlEncode
- func UrlEncode(str string) string {
- return url.QueryEscape(str)
- }
|