utils.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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-garden/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. //身份证
  68. func CheckIDCert(str string) (res bool) {
  69. defer func() {
  70. if r := recover(); r != nil {
  71. res = false
  72. }
  73. }()
  74. idCert := strings.ToUpper(str)
  75. if len(idCert) != 18 {
  76. return false
  77. }
  78. year, err := strconv.Atoi(idCert[6:10])
  79. if err != nil {
  80. return false
  81. }
  82. if !(1900 < year && year < 2100) {
  83. return false
  84. }
  85. check := 0
  86. lastLetter := 0
  87. for index, value := range []byte(idCert) {
  88. if index == 17 {
  89. lastLetter = int(value)
  90. break
  91. }
  92. if !(value >= 48 && value <= 57) {
  93. return false
  94. }
  95. v := value - 48
  96. check = check + idCertMatrix[index]*int(v)
  97. }
  98. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  99. return false
  100. }
  101. timeLayout := "20060102" //转化所需模板
  102. loc, _ := time.LoadLocation("Local") //重要:获取时区
  103. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  104. if err != nil {
  105. return false
  106. }
  107. verifyCode := int(idCertVerifyCode[check%11])
  108. if lastLetter == verifyCode {
  109. return true
  110. }
  111. return false
  112. }
  113. func ToInt64Arrays(str string) []int64 {
  114. strs := make([]int64, 0)
  115. if str != "" && str != "[]" {
  116. json.Unmarshal([]byte(str), &strs)
  117. }
  118. return strs
  119. }
  120. //截取字符串
  121. func SubString(str string, start, length int) string {
  122. rs := []rune(str)
  123. rl := len(rs)
  124. end := 0
  125. if start < 0 {
  126. start = rl - 0 + start
  127. }
  128. end = start + length
  129. if start > end {
  130. start, end = end, start
  131. }
  132. if start < 0 {
  133. start = 0
  134. }
  135. if start > rl {
  136. start = rl
  137. }
  138. if end < 0 {
  139. end = 0
  140. }
  141. if end > rl {
  142. end = rl
  143. }
  144. return string(rs[start:end])
  145. }
  146. // 判断time格式时间
  147. func CheckDateTime(datetime time.Time) string {
  148. if !datetime.IsZero() {
  149. return datetime.Format("2006-01-02 15:04:05")
  150. }
  151. return ""
  152. }
  153. // 转换时间为时间戳
  154. func ConvertFormatTime(s string) time.Time {
  155. loc, _ := time.LoadLocation("Local") //获取时区
  156. tmp, _ := time.ParseInLocation("2006-01-02 15:04:05", s, loc)
  157. return tmp //转化为时间戳 类型是int64
  158. }
  159. func StringToInt64(s string) int64 {
  160. res, _ := strconv.ParseInt(s, 10, 64)
  161. return res
  162. }
  163. func Round(val float64, places int) float64 {
  164. var t float64
  165. f := math.Pow10(places)
  166. x := val * f
  167. if math.IsInf(x, 0) || math.IsNaN(x) {
  168. return val
  169. }
  170. if x >= 0.0 {
  171. t = math.Ceil(x)
  172. if (t - x) > 0.50000000001 {
  173. t -= 1.0
  174. }
  175. } else {
  176. t = math.Ceil(-x)
  177. if (t + x) > 0.50000000001 {
  178. t -= 1.0
  179. }
  180. t = -t
  181. }
  182. x = t / f
  183. if !math.IsInf(x, 0) {
  184. return x
  185. }
  186. return t
  187. }
  188. func StringToFloat64(str string) float64 {
  189. v, _ := strconv.ParseFloat(str, 64)
  190. return v
  191. }
  192. func GetIconUrl(msgType string) string {
  193. return fmt.Sprintf("%s/%s.png", parser.Conf.Oss.IconBucket, msgType)
  194. }
  195. const (
  196. NUmStr = "0123456789"
  197. CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  198. SpecStr = "+=-@#~,.[]()!%^*$"
  199. )
  200. func GenerateRandomStr(length int, charset string) string {
  201. time.Sleep(1 * time.Microsecond)
  202. rand.Seed(time.Now().UnixNano())
  203. //初始化密码切片
  204. var passwd []byte = make([]byte, length, length)
  205. //源字符串
  206. var sourceStr string
  207. //判断字符类型,如果是数字
  208. if charset == "num" {
  209. sourceStr = NUmStr
  210. //如果选的是字符
  211. } else if charset == "char" {
  212. sourceStr = charset
  213. //如果选的是混合模式
  214. } else if charset == "mix" {
  215. sourceStr = fmt.Sprintf("%s%s", NUmStr, CharStr)
  216. //如果选的是高级模式
  217. } else if charset == "advance" {
  218. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  219. } else {
  220. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  221. }
  222. //遍历,生成一个随机index索引,
  223. for i := 0; i < length; i++ {
  224. index := rand.Intn(len(sourceStr))
  225. passwd[i] = sourceStr[index]
  226. }
  227. return string(passwd)
  228. }
  229. func GetGardenCode(gardenId int64) string {
  230. return fmt.Sprintf("%d", gardenId)
  231. }
  232. const GardenDbPrefix = "db_garden"
  233. func GetGardenDbName(gardenId int64) string {
  234. code := GetGardenCode(gardenId)
  235. return fmt.Sprintf("%s_%s", GardenDbPrefix, code)
  236. }
  237. func VerifyMobileFormat(phone string) bool {
  238. 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}$"
  239. reg := regexp.MustCompile(regular)
  240. return reg.MatchString(phone)
  241. }
  242. const CRYPTO_KEY = "2D29BP43U3Y1B4N6REQW3F319DD23454"
  243. func CertEncrypt(cert string) string {
  244. cryPasswd, _ := util.AesEncrypt(cert, CRYPTO_KEY)
  245. ret := base64.StdEncoding.EncodeToString(cryPasswd)
  246. return ret
  247. }
  248. func CertDecrypt(cert string) string {
  249. if cert == "" {
  250. return ""
  251. }
  252. bytes, err := base64.StdEncoding.DecodeString(cert)
  253. if err != nil {
  254. return ""
  255. }
  256. ret, err := util.AesDecrypt(bytes, []byte(CRYPTO_KEY))
  257. if err != nil {
  258. return ""
  259. }
  260. return string(ret)
  261. }
  262. func StringJoin(data []string, elem string) string {
  263. ret := []string{}
  264. for _, v := range data {
  265. if v != "" {
  266. ret = append(ret, v)
  267. }
  268. }
  269. if len(ret) == 0 {
  270. return ""
  271. }
  272. return strings.Join(ret, elem)
  273. }