supsearch.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. package front
  2. import (
  3. "fmt"
  4. "jfw/config"
  5. "jfw/filter"
  6. "jfw/jylabutil"
  7. "jfw/public"
  8. "math/rand"
  9. "net/http"
  10. "qfw/util"
  11. "qfw/util/elastic"
  12. "qfw/util/jy"
  13. "qfw/util/redis"
  14. "strconv"
  15. "strings"
  16. "time"
  17. uc "ucbsutil"
  18. "github.com/SKatiyar/qr"
  19. "github.com/go-xweb/xweb"
  20. "github.com/gocql/gocql"
  21. )
  22. type Pcsearch struct {
  23. *xweb.Action
  24. pcSearchIndex xweb.Mapper `xweb:"/jylab/supsearch/index.html"` //搜索首页
  25. getNewBids xweb.Mapper `xweb:"/jylab/supsearch/getNewBids"` //最新招标信息
  26. getstatus xweb.Mapper `xweb:"/jylab/supsearch/getstatus"` //实验室开启状态
  27. proposedProject xweb.Mapper `xweb:"/jylab/supsearch/proposedProject.html"` //拟建项目
  28. qr xweb.Mapper `xweb:"/jylab/supsearch/qr/(.*)"` //生成进入实验室的二维码
  29. changePro xweb.Mapper `xweb:"/jylab/changePro"` //修改提示信息状态
  30. }
  31. //
  32. var industrylist map[string][]string
  33. var sortArray []string
  34. var industryname string
  35. //
  36. func init() {
  37. xweb.AddAction(&Pcsearch{})
  38. industryname = util.ObjToString(config.Sysconfig["industry"])
  39. industrylist, sortArray = getindustrys()
  40. }
  41. //
  42. func (p *Pcsearch) ChangePro() {
  43. defer util.Catch()
  44. userid := p.GetSession("userId")
  45. if userid != nil {
  46. jylabutil.UpdateAuthory(userid.(string), "i_tablepro", 1)
  47. }
  48. }
  49. //
  50. func (p *Pcsearch) ProposedProject() error {
  51. defer util.Catch()
  52. var shareid = p.GetString("id")
  53. if len(shareid) == 0 {
  54. shareid = "10"
  55. }
  56. p.T["logid"] = config.Seoconfig["jynjxmy"].(string)
  57. b_word, _, s_word := jy.InterceptSearchKW(p.GetString("keywords"), false, true)
  58. area := p.GetString("area") //地区
  59. publishtime := p.GetString("publishtime") //发布时间
  60. subtype := p.GetString("subtype") //信息类型
  61. if subtype == "" {
  62. subtype = "拟建"
  63. }
  64. var totalPage int64
  65. var list *[]map[string]interface{}
  66. var status = 1
  67. if len(s_word) > 0 {
  68. status = 2
  69. var count int64
  70. count, totalPage, list = getBidSearchData(s_word, area, publishtime, subtype, "", "", "", 0, true, "", "")
  71. listSize := 0
  72. if list != nil {
  73. listSize = len(*list)
  74. }
  75. SaveUserSearchLog(p.Request, util.ObjToString(p.GetSession("userId")), count, "pc", "拟建", map[string]interface{}{
  76. "search_word": s_word,
  77. "search_area": area,
  78. "search_publishtime": publishtime,
  79. "pagenum": 1,
  80. "pagesize": listSize,
  81. })
  82. } else {
  83. p.DisableHttpCache()
  84. p.T["list"] = Newbids("nijian")[0]
  85. }
  86. if status == 2 {
  87. if list != nil {
  88. for _, v := range *list {
  89. v["_id"] = util.EncodeArticleId2ByCheck(v["_id"].(string))
  90. stp, _ := v["subtype"].(string)
  91. if stp == "" {
  92. stp, _ = v["toptype"].(string)
  93. }
  94. area, _ := v["area"].(string)
  95. v["stypeadd"], v["areaadd"], _ = classify(stp, area, "")
  96. //正文匹配检索关键词
  97. highlight, _ := v["highlight"].(map[string][]string)
  98. detail := ""
  99. for _, val := range highlight["detail"] {
  100. detail += public.ClearHtml.ReplaceAllString(val, "")
  101. }
  102. v["detail"] = detail
  103. }
  104. }
  105. p.T["list"] = list
  106. }
  107. p.T["area"] = area
  108. p.T["subtype"] = subtype
  109. p.T["publishtime"] = publishtime
  110. p.T["timeslot"] = p.GetString("timeslot")
  111. p.T["totalPage"] = totalPage
  112. p.T["keywords"] = b_word
  113. p.T["searchvalue"] = s_word
  114. p.T["login"] = p.Session().Get("user")
  115. p.SetSession("paramkey", b_word)
  116. if publishtime == "lately-7" {
  117. p.SetSession("parampublishtime", "最近7天")
  118. } else if publishtime == "lately-30" {
  119. p.SetSession("parampublishtime", "最近30天")
  120. } else if publishtime == "thisyear" {
  121. p.SetSession("parampublishtime", "去年")
  122. } else {
  123. p.SetSession("parampublishtime", publishtime)
  124. }
  125. p.SetSession("paramarea", area)
  126. p.T["shareid"] = se.EncodeString(shareid)
  127. return p.Render("/pc/proproject.html", &p.T)
  128. }
  129. //
  130. func (p *Pcsearch) Getstatus() error {
  131. defer util.Catch()
  132. var supstatus, entstatus, tablepro, dataexportstatus, portraitpower, followent, smartstatus bool
  133. if userid := util.ObjToString(p.GetSession("userId")); userid != "" {
  134. user, b := mongodb.FindById("user", userid,
  135. `{"i_tablepro":1}`)
  136. if len(*user) > 0 && b {
  137. tablepro = util.Int64All((*user)["i_tablepro"]) == 1
  138. }
  139. }
  140. p.ServeJson(map[string]interface{}{
  141. "smartstatus": smartstatus,
  142. "supstatus": supstatus,
  143. "entstatus": entstatus,
  144. "tablepro": tablepro,
  145. "dataexportstatus": dataexportstatus,
  146. "portraitpower": portraitpower,
  147. "followent": followent,
  148. })
  149. return nil
  150. }
  151. //
  152. func (p *Pcsearch) GetNewBids() error {
  153. pagenum, _ := p.GetInteger("pageNumber")
  154. pageType := p.GetString("pageType")
  155. list := util.ObjArrToMapArr(Newbids(pageType)[pagenum-1].([]interface{}))
  156. if pageType == "" { //最新招标信息仅第一页条展示表格多字段
  157. for k, v := range list {
  158. if pagenum == 1 && k < 20 {
  159. continue
  160. }
  161. delete(v, "areaadd")
  162. delete(v, "bidopentime")
  163. delete(v, "buyer")
  164. delete(v, "indadd")
  165. delete(v, "k")
  166. delete(v, "projectcode")
  167. delete(v, "projectname")
  168. delete(v, "stypeadd")
  169. delete(v, "winner")
  170. delete(v, "timetemp")
  171. delete(v, "budget")
  172. delete(v, "s_subscopeclass")
  173. delete(v, "toptype")
  174. }
  175. }
  176. p.ServeJson(map[string]interface{}{
  177. "list": list,
  178. })
  179. return nil
  180. }
  181. //
  182. func (p *Pcsearch) PcSearchIndex() error {
  183. defer util.Catch()
  184. var shareid = p.GetString("id")
  185. if len(shareid) == 0 {
  186. shareid = "10"
  187. }
  188. p.T["logid"] = config.Seoconfig["jysslby"].(string)
  189. fmt.Println("logid:", config.Seoconfig["jysslby"].(string))
  190. keywords := p.GetString("keywords")
  191. area := p.GetString("area") //地区
  192. publishtime := p.GetString("publishtime") //发布时间
  193. timeslot := p.GetString("timeslot") //显示选择时间
  194. toptype := p.GetString("toptype") //信息类型
  195. subtype := p.GetString("subtype") //信息类型
  196. minprice := p.GetString("minprice") //最低价格
  197. maxprice := p.GetString("maxprice") //最高价格
  198. industry := strings.TrimSpace(p.GetString("industry")) //选中的行业
  199. p.SetSession("industry", industry)
  200. selectType := p.GetString("selectType") //标题或全文
  201. selectTypesess := p.GetSession("selectType")
  202. if selectTypesess != nil && selectTypesess != "" {
  203. selectType = selectTypesess.(string)
  204. }
  205. //if selectType == "" {
  206. selectType = "title"
  207. //}
  208. //历史导出数据回显
  209. if strings.Contains(p.Url(), "?goback") {
  210. keywords = util.ObjToString(p.GetSession("Echo_keywords"))
  211. publishtime = util.ObjToString(p.GetSession("Echo_publishtime"))
  212. area = util.ObjToString(p.GetSession("Echo_area"))
  213. subtype = util.ObjToString(p.GetSession("Echo_subtype"))
  214. minprice = util.ObjToString(p.GetSession("Echo_minprice"))
  215. maxprice = util.ObjToString(p.GetSession("Echo_maxprice"))
  216. industry = util.ObjToString(p.GetSession("Echo_industry"))
  217. selectType = util.ObjToString(p.GetSession("Echo_selectType"))
  218. timeslot = util.ObjToString(p.GetSession("Echo_timeslot"))
  219. }
  220. b_word, _, s_word := jy.InterceptSearchKW(keywords, false, len(industry) == 0)
  221. var list *[]map[string]interface{}
  222. var status = 1
  223. var count, totalPage int64
  224. secondFlag := ""
  225. secondKWS := ""
  226. if len(s_word) > 0 || len(industry) > 0 {
  227. status = 2
  228. count, totalPage, list = getBidSearchData(s_word, area, publishtime, subtype, industry, minprice, maxprice, 0, true, selectType, "")
  229. listSize := 0
  230. if list != nil {
  231. listSize = len(*list)
  232. }
  233. //所有的再次分词查询 只查标题
  234. if len([]rune(s_word)) > 3 && int(count) < pc_pageSize {
  235. secondKWS = jy.HttpEs(s_word, "ik_smart", config.Sysconfig["elasticsearch"].(string))
  236. findfields := `"title"`
  237. qstr := getSearchQuery(secondKWS, industry, minprice, maxprice, findfields, getBidSearchQuery(area, publishtime, subtype))
  238. secondLimit := 2*pc_pageSize - int(count)
  239. secRel := elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, bidSearch_field, 0, secondLimit, 0, false)
  240. if secRel != nil {
  241. public.BidListConvert(industry, secRel)
  242. }
  243. if list != nil {
  244. list = public.MapArrSortMerge(*list, *secRel, "_id", "publishtime")
  245. } else {
  246. list = secRel
  247. }
  248. if len(*secRel) > 0 {
  249. if secondKWS != "" {
  250. s_word += "+" + secondKWS
  251. }
  252. secondFlag = "T"
  253. }
  254. }
  255. SaveUserSearchLog(p.Request, util.ObjToString(p.GetSession("userId")), count, "pc", "超级搜索", map[string]interface{}{
  256. "search_word": s_word,
  257. "search_area": area,
  258. "search_price": []string{minprice, maxprice},
  259. "search_publishtime": publishtime,
  260. "search_type": subtype,
  261. "search_industry": industry,
  262. "pagenum": 1,
  263. "pagesize": listSize,
  264. })
  265. } else {
  266. p.DisableHttpCache()
  267. p.T["list"] = Newbids("")[0]
  268. }
  269. if status == 2 {
  270. if list != nil {
  271. for _, v := range *list {
  272. v["_id"] = util.EncodeArticleId2ByCheck(v["_id"].(string))
  273. stp, _ := v["subtype"].(string)
  274. if stp == "" {
  275. stp, _ = v["toptype"].(string)
  276. }
  277. area, _ := v["area"].(string)
  278. indtry := util.ObjToString(v["industry"])
  279. v["stypeadd"], v["areaadd"], v["indadd"] = classify(stp, area, indtry)
  280. //正文匹配检索关键词
  281. highlight, _ := v["highlight"].(map[string][]string)
  282. detail := ""
  283. for _, val := range highlight["detail"] {
  284. detail += public.ClearHtml.ReplaceAllString(val, "")
  285. }
  286. v["detail"] = detail
  287. }
  288. }
  289. if secondFlag == "" {
  290. p.T["list"] = list
  291. } else {
  292. if len(*list) > pc_pageSize {
  293. p.T["list"] = (*list)[:pc_pageSize]
  294. p.T["secondList"] = (*list)[pc_pageSize:]
  295. } else {
  296. p.T["list"] = list
  297. p.T["secondList"] = ""
  298. totalPage = 1
  299. }
  300. }
  301. }
  302. p.T["secondFlag"] = secondFlag
  303. p.T["area"] = area
  304. p.T["publishtime"] = publishtime
  305. p.T["timeslot"] = timeslot
  306. p.T["toptype"] = toptype
  307. p.T["subtype"] = subtype
  308. p.T["keywords"] = b_word
  309. p.T["searchvalue"] = s_word
  310. p.T["selectType"] = selectType
  311. p.T["industry"] = industry
  312. p.T["minprice"] = minprice
  313. p.T["maxprice"] = maxprice
  314. p.T["login"] = p.Session().Get("user")
  315. p.T["totalPage"] = totalPage
  316. p.T["count"] = count
  317. p.SetSession("paramkey", b_word)
  318. if publishtime == "lately-7" {
  319. p.SetSession("parampublishtime", "最近7天")
  320. } else if publishtime == "lately-30" {
  321. p.SetSession("parampublishtime", "最近30天")
  322. } else if publishtime == "thisyear" {
  323. p.SetSession("parampublishtime", "去年")
  324. } else {
  325. p.SetSession("parampublishtime", publishtime)
  326. }
  327. p.SetSession("paramarea", area)
  328. if subtype != "" {
  329. p.SetSession("paraminfotype", subtype)
  330. } else {
  331. p.SetSession("paraminfotype", toptype)
  332. }
  333. p.T["industrylist"] = industrylist
  334. p.T["sortArray"] = sortArray
  335. p.T["shareid"] = se.EncodeString(shareid)
  336. //
  337. if userid := p.GetSession("userId"); userid != nil {
  338. p.T["portraitpower"] = jylabutil.IsAuthorized(userid.(string), "i_portraitpower")
  339. }
  340. return p.Render("/pc/supsearch.html", &p.T)
  341. }
  342. //
  343. func getindustrys() (industry map[string][]string, sortArray []string) {
  344. industry = map[string][]string{}
  345. sortArray = []string{}
  346. classdata, ok1 := mongodb.FindOneByField("rc_task", `{"s_name":"`+industryname+`"}`, `{"s_class":1}`)
  347. if ok1 && len(*classdata) > 0 {
  348. classid := ""
  349. if util.ObjToString((*classdata)["s_class"]) != "" {
  350. classids := strings.Split(util.ObjToString((*classdata)["s_class"]), ",")
  351. for k, v := range classids {
  352. if k > 0 {
  353. classid += `,`
  354. }
  355. classid += `"` + v + `"`
  356. }
  357. }
  358. industryData, ok := mongodb.Find("rc_rule", `{"s_pid":{"$ne":""},"s_classid":{"$in":[`+classid+`]}}`, `{"i_order":1}`, `{"_id":1,"s_name":1}`, false, -1, -1)
  359. if ok && industryData != nil && len(*industryData) > 0 {
  360. for _, v := range *industryData {
  361. fatFlag := 0
  362. industryname, _ := v["s_name"].(string)
  363. if strings.Contains(industryname, "_") {
  364. fat := strings.Split(industryname, "_")[0]
  365. child := strings.Split(industryname, "_")[1]
  366. if len(industry[fat]) == 0 {
  367. sortArray = append(sortArray, fat)
  368. }
  369. for _, fv := range industry[fat] {
  370. if fv == strings.Trim(child, " ") {
  371. fatFlag = 1
  372. break
  373. }
  374. }
  375. if fatFlag != 1 {
  376. industry[fat] = append(industry[fat], child)
  377. }
  378. }
  379. }
  380. }
  381. }
  382. return industry, sortArray
  383. }
  384. //
  385. func Newbids(p string) []interface{} {
  386. pages, _ := redis.Get("other", "index_list"+p).([]interface{})
  387. if len(pages) == 0 {
  388. var list *[]map[string]interface{}
  389. subtype := ""
  390. if len(p) > 0 {
  391. subtype = "拟建"
  392. }
  393. intns := make([]int, 0)
  394. for i := 0; i < 10; i++ {
  395. if len(intns) == 0 {
  396. intns = append(intns, rand.Intn(100))
  397. } else {
  398. for {
  399. v := rand.Intn(intns[(i-1)] + 100)
  400. if v-intns[(i-1)] >= 30 {
  401. intns = append(intns, v)
  402. break
  403. }
  404. }
  405. }
  406. }
  407. for i := 0; i < 10; i++ {
  408. _, list = getLastNewsData("", "", "", subtype, "", "", "", "Y", intns[i], true, false)
  409. for k, v := range *list {
  410. v["k"] = (k + 1) + i*50
  411. t := time.Unix(util.Int64All(v["publishtime"]), 0)
  412. v["timetemp"] = fmt.Sprint(util.Int64All(v["publishtime"]))
  413. v["_id"] = util.EncodeArticleId2ByCheck(v["_id"].(string))
  414. v["time"] = util.TimeDiff(t)
  415. var stp = ""
  416. if v["subtype"] != nil {
  417. stp, _ = v["subtype"].(string)
  418. } else {
  419. stp = ""
  420. }
  421. if stp == "" && v["toptype"] != nil {
  422. stp, _ = v["toptype"].(string)
  423. }
  424. area, _ := v["area"].(string)
  425. indtry := util.ObjToString(v["industry"])
  426. v["stypeadd"], v["areaadd"], v["indadd"] = classify(stp, area, indtry)
  427. }
  428. pages = append(pages, list)
  429. }
  430. redis.Put("other", "index_list"+p, pages, 60*60*24*2)
  431. }
  432. return pages
  433. }
  434. //二维码图片
  435. func (p *Pcsearch) Qr(t string) error {
  436. openid := p.GetSession("openid")
  437. if openid == nil {
  438. return nil
  439. }
  440. w := p.ResponseWriter
  441. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  442. w.Header().Set("Pragma", "no-cache")
  443. w.Header().Set("Expires", "0")
  444. w.Header().Set("Content-Type", "image/png")
  445. data := config.Sysconfig["webdomain"].(string) + "/front/sess/" + sewx.EncodeString(openid.(string)+",uid,"+strconv.Itoa(int(time.Now().Unix()))+",qrToLab") + "__" + t
  446. r, _ := qr.Encode(data, qr.M)
  447. pngdat := r.PNG()
  448. _, err := w.Write(pngdat)
  449. return err
  450. }
  451. //保存用户搜索日志
  452. func SaveUserSearchLog(request *http.Request, userid string, count int64, platform, source string, condition map[string]interface{}) {
  453. go func() {
  454. data := map[string]interface{}{
  455. "ip": filter.GetIp(request),
  456. "count": count,
  457. "openid": userid,
  458. "pform": platform,
  459. "source": source,
  460. "date": time.Now(),
  461. "uagent": request.Header.Get("User-Agent"),
  462. }
  463. for _, v := range []string{"pagenum", "pagesize", "search_word", "search_area", "search_publishtime", "search_industry", "search_price", "search_type"} {
  464. if vv := condition[v]; vv != nil {
  465. //替换key
  466. if v == "search_word" {
  467. v = "word"
  468. } else if v == "search_area" {
  469. v = "area"
  470. } else if v == "search_publishtime" {
  471. v = "ptime"
  472. } else if v == "search_industry" {
  473. v = "indst"
  474. } else if v == "search_price" {
  475. v = "price"
  476. } else if v == "search_type" {
  477. v = "type"
  478. } else if v == "pagenum" {
  479. v = "pnum"
  480. } else if v == "pagesize" {
  481. v = "psize"
  482. }
  483. data[v] = vv
  484. }
  485. }
  486. data["id"] = uc.DayShortTime()
  487. data["tuid"] = gocql.TimeUUID()
  488. if !public.Ca_Log.SaveCache("jy_search", data) {
  489. data := map[string]interface{}{
  490. "ip": filter.GetIp(request),
  491. "count": count,
  492. "s_userid": userid,
  493. "platform": platform,
  494. "source": source,
  495. "createtime": time.Now().Unix(),
  496. "userAgent": request.Header.Get("User-Agent"),
  497. }
  498. for _, v := range []string{"pagenum", "pagesize", "search_word", "search_area", "search_publishtime", "search_industry", "search_price", "search_type"} {
  499. if vv := condition[v]; vv != nil {
  500. data[v] = vv
  501. }
  502. }
  503. mongodb.Save("jy_search_log", data)
  504. }
  505. }()
  506. }