common.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package utils
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "fmt"
  7. "regexp"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. )
  12. // HmacSha256 计算HmacSha256
  13. // key 是加密所使用的key
  14. // data 是加密的内容
  15. func HmacSha256(key string, data string) []byte {
  16. mac := hmac.New(sha256.New, []byte(key))
  17. _, _ = mac.Write([]byte(data))
  18. return mac.Sum(nil)
  19. }
  20. // 数据没有记录
  21. func ErrNoRow() string {
  22. return "<QuerySeter> no row found"
  23. }
  24. // HmacSha256ToBase64 将加密后的二进制转Base64字符串
  25. func HmacSha256ToBase64(key string, data string) string {
  26. return base64.URLEncoding.EncodeToString(HmacSha256(key, data))
  27. }
  28. func GetSign(nonce, timestamp, appId, secret string) (sign string) {
  29. signStrMap := map[string]string{
  30. "nonce": nonce,
  31. "timestamp": timestamp,
  32. "appid": appId,
  33. }
  34. keys := make([]string, 0, len(signStrMap))
  35. for k := range signStrMap {
  36. keys = append(keys, k)
  37. }
  38. sort.Strings(keys)
  39. var signStr string
  40. for _, k := range keys {
  41. signStr += k + "=" + signStrMap[k] + "&"
  42. }
  43. signStr = strings.Trim(signStr, "&")
  44. fmt.Println("signStr:" + signStr)
  45. sign = HmacSha256ToBase64(secret, signStr)
  46. return
  47. }
  48. func StringsToJSON(str string) string {
  49. rs := []rune(str)
  50. jsons := ""
  51. for _, r := range rs {
  52. rint := int(r)
  53. if rint < 128 {
  54. jsons += string(r)
  55. } else {
  56. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  57. }
  58. }
  59. return jsons
  60. }
  61. func StartIndex(page, pagesize int) int {
  62. if page > 1 {
  63. return (page - 1) * pagesize
  64. }
  65. return 0
  66. }
  67. // TrimHtml
  68. func TrimHtml(src string) string {
  69. //将HTML标签全转换成小写
  70. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  71. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  72. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  73. src = re.ReplaceAllString(src, "")
  74. re, _ = regexp.Compile("class[\\S\\s]+?>")
  75. src = re.ReplaceAllString(src, "")
  76. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  77. src = re.ReplaceAllString(src, "")
  78. return strings.TrimSpace(src)
  79. }
  80. func Unique[T comparable](slice []T) []T {
  81. seen := make(map[T]struct{})
  82. var unique []T
  83. for _, v := range slice {
  84. if _, exists := seen[v]; !exists {
  85. unique = append(unique, v)
  86. seen[v] = struct{}{}
  87. }
  88. }
  89. return unique
  90. }
  91. func GetOrmReplaceHolder(num int) string {
  92. var stringBuffer strings.Builder
  93. for i := 0; i < num; i++ {
  94. stringBuffer.WriteString("?")
  95. if i != num-1 {
  96. stringBuffer.WriteString(",")
  97. }
  98. }
  99. return stringBuffer.String()
  100. }