utils.go 5.9 KB

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