package util import ( "crypto/md5" "encoding/hex" "math/rand" "ordonnance/logger" "regexp" "strconv" "strings" "time" ) const secret = "1a113ef6b61820daa5611c870ed8d5ee" var ( rr = rand.New(rand.NewSource(time.Now().UnixNano())) ) func Strip(res string) string { res = strings.Replace(res, " ", "", -1) res = strings.Replace(res, "\t", "", -1) res = strings.Replace(res, "\n", "", -1) res = strings.Replace(res, " ", "", -1) return res } func Md5(data string) string { c := md5.New() c.Write([]byte(data)) cipherStr := c.Sum(nil) return hex.EncodeToString(cipherStr) } func RandToken(userId int) string { timestamp := time.Now().Unix() userIdStr := strconv.Itoa(userId) return Md5("demo_"+strconv.FormatInt(timestamp, 16)) + userIdStr } func LogErr(tag string, err error) bool { if err != nil { logger.Infof("err %s %s", tag, err.Error()) return true } return false } func IsIdCard(idCard string) bool { checkStr := "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$" if res, err := regexp.Match(checkStr, []byte(idCard)); err == nil && res { return true } return false } func IsEmail(email string) bool { //pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱 pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$` reg := regexp.MustCompile(pattern) return reg.MatchString(email) } //mobile verify func IsPhone(mobileNum string) bool { regular := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$" reg := regexp.MustCompile(regular) return reg.MatchString(mobileNum) } func CurTime() string { return time.Now().Format("2006-01-02 15:04:05") } type Media struct { MediaId int CourseId int Duration int Xs int Play int } // type MediaUrl struct { MediaId int UserId int Time int } func GetInt(val interface{}) int { if val == nil || val == "" { return 0 } switch val.(type) { case int: return val.(int) case int64: return int(val.(int64)) case float32: return int(val.(float32)) case float64: return int(val.(float64)) case string: if mint, flag := strconv.Atoi(val.(string)); flag == nil { return mint } } return 0 } func JoinInts(list []int) string { res := "" for i, item := range list { if i > 0 { res += "," } res += strconv.Itoa(item) } return res } func GetBoolInt(val interface{}) int { if GetBool(val) { return 1 } else { return 0 } } func GetBool(val interface{}) bool { if val == nil || val == "" { return false } switch val.(type) { case int: return val.(int) > 0 case int64: return val.(int64) > 0 case float32: return val.(float32) > 0 case float64: return val.(float64) > 0 case string: return val != "" } return false } func ToInts(str string) []int { data := []int{} for _, item := range strings.Split(str, ",") { if val, err := strconv.Atoi(item); err == nil { data = append(data, val) } } return data } func sliceOutOfOrder(in []int) []int { l := len(in) for i := l - 1; i > 0; i-- { r := rr.Intn(i) in[r], in[i] = in[i], in[r] } return in } func FilterMap(list []int, data map[int]bool, minCount int) []int { nlist := []int{} slist := sliceOutOfOrder(list) gmap := make(map[int]bool) for _, id := range slist { // 80% 的概率剔除 if data[id] { continue } if len(nlist) > minCount { break } gmap[id] = true nlist = append(nlist, id) } for _, id := range slist { if len(nlist) > minCount { break } if gmap[id] { continue } nlist = append(nlist, id) } return nlist } func FilterList(list []int, data map[int]bool) []int { nlist := []int{} for _, id := range list { if data[id] { continue } nlist = append(nlist, id) } return nlist } // 生成随机数 func GenerateRandomNumber(start int, end int, count int) []int { if end < start || (end-start) < count { return nil } nums := make([]int, 0) r := rand.New(rand.NewSource(time.Now().UnixNano())) for len(nums) < count { num := r.Intn((end - start)) + start exist := false for _, v := range nums { if v == num { exist = true break } } if !exist { nums = append(nums, num) } } return nums } func RandIntList(data []int, count int) []int { lenData := len(data) if lenData <= count { return data } datalist := []int{} existMap := map[int]bool{} //自由随机 for i := 0; i < lenData; i++ { existMap[i] = true } // 利用 map无序 for index := range existMap { if count == 0 { break } count-- datalist = append(datalist, data[index]) } return datalist } func GetBirthday(cardId string) string { return cardId[6:10] + "-" + cardId[10:12] + "-" + cardId[12:14] }