package util import ( "crypto/sha256" "fmt" "github.com/shopspring/decimal" "io" "math/big" "net/http" qu "qfw/util" "reflect" "regexp" "strconv" ) var reg = regexp.MustCompile("[^0-9A-Za-z\u4e00-\u9fa5]+") var Filter = regexp.MustCompile("<[^>]*?>|[\\s\u3000\u2003\u00a0]") func Sha(con string) string { h := sha256.New() con = reg.ReplaceAllString(Filter.ReplaceAllString(con, ""), "") h.Write([]byte(con)) return fmt.Sprintf("%x", h.Sum(nil)) } func HexToBigIntMod(href string) int { //取哈希值 t := sha256.New() _, _ = io.WriteString(t, href) hex := fmt.Sprintf("%x", t.Sum(nil)) //取模 n := new(big.Int) n, _ = n.SetString(hex[2:], 16) return int(n.Mod(n, big.NewInt(16)).Int64()) } func HexText(href string) string { h := sha256.New() h.Write([]byte(href)) return fmt.Sprintf("%x", h.Sum(nil)) } //数据类型转换 func GetPostForm(r *http.Request) map[string]interface{} { val := map[string]interface{}{} for k, _ := range r.Form { // if len(k) < 2 { // continue // } if k != "_id" { v := r.FormValue(k) switch k[:2] { case "s_": //string型 val[k] = v case "l_": //int64位 val[k], _ = strconv.ParseInt(v, 10, 64) case "i_": //int型 val[k], _ = strconv.Atoi(v) default: if v == "true" || v == "false" { b, _ := strconv.ParseBool(v) val[k] = b } else { val[k] = v } } } } return val } //从数组中删除元素 func deleteSlice(arr []string, v string) []string { for k, v1 := range arr { if v1 == v { return append(arr[:k], arr[k+1:]...) } } return arr } /** * 前端科学计数法处理 */ func FormatNumber(tmp map[string]interface{}) { for k, v := range tmp { if reflect.TypeOf(v).Name() == reflect.Float64.String() { num, _ := decimal.NewFromString(strconv.FormatFloat(qu.Float64All(v), 'e', -1, 64)) tmp[k], _ = num.Float64() } } } // 公告信息修改字段,需要修改项目信息的字段 var ElementsPro = []string{ "projectname", "projectcode", "buyer", "agency", "area", "city", "publishtime", "toptype", "subtype", "district", "title", "buyerperson", "buyertel", "buyerclass", "createtime", "review_experts", "purchasing", "href", "topscopeclass", "budget", "bidamount", "winner", "s_winner", } func IsModifyPro(tmp map[string]interface{}) bool { for _, v := range ElementsPro { if tmp[v] != nil { return true } } return false } func GetJyHref(id string) string { return `https://www.jianyu360.cn/article/content/` + qu.CommonEncodeArticle("content", id) + `.html` } func PutHrefRedis(href, shaid, id string) { db := HexToBigIntMod(href) hashHref := HexText(href) PutRedis("title_repeat_fulljudgement", db, hashHref, id, -1) PutRedis("shaid", 0, shaid, "", 5184000) }