123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package utils
- import (
- "crypto/hmac"
- "crypto/md5"
- "crypto/sha256"
- "encoding/base64"
- "encoding/hex"
- "fmt"
- "math"
- "math/rand"
- "regexp"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // 手机号,电子邮箱正则
- const (
- RegularMobile = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0-9])|(17[0-9])|(16[0-9])|(19[0-9]))\\d{8}$" //手机号码
- RegularEmail = `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
- )
- const TelAreaCodeHome = "86" // 大陆区号
- // 数据没有记录
- func ErrNoRow() string {
- return "<QuerySeter> no row found"
- }
- var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
- // ValidateMobileFormatat 校验手机格式
- func ValidateMobileFormatat(mobileNum string) bool {
- reg := regexp.MustCompile(RegularMobile)
- return reg.MatchString(mobileNum)
- }
- // ValidateEmailFormatat 校验邮箱格式
- func ValidateEmailFormatat(email string) bool {
- reg := regexp.MustCompile(RegularEmail)
- return reg.MatchString(email)
- }
- func MD5(data string) string {
- m := md5.Sum([]byte(data))
- return hex.EncodeToString(m[:])
- }
- func CheckPwd(pwd string) bool {
- compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
- reg := regexp.MustCompile(compile)
- flag := reg.MatchString(pwd)
- return flag
- }
- // 计算分页起始页
- func StartIndex(page, pagesize int) int {
- if page > 1 {
- return (page - 1) * pagesize
- }
- return 0
- }
- // GetLikeKeywordPars 获取sql查询中的参数切片
- func GetLikeKeywordPars(pars []interface{}, keyword string, num int) (newPars []interface{}) {
- newPars = pars
- if newPars == nil {
- newPars = make([]interface{}, 0)
- }
- for i := 1; i <= num; i++ {
- newPars = append(newPars, `%`+keyword+`%`)
- }
- return
- }
- // GetRandDigit 获取数字随机字符
- func GetRandDigit(n int) string {
- return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
- }
- func GetRandString(size int) string {
- allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
- randomSb := ""
- digitSize := len(allLetterDigit)
- for i := 0; i < size; i++ {
- randomSb += allLetterDigit[rnd.Intn(digitSize)]
- }
- return randomSb
- }
- // HmacSha256 计算HmacSha256
- // key 是加密所使用的key
- // data 是加密的内容
- func HmacSha256(key string, data string) []byte {
- mac := hmac.New(sha256.New, []byte(key))
- _, _ = mac.Write([]byte(data))
- return mac.Sum(nil)
- }
- // HmacSha256ToBase64 将加密后的二进制转Base64字符串
- func HmacSha256ToBase64(key string, data string) string {
- return base64.URLEncoding.EncodeToString(HmacSha256(key, data))
- }
- func GetSign(nonce, timestamp, appId, secret string) (sign string) {
- signStrMap := map[string]string{
- "nonce": nonce,
- "timestamp": timestamp,
- "appid": appId,
- }
- keys := make([]string, 0, len(signStrMap))
- for k := range signStrMap {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- var signStr string
- for _, k := range keys {
- signStr += k + "=" + signStrMap[k] + "&"
- }
- signStr = strings.Trim(signStr, "&")
- fmt.Println("signStr:" + signStr)
- sign = HmacSha256ToBase64(secret, signStr)
- return
- }
- func GetRandStringNoSpecialChar(size int) string {
- allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
- randomSb := ""
- digitSize := len(allLetterDigit)
- for i := 0; i < size; i++ {
- randomSb += allLetterDigit[rnd.Intn(digitSize)]
- }
- return randomSb
- }
- func StringsToJSON(str string) string {
- rs := []rune(str)
- jsons := ""
- for _, r := range rs {
- rint := int(r)
- if rint < 128 {
- jsons += string(r)
- } else {
- jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
- }
- }
- return jsons
- }
- func SecondsToHMS(seconds int64) string {
- duration := time.Duration(seconds) * time.Second
- hours := int64(duration.Hours())
- minutes := int64(duration.Minutes()) % 60
- secs := int64(duration.Seconds()) % 60
- var result string
- if hours > 0 {
- result += fmt.Sprintf("%d时", hours)
- }
- if minutes > 0 || (hours > 0 && secs > 0) {
- result += fmt.Sprintf("%d分", minutes)
- }
- if secs > 0 || (hours == 0 && minutes == 0) {
- result += fmt.Sprintf("%d秒", secs)
- }
- return result
- }
- func Unique[T comparable](slice []T) []T {
- seen := make(map[T]struct{})
- var unique []T
- for _, v := range slice {
- if _, exists := seen[v]; !exists {
- unique = append(unique, v)
- seen[v] = struct{}{}
- }
- }
- return unique
- }
- func GetOrmReplaceHolder(num int) string {
- var stringBuffer strings.Builder
- for i := 0; i < num; i++ {
- stringBuffer.WriteString("?")
- if i != num-1 {
- stringBuffer.WriteString(",")
- }
- }
- return stringBuffer.String()
- }
- func SetKeyExpireToday() time.Duration {
- now := time.Now()
- endOfDay := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
- return time.Until(endOfDay)
- }
|