util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package util
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. "io"
  7. "math/big"
  8. "net/http"
  9. qu "qfw/util"
  10. "reflect"
  11. "regexp"
  12. "strconv"
  13. )
  14. var reg = regexp.MustCompile("[^0-9A-Za-z\u4e00-\u9fa5]+")
  15. var Filter = regexp.MustCompile("<[^>]*?>|[\\s\u3000\u2003\u00a0]")
  16. func Sha(con string) string {
  17. h := sha256.New()
  18. con = reg.ReplaceAllString(Filter.ReplaceAllString(con, ""), "")
  19. h.Write([]byte(con))
  20. return fmt.Sprintf("%x", h.Sum(nil))
  21. }
  22. func HexToBigIntMod(href string) int {
  23. //取哈希值
  24. t := sha256.New()
  25. _, _ = io.WriteString(t, href)
  26. hex := fmt.Sprintf("%x", t.Sum(nil))
  27. //取模
  28. n := new(big.Int)
  29. n, _ = n.SetString(hex[2:], 16)
  30. return int(n.Mod(n, big.NewInt(16)).Int64())
  31. }
  32. func HexText(href string) string {
  33. h := sha256.New()
  34. h.Write([]byte(href))
  35. return fmt.Sprintf("%x", h.Sum(nil))
  36. }
  37. //数据类型转换
  38. func GetPostForm(r *http.Request) map[string]interface{} {
  39. val := map[string]interface{}{}
  40. for k, _ := range r.Form {
  41. // if len(k) < 2 {
  42. // continue
  43. // }
  44. if k != "_id" {
  45. v := r.FormValue(k)
  46. switch k[:2] {
  47. case "s_": //string型
  48. val[k] = v
  49. case "l_": //int64位
  50. val[k], _ = strconv.ParseInt(v, 10, 64)
  51. case "i_": //int型
  52. val[k], _ = strconv.Atoi(v)
  53. default:
  54. if v == "true" || v == "false" {
  55. b, _ := strconv.ParseBool(v)
  56. val[k] = b
  57. } else {
  58. val[k] = v
  59. }
  60. }
  61. }
  62. }
  63. return val
  64. }
  65. //从数组中删除元素
  66. func deleteSlice(arr []string, v string) []string {
  67. for k, v1 := range arr {
  68. if v1 == v {
  69. return append(arr[:k], arr[k+1:]...)
  70. }
  71. }
  72. return arr
  73. }
  74. /**
  75. * 前端科学计数法处理
  76. */
  77. func FormatNumber(tmp map[string]interface{}) {
  78. for k, v := range tmp {
  79. if reflect.TypeOf(v).Name() == reflect.Float64.String() {
  80. num, _ := decimal.NewFromString(strconv.FormatFloat(qu.Float64All(v), 'e', -1, 64))
  81. tmp[k], _ = num.Float64()
  82. }
  83. }
  84. }
  85. // 公告信息修改字段,需要修改项目信息的字段
  86. var ElementsPro = []string{
  87. "projectname",
  88. "projectcode",
  89. "buyer",
  90. "agency",
  91. "area",
  92. "city",
  93. "publishtime",
  94. "toptype",
  95. "subtype",
  96. "district",
  97. "title",
  98. "buyerperson",
  99. "buyertel",
  100. "buyerclass",
  101. "createtime",
  102. "review_experts",
  103. "purchasing",
  104. "href",
  105. "topscopeclass",
  106. "budget",
  107. "bidamount",
  108. "winner",
  109. "s_winner",
  110. }
  111. func IsModifyPro(tmp map[string]interface{}) bool {
  112. for _, v := range ElementsPro {
  113. if tmp[v] != nil {
  114. return true
  115. }
  116. }
  117. return false
  118. }
  119. func GetJyHref(id string) string {
  120. return `https://www.jianyu360.cn/article/content/` + qu.CommonEncodeArticle("content", id) + `.html`
  121. }
  122. func PutHrefRedis(href, shaid, id string) {
  123. db := HexToBigIntMod(href)
  124. hashHref := HexText(href)
  125. PutRedis("title_repeat_fulljudgement", db, hashHref, id, -1)
  126. PutRedis("shaid", 0, shaid, "", 5184000)
  127. }