utils.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "property-thirdparty/parser"
  6. "encoding/json"
  7. "fmt"
  8. "math"
  9. "math/rand"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "unsafe"
  14. "regexp"
  15. )
  16. var (
  17. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  18. idCertVerifyCode = []byte("10X98765432")
  19. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  20. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  21. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  22. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  23. 'W': 28, 'X': 29, 'Y': 30}
  24. 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'}
  25. 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}
  26. )
  27. //单位证件
  28. func CheckSocialCode(str string) (res bool) {
  29. defer func() {
  30. if r := recover(); r != nil {
  31. res = false
  32. }
  33. }()
  34. socialCode := strings.ToUpper(str)
  35. if len(socialCode) != 18 {
  36. return false
  37. }
  38. check := 0
  39. lastLetter := 0
  40. for index, value := range []byte(socialCode) {
  41. if index == 17 {
  42. lastLetter = int(value)
  43. break
  44. }
  45. if value >= 48 && value <= 57 {
  46. //fmt.Println((int(value)-48),socialCreditMatrix[index])
  47. check = check + (int(value)-48)*socialCreditMatrix[index]
  48. } else {
  49. check = check + socialCreditMap[int(value)]*socialCreditMatrix[index]
  50. }
  51. }
  52. diff := 31 - check%31
  53. keys := make([]int, 0)
  54. values := make([]int, 0)
  55. for k, v := range socialCreditMap {
  56. keys = append(keys, k)
  57. values = append(values, v)
  58. }
  59. verifyCode := socialCreditMapKeys[socialCreditMapValues[diff]]
  60. if verifyCode == lastLetter {
  61. return true
  62. } else {
  63. return false
  64. }
  65. }
  66. //身份证
  67. func CheckIDCert(str string) (res bool) {
  68. defer func() {
  69. if r := recover(); r != nil {
  70. res = false
  71. }
  72. }()
  73. idCert := strings.ToUpper(str)
  74. if len(idCert) != 18 {
  75. return false
  76. }
  77. year, err := strconv.Atoi(idCert[6:10])
  78. if err != nil {
  79. return false
  80. }
  81. if !(1900 < year && year < 2100) {
  82. return false
  83. }
  84. check := 0
  85. lastLetter := 0
  86. for index, value := range []byte(idCert) {
  87. if index == 17 {
  88. lastLetter = int(value)
  89. break
  90. }
  91. if !(value >= 48 && value <= 57) {
  92. return false
  93. }
  94. v := value - 48
  95. check = check + idCertMatrix[index]*int(v)
  96. }
  97. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  98. return false
  99. }
  100. timeLayout := "20060102" //转化所需模板
  101. loc, _ := time.LoadLocation("Local") //重要:获取时区
  102. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  103. if err != nil {
  104. return false
  105. }
  106. verifyCode := int(idCertVerifyCode[check%11])
  107. if lastLetter == verifyCode {
  108. return true
  109. }
  110. return false
  111. }
  112. func ToInt64Arrays(str string) []int64 {
  113. strs := make([]int64, 0)
  114. if str != "" && str != "[]" {
  115. json.Unmarshal([]byte(str), &strs)
  116. }
  117. return strs
  118. }
  119. //截取字符串
  120. func SubString(str string, start, length int) string {
  121. rs := []rune(str)
  122. rl := len(rs)
  123. end := 0
  124. if start < 0 {
  125. start = rl - 0 + start
  126. }
  127. end = start + length
  128. if start > end {
  129. start, end = end, start
  130. }
  131. if start < 0 {
  132. start = 0
  133. }
  134. if start > rl {
  135. start = rl
  136. }
  137. if end < 0 {
  138. end = 0
  139. }
  140. if end > rl {
  141. end = rl
  142. }
  143. return string(rs[start:end])
  144. }
  145. // 判断time格式时间
  146. func CheckDateTime(datetime time.Time) string {
  147. if !datetime.IsZero() {
  148. return datetime.Format("2006-01-02 15:04:05")
  149. }
  150. return ""
  151. }
  152. // 转换时间为时间戳
  153. func ConvertFormatTime(s string) time.Time {
  154. loc, _ := time.LoadLocation("Local") //获取时区
  155. tmp, _ := time.ParseInLocation("2006-01-02 15:04:05", s, loc)
  156. return tmp //转化为时间戳 类型是int64
  157. }
  158. func StringToInt64(s string) int64 {
  159. res, _ := strconv.ParseInt(s, 10, 64)
  160. return res
  161. }
  162. func Round(val float64, places int) float64 {
  163. var t float64
  164. f := math.Pow10(places)
  165. x := val * f
  166. if math.IsInf(x, 0) || math.IsNaN(x) {
  167. return val
  168. }
  169. if x >= 0.0 {
  170. t = math.Ceil(x)
  171. if (t - x) > 0.50000000001 {
  172. t -= 1.0
  173. }
  174. } else {
  175. t = math.Ceil(-x)
  176. if (t + x) > 0.50000000001 {
  177. t -= 1.0
  178. }
  179. t = -t
  180. }
  181. x = t / f
  182. if !math.IsInf(x, 0) {
  183. return x
  184. }
  185. return t
  186. }
  187. func StringToFloat64(str string) float64 {
  188. v, _ := strconv.ParseFloat(str, 64)
  189. return v
  190. }
  191. func GetIconUrl(msgType string) string {
  192. return fmt.Sprintf("%s/%s.png", parser.Conf.Oss.IconBucket, msgType)
  193. }
  194. const (
  195. NUmStr = "0123456789"
  196. CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  197. SpecStr = "+=-@#~,.[]()!%^*$"
  198. )
  199. func GenerateRandomStr(length int, charset string) string {
  200. time.Sleep(1*time.Microsecond)
  201. rand.Seed(time.Now().UnixNano())
  202. //初始化密码切片
  203. var passwd []byte = make([]byte, length, length)
  204. //源字符串
  205. var sourceStr string
  206. //判断字符类型,如果是数字
  207. if charset == "num" {
  208. sourceStr = NUmStr
  209. //如果选的是字符
  210. } else if charset == "char" {
  211. sourceStr = charset
  212. //如果选的是混合模式
  213. } else if charset == "mix" {
  214. sourceStr = fmt.Sprintf("%s%s", NUmStr, CharStr)
  215. //如果选的是高级模式
  216. } else if charset == "advance" {
  217. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  218. } else {
  219. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  220. }
  221. //遍历,生成一个随机index索引,
  222. for i := 0; i < length; i++ {
  223. index := rand.Intn(len(sourceStr))
  224. passwd[i] = sourceStr[index]
  225. }
  226. return string(passwd)
  227. }
  228. // VerifyMobileFormat 手机号验证
  229. func VerifyMobileFormat(phone string) bool {
  230. 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}$"
  231. reg := regexp.MustCompile(regular)
  232. return reg.MatchString(phone)
  233. }
  234. // RankVCode 最小到最大值之间生成随机数
  235. func RankVCode(min int, max int) int {
  236. rand.Seed(time.Now().UnixNano())
  237. return rand.Intn(max-min) + min
  238. }
  239. // StrToBytes 快读的string转byte
  240. func StrToBytes(s string) []byte {
  241. x := (*[2]uintptr)(unsafe.Pointer(&s))
  242. h := [3]uintptr{x[0], x[1], x[1]}
  243. return *(*[]byte)(unsafe.Pointer(&h))
  244. }
  245. // StrToBytes 快速的byte转string
  246. func BytesToStr(b []byte) string {
  247. return *(*string)(unsafe.Pointer(&b))
  248. }
  249. //根据length获取随机字符串
  250. func RandomString(length int) string {
  251. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  252. bytes := []byte(str)
  253. result := []byte{}
  254. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  255. for i := 0; i < length; i++ {
  256. result = append(result, bytes[r.Intn(len(bytes))])
  257. }
  258. return string(result)
  259. }