util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package util
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "math/rand"
  6. "ordonnance/logger"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. const secret = "1a113ef6b61820daa5611c870ed8d5ee"
  13. var (
  14. rr = rand.New(rand.NewSource(time.Now().UnixNano()))
  15. )
  16. func Strip(res string) string {
  17. res = strings.Replace(res, " ", "", -1)
  18. res = strings.Replace(res, "\t", "", -1)
  19. res = strings.Replace(res, "\n", "", -1)
  20. res = strings.Replace(res, " ", "", -1)
  21. return res
  22. }
  23. func Md5(data string) string {
  24. c := md5.New()
  25. c.Write([]byte(data))
  26. cipherStr := c.Sum(nil)
  27. return hex.EncodeToString(cipherStr)
  28. }
  29. func RandToken(userId int) string {
  30. timestamp := time.Now().Unix()
  31. userIdStr := strconv.Itoa(userId)
  32. return Md5("demo_"+strconv.FormatInt(timestamp, 16)) + userIdStr
  33. }
  34. func LogErr(tag string, err error) bool {
  35. if err != nil {
  36. logger.Infof("err %s %s", tag, err.Error())
  37. return true
  38. }
  39. return false
  40. }
  41. func IsIdCard(idCard string) bool {
  42. checkStr := "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$"
  43. if res, err := regexp.Match(checkStr, []byte(idCard)); err == nil && res {
  44. return true
  45. }
  46. return false
  47. }
  48. func IsEmail(email string) bool {
  49. //pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
  50. pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`
  51. reg := regexp.MustCompile(pattern)
  52. return reg.MatchString(email)
  53. }
  54. //mobile verify
  55. func IsPhone(mobileNum string) bool {
  56. 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}$"
  57. reg := regexp.MustCompile(regular)
  58. return reg.MatchString(mobileNum)
  59. }
  60. func CurTime() string {
  61. return time.Now().Format("2006-01-02 15:04:05")
  62. }
  63. type Media struct {
  64. MediaId int
  65. CourseId int
  66. Duration int
  67. Xs int
  68. Play int
  69. }
  70. //
  71. type MediaUrl struct {
  72. MediaId int
  73. UserId int
  74. Time int
  75. }
  76. func GetInt(val interface{}) int {
  77. if val == nil || val == "" {
  78. return 0
  79. }
  80. switch val.(type) {
  81. case int:
  82. return val.(int)
  83. case int64:
  84. return int(val.(int64))
  85. case float32:
  86. return int(val.(float32))
  87. case float64:
  88. return int(val.(float64))
  89. case string:
  90. if mint, flag := strconv.Atoi(val.(string)); flag == nil {
  91. return mint
  92. }
  93. }
  94. return 0
  95. }
  96. func JoinInts(list []int) string {
  97. res := ""
  98. for i, item := range list {
  99. if i > 0 {
  100. res += ","
  101. }
  102. res += strconv.Itoa(item)
  103. }
  104. return res
  105. }
  106. func GetBoolInt(val interface{}) int {
  107. if GetBool(val) {
  108. return 1
  109. } else {
  110. return 0
  111. }
  112. }
  113. func GetBool(val interface{}) bool {
  114. if val == nil || val == "" {
  115. return false
  116. }
  117. switch val.(type) {
  118. case int:
  119. return val.(int) > 0
  120. case int64:
  121. return val.(int64) > 0
  122. case float32:
  123. return val.(float32) > 0
  124. case float64:
  125. return val.(float64) > 0
  126. case string:
  127. return val != ""
  128. }
  129. return false
  130. }
  131. func ToInts(str string) []int {
  132. data := []int{}
  133. for _, item := range strings.Split(str, ",") {
  134. if val, err := strconv.Atoi(item); err == nil {
  135. data = append(data, val)
  136. }
  137. }
  138. return data
  139. }
  140. func sliceOutOfOrder(in []int) []int {
  141. l := len(in)
  142. for i := l - 1; i > 0; i-- {
  143. r := rr.Intn(i)
  144. in[r], in[i] = in[i], in[r]
  145. }
  146. return in
  147. }
  148. func FilterMap(list []int, data map[int]bool, minCount int) []int {
  149. nlist := []int{}
  150. slist := sliceOutOfOrder(list)
  151. gmap := make(map[int]bool)
  152. for _, id := range slist {
  153. // 80% 的概率剔除
  154. if data[id] {
  155. continue
  156. }
  157. if len(nlist) > minCount {
  158. break
  159. }
  160. gmap[id] = true
  161. nlist = append(nlist, id)
  162. }
  163. for _, id := range slist {
  164. if len(nlist) > minCount {
  165. break
  166. }
  167. if gmap[id] {
  168. continue
  169. }
  170. nlist = append(nlist, id)
  171. }
  172. return nlist
  173. }
  174. func FilterList(list []int, data map[int]bool) []int {
  175. nlist := []int{}
  176. for _, id := range list {
  177. if data[id] {
  178. continue
  179. }
  180. nlist = append(nlist, id)
  181. }
  182. return nlist
  183. }
  184. // 生成随机数
  185. func GenerateRandomNumber(start int, end int, count int) []int {
  186. if end < start || (end-start) < count {
  187. return nil
  188. }
  189. nums := make([]int, 0)
  190. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  191. for len(nums) < count {
  192. num := r.Intn((end - start)) + start
  193. exist := false
  194. for _, v := range nums {
  195. if v == num {
  196. exist = true
  197. break
  198. }
  199. }
  200. if !exist {
  201. nums = append(nums, num)
  202. }
  203. }
  204. return nums
  205. }
  206. func RandIntList(data []int, count int) []int {
  207. lenData := len(data)
  208. if lenData <= count {
  209. return data
  210. }
  211. datalist := []int{}
  212. existMap := map[int]bool{}
  213. //自由随机
  214. for i := 0; i < lenData; i++ {
  215. existMap[i] = true
  216. }
  217. // 利用 map无序
  218. for index := range existMap {
  219. if count == 0 {
  220. break
  221. }
  222. count--
  223. datalist = append(datalist, data[index])
  224. }
  225. return datalist
  226. }
  227. func GetBirthday(cardId string) string {
  228. return cardId[6:10] + "-" + cardId[10:12] + "-" + cardId[12:14]
  229. }