utils.go 6.6 KB

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