// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package utils import ( "encoding/base64" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/util" "math" "math/rand" "property-household/parser" "regexp" "strconv" "strings" "time" ) var ( idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2} idCertVerifyCode = []byte("10X98765432") socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28} socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18, 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27, 'W': 28, 'X': 29, 'Y': 30} socialCreditMapKeys = []int{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'} socialCreditMapValues = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30} ) //单位证件 func CheckSocialCode(str string) (res bool) { defer func() { if r := recover(); r != nil { res = false } }() socialCode := strings.ToUpper(str) if len(socialCode) != 18 { return false } check := 0 lastLetter := 0 for index, value := range []byte(socialCode) { if index == 17 { lastLetter = int(value) break } if value >= 48 && value <= 57 { //fmt.Println((int(value)-48),socialCreditMatrix[index]) check = check + (int(value)-48)*socialCreditMatrix[index] } else { check = check + socialCreditMap[int(value)]*socialCreditMatrix[index] } } diff := 31 - check%31 keys := make([]int, 0) values := make([]int, 0) for k, v := range socialCreditMap { keys = append(keys, k) values = append(values, v) } verifyCode := socialCreditMapKeys[socialCreditMapValues[diff]] if verifyCode == lastLetter { return true } else { return false } } func CheckGATID(str string) bool { re := regexp.MustCompile(`^8[123]0000(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dX]$`) return re.Match([]byte(str)) } //身份证 func CheckIDCert(str string) (res bool) { defer func() { if r := recover(); r != nil { res = false } }() idCert := strings.ToUpper(str) if len(idCert) != 18 { return false } year, err := strconv.Atoi(idCert[6:10]) if err != nil { return false } if !(1900 < year && year < 2100) { return false } check := 0 lastLetter := 0 for index, value := range []byte(idCert) { if index == 17 { lastLetter = int(value) break } if !(value >= 48 && value <= 57) { return false } v := value - 48 check = check + idCertMatrix[index]*int(v) } if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') { return false } timeLayout := "20060102" //转化所需模板 loc, _ := time.LoadLocation("Local") //重要:获取时区 _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型 if err != nil { return false } verifyCode := int(idCertVerifyCode[check%11]) if lastLetter == verifyCode { return true } return false } func ToInt64Arrays(str string) []int64 { strs := make([]int64, 0) if str != "" && str != "[]" { json.Unmarshal([]byte(str), &strs) } return strs } //截取字符串 func SubString(str string, start, length int) string { rs := []rune(str) rl := len(rs) end := 0 if start < 0 { start = rl - 0 + start } end = start + length if start > end { start, end = end, start } if start < 0 { start = 0 } if start > rl { start = rl } if end < 0 { end = 0 } if end > rl { end = rl } return string(rs[start:end]) } // 判断time格式时间 func CheckDateTime(datetime time.Time) string { if !datetime.IsZero() { return datetime.Format("2006-01-02 15:04:05") } return "" } // 转换时间为时间戳 func ConvertFormatTime(s string) time.Time { loc, _ := time.LoadLocation("Local") //获取时区 tmp, _ := time.ParseInLocation("2006-01-02 15:04:05", s, loc) return tmp //转化为时间戳 类型是int64 } func StringToInt64(s string) int64 { res, _ := strconv.ParseInt(s, 10, 64) return res } func Round(val float64, places int) float64 { var t float64 f := math.Pow10(places) x := val * f if math.IsInf(x, 0) || math.IsNaN(x) { return val } if x >= 0.0 { t = math.Ceil(x) if (t - x) > 0.50000000001 { t -= 1.0 } } else { t = math.Ceil(-x) if (t + x) > 0.50000000001 { t -= 1.0 } t = -t } x = t / f if !math.IsInf(x, 0) { return x } return t } func StringToFloat64(str string) float64 { v, _ := strconv.ParseFloat(str, 64) return v } func GetIconUrl(msgType string) string { return fmt.Sprintf("%s/%s.png", parser.Conf.Oss.IconBucket, msgType) } const ( NUmStr = "0123456789" CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" SpecStr = "+=-@#~,.[]()!%^*$" ) func GenerateRandomStr(length int, charset string) string { time.Sleep(1 * time.Microsecond) rand.Seed(time.Now().UnixNano()) //初始化密码切片 var passwd []byte = make([]byte, length, length) //源字符串 var sourceStr string //判断字符类型,如果是数字 if charset == "num" { sourceStr = NUmStr //如果选的是字符 } else if charset == "char" { sourceStr = charset //如果选的是混合模式 } else if charset == "mix" { sourceStr = fmt.Sprintf("%s%s", NUmStr, CharStr) //如果选的是高级模式 } else if charset == "advance" { sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr) } else { sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr) } //遍历,生成一个随机index索引, for i := 0; i < length; i++ { index := rand.Intn(len(sourceStr)) passwd[i] = sourceStr[index] } return string(passwd) } const CRYPTO_KEY = "2D29BP43U3Y1B4N6REQW3F319DD23454" func CertEncrypt(cert string) string { if cert == "" { return "" } cryPasswd, _ := util.AesEncrypt(cert, CRYPTO_KEY) ret := base64.StdEncoding.EncodeToString(cryPasswd) return ret } func CertDecrypt(cert string) string { if cert == "" { return "" } bytes, err := base64.StdEncoding.DecodeString(cert) if err != nil { return "" } ret, err := util.AesDecrypt(bytes, []byte(CRYPTO_KEY)) if err != nil { return "" } return string(ret) } func StringJoin(data []string, elem string) string { ret := []string{} for _, v := range data { if v != "" { ret = append(ret, v) } } if len(ret) == 0 { return "" } return strings.Join(ret, elem) }