common.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "regexp"
  6. "strconv"
  7. )
  8. func MD5(data string) string {
  9. m := md5.Sum([]byte(data))
  10. return hex.EncodeToString(m[:])
  11. }
  12. func CheckPwd(pwd string) bool {
  13. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  14. reg := regexp.MustCompile(compile)
  15. flag := reg.MatchString(pwd)
  16. return flag
  17. }
  18. // 校验邮箱格式
  19. func ValidateEmailFormatat(email string) bool {
  20. reg := regexp.MustCompile(RegularEmail)
  21. return reg.MatchString(email)
  22. }
  23. // 验证是否是手机号
  24. func ValidateMobileFormatat(mobileNum string) bool {
  25. reg := regexp.MustCompile(RegularMobile)
  26. return reg.MatchString(mobileNum)
  27. }
  28. // 计算分页起始页
  29. func StartIndex(page, pagesize int) int {
  30. if page > 1 {
  31. return (page - 1) * pagesize
  32. }
  33. return 0
  34. }
  35. // GetLikeKeywordPars 获取sql查询中的参数切片
  36. func GetLikeKeywordPars(pars []interface{}, keyword string, num int) (newPars []interface{}) {
  37. newPars = pars
  38. if newPars == nil {
  39. newPars = make([]interface{}, 0)
  40. }
  41. for i := 1; i <= num; i++ {
  42. newPars = append(newPars, `%`+keyword+`%`)
  43. }
  44. return
  45. }
  46. func StringsToJSON(str string) string {
  47. rs := []rune(str)
  48. jsons := ""
  49. for _, r := range rs {
  50. rint := int(r)
  51. if rint < 128 {
  52. jsons += string(r)
  53. } else {
  54. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  55. }
  56. }
  57. return jsons
  58. }