utils.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. "context"
  6. "encoding/json"
  7. "fmt"
  8. "math"
  9. "math/rand"
  10. "property-company/parser"
  11. "property-company/pb"
  12. v1 "property-company/pb/v1"
  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 RobotMsg(content string) {
  230. mreq := v1.RobotMsgRequest{Content: content}
  231. ctx, cel := context.WithTimeout(context.Background(), time.Second*5)
  232. defer cel()
  233. pb.Thirdparty.RobotMsg(ctx, &mreq)
  234. }