123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package public
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "regexp"
- "strings"
- "time"
- util "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/encrypt"
- elastic "app.yhyue.com/moapp/jybase/esv1"
- . "app.yhyue.com/moapp/jybase/mongodb"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
- )
- var ClearHtml = regexp.MustCompile("<[^>]*>")
- var ClearOther = regexp.MustCompile("[\n\r\\s\u3000\u2003\u00a0]")
- var filterReg_3 = regexp.MustCompile("(项目|公告|公示)$")
- var filterReg_2 = regexp.MustCompile("^[)\\)>》】\\]}}〕,,;;::'\"“”。.\\??、/+=\\_—*&……\\^%$¥@!!`~·(\\(<《【\\[{{〔]+$")
- var filterReg_1 = regexp.MustCompile("^([0-9]{1,3}|[零一二三四五六七八九十]{1,2}|联系人?|电话|地址|编号|采购|政府采购|成交|更正|招标|中标|变更|结果)$")
- var filterReg = regexp.MustCompile("^[的人号时元万公告项目地址电话邮编日期联系招标中结果成交项目项目采购采购项目政府采购公告更正公告]+$")
- var MgoBiddingFields = `{"_id":1,"projectname":1,"projectcode":1,"title":1,"s_winner":1,"buyertel":1,"bidstatus":1,"site":1,"bidamount":1,"toptype":1,"winneraddr":1,"winner":1,"agency":1,"buyer":1,"detail":1,"city":1,"subtype":1,"buyerclass":1,"href":1,"comeintime":1,"winnertel":1,"area":1,"publishtime":1,"buyeraddr":1,"agencytel":1,"budget":1,"entidlist":1,"buyerperson":1,"winnerperson":1,"agencyaddr":1,"recommended_service":1,"competehref":1,"owner":1,"total_investment":1,"projectaddr":1,"projectperiod":1,"approvedept":1,"approvecontent":1,"approvecode":1,"approvenumber":1,"approvetime":1,"approvestatus":1,"project_scale":1}`
- const (
- INDEX = "bidding"
- TYPE = "bidding"
- )
- func FilteKey(k string) string {
- k = strings.TrimSpace(k)
- k = filterReg_3.ReplaceAllString(k, "")
- k = filterReg_2.ReplaceAllString(k, "")
- k = filterReg_1.ReplaceAllString(k, "")
- k = filterReg.ReplaceAllString(k, "")
- return k
- }
- /*
- * 结果列表转换,目前只换行行业字段
- * 所有的招标搜索都要调用此方法,列表中有展示行业的也可以用
- * industry 搜索条件中的行业,默认为空
- */
- func BidListConvert(industry string, list *[]map[string]interface{}) {
- if list == nil {
- return
- }
- commonSubstring := func(v string) (value string) {
- bcs := strings.Split(v, "_")
- if len(bcs) == 1 {
- value = bcs[0]
- } else if len(bcs) == 2 {
- value = bcs[0]
- if strings.TrimSpace(value) == "" {
- value = bcs[0]
- }
- }
- return
- }
- for _, v := range *list {
- budget, _ := v["budget"].(float64)
- bidamount, _ := v["bidamount"].(float64)
- if budget == 0 || strings.TrimSpace(fmt.Sprint(v["budget"])) == "" {
- delete(v, "budget")
- }
- if bidamount == 0 || strings.TrimSpace(fmt.Sprint(v["bidamount"])) == "" {
- delete(v, "bidamount")
- }
- value := ""
- subscopeclass, _ := v["s_subscopeclass"].(string)
- subscopeclass = strings.Trim(subscopeclass, ",")
- bct := strings.Split(subscopeclass, ",")
- if bct == nil || len(bct) == 0 {
- continue
- }
- //搜索条件中没有行业的话,取查询结果中第一个行业
- if industry == "" {
- value = commonSubstring(bct[0])
- } else { //搜索条件中有行业的话,取行业中和搜索条件相对应的第一个
- industrys := strings.Split(industry, ",")
- L:
- for _, bc := range bct {
- for _, is := range industrys {
- if bc == is {
- value = commonSubstring(bc)
- break L
- }
- }
- }
- }
- if strings.TrimSpace(value) == "" {
- continue
- }
- v["industry"] = value
- }
- }
- // a_mergeorder 根据这三个字段产生的顺序,决定用哪个当作openid
- func GetOldOpenId(userId string) (*map[string]interface{}, string) {
- user, ok := MQFW.FindById("user", userId, `{"s_m_openid":1,"a_m_openid":1,"s_phone":1,"a_mergeorder":1,"o_jy":1,"l_firstpushtime":1}`)
- if !ok || user == nil {
- return nil, ""
- }
- s_m_openid, _ := (*user)["s_m_openid"].(string)
- a_m_openid, _ := (*user)["a_m_openid"].(string)
- s_phone, _ := (*user)["s_phone"].(string)
- openid := jy.GetOldOpenid(s_m_openid, a_m_openid, s_phone, (*user)["a_mergeorder"])
- return user, openid
- }
- func PushViewDatas(userid, allquery, field string, pageNum, pageSize int) (keys []interface{}, list *[]map[string]interface{}) {
- keys, list = PushView(userid, allquery, field, pageNum, pageSize)
- if list != nil {
- for _, v := range *list {
- v["_id"] = EncodeArticleId2ByCheck(util.ObjToString(v["_id"]))
- }
- }
- return
- }
- func PushView(userid, allquery, field string, pageNum, pageSize int) (keys []interface{}, list *[]map[string]interface{}) {
- if userid == "" {
- return
- }
- tmp, ok := MQFW.FindById("user", userid, `{"_id":1,"o_jy":1}`)
- if !ok || tmp == nil || len(*tmp) == 0 {
- return
- }
- o_jy := (*tmp)["o_jy"].(map[string]interface{})
- a_key, _ := o_jy["a_key"].([]interface{})
- if len(a_key) == 0 {
- return
- }
- for _, v := range a_key {
- keyMap, _ := v.(map[string]interface{})
- key, _ := keyMap["key"].([]interface{})
- keys = append(keys, key)
- }
- var allkeys []elastic.KeyConfig //用户配置
- _bs, err := json.Marshal(a_key)
- if err == nil {
- json.Unmarshal(_bs, &allkeys)
- }
- list = elastic.GetResForJY(INDEX, TYPE, allkeys, allquery, `"title"`, `{"publishtime":"desc"}`, field, (pageNum-1)*pageSize, pageSize)
- return
- }
- // 查询获取最多订阅词
- func GetHotkeys() []interface{} {
- sess := MQFW.GetMgoConn()
- defer MQFW.DestoryMongoConn(sess)
- var res []map[string]interface{}
- var keys []interface{}
- sess.DB("qfw").C("user").Pipe([]map[string]interface{}{
- map[string]interface{}{"$match": map[string]interface{}{"o_jy.a_key": map[string]interface{}{"$exists": 1}}},
- map[string]interface{}{"$project": map[string]interface{}{"o_jy.a_key.key": 1}},
- map[string]interface{}{"$unwind": "$o_jy.a_key"},
- map[string]interface{}{"$project": map[string]interface{}{"key": "$o_jy.a_key.key"}},
- map[string]interface{}{"$unwind": "$key"},
- map[string]interface{}{"$group": map[string]interface{}{"_id": "$key", "sum": map[string]interface{}{"$sum": 1}}},
- map[string]interface{}{"$sort": map[string]interface{}{"sum": -1}},
- map[string]interface{}{"$limit": 6},
- }).All(&res)
- for _, v := range res {
- keys = append(keys, v["_id"])
- }
- return keys
- }
- func GetWinnerNewestDatas(winner string) *[]map[string]interface{} {
- if winner == "" {
- return nil
- }
- //判断用户是否关注
- fields := `"_id","projectname","bidamount","title","publishtime","subtype","toptype","href"`
- data := elastic.GetPage("bidding", "bidding", `{"TERM_s_winner":"`+winner+`"}`, `{"publishtime":-1}`, fields, 0, 100)
- if *data != nil {
- for _, v := range *data {
- v["_id"] = EncodeArticleId2ByCheck(BsonIdToSId(v["_id"]))
- v["bidamount"], _ = v["bidamount"].(float64)
- }
- }
- return data
- }
- // 删除redis相关数据
- func DelRelRedis(userid interface{}, relationinfo interface{}) {
- defer util.Catch()
- uid, _ := userid.(string)
- if uid == "" || relationinfo == nil {
- return
- }
- array, _ := relationinfo.([]interface{})
- for _, v := range util.ObjArrToMapArr(array) {
- sid, _ := v["s_id"].(string)
- redis.Del("push", "push_"+uid+"_"+sid)
- }
- }
- // 合并两次检索结果(根据filed从大到小)
- // sortFiled 排序字段
- // Mergefiled 判重字段
- func MapArrSortMerge(arr1, arr2 []map[string]interface{}, mergeFiled, sortFiled string) *[]map[string]interface{} {
- for _, v := range arr1 {
- for n, m := range arr2 {
- if util.ObjToString(v["_id"]) == util.ObjToString(m["_id"]) {
- arr2 = append((arr2)[0:n], (arr2)[n+1:]...)
- break
- }
- }
- }
- arr1 = append(arr1, arr2...)
- return &arr1
- // var tmp []map[string]interface{}
- // i, j := 0, 0
- // var idMap = map[string]bool{}
- // MaxLenArr1, MaxLenArr2 := len(arr1), len(arr2)
- // for i < MaxLenArr1 || j < MaxLenArr2 {
- // var mergeTmp interface{}
- // var sign map[string]interface{}
- // if len(tmp) > 0 {
- // mergeTmp = tmp[len(tmp)-1][mergeFiled]
- // }
- // if i <= MaxLenArr1-1 {
- // sign = arr1[i]
- // }
- // if j <= MaxLenArr2-1 {
- // if util.Int64All(sign[sortFiled]) > util.Int64All(arr2[j][sortFiled]) {
- // i++
- // } else {
- // sign = arr2[j]
- // j++
- // }
- // } else {
- // i++
- // }
- // if mergeTmp != sign[mergeFiled] {
- // if !idMap[util.ObjToString(sign[mergeFiled])] {
- // idMap[util.ObjToString(sign[mergeFiled])] = true
- // tmp = append(tmp, sign)
- // }
- // }
- // }
- // return &tmp
- }
- // 保存用户搜索日志
- func SaveUserSearchLog(request *http.Request, userid string, count int64, platform, source string, condition map[string]interface{}) {
- go func() {
- data := map[string]interface{}{
- "ip": util.GetIp(request),
- "count": count,
- "s_userid": userid,
- "platform": platform,
- "source": source,
- "createtime": time.Now().Unix(),
- "userAgent": request.Header.Get("User-Agent"),
- }
- for _, v := range []string{"pagenum", "pagesize", "search_word", "search_area", "search_publishtime", "search_industry", "search_price", "search_type", "bid_field"} {
- if vv := condition[v]; vv != nil {
- data[v] = vv
- }
- }
- Mgo_Log.Save("jy_search_log", data)
- }()
- }
- func isPhone(value string) bool {
- var phonePattern = regexp.MustCompile("^[1][3-9][0-9]{9}$")
- return phonePattern.MatchString(value)
- }
- func isEmail(value string) bool {
- var emailPattern = regexp.MustCompile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
- return emailPattern.MatchString(value)
- }
|