search.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package controllers
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "hongze/hongze_clpt/models"
  5. "hongze/hongze_clpt/services"
  6. "hongze/hongze_clpt/utils"
  7. "strings"
  8. )
  9. type SearchController struct {
  10. BaseAuthController
  11. }
  12. type BaseSearchController struct {
  13. BaseCommonController
  14. }
  15. type MobileSearchController struct {
  16. BaseAuthMobileController
  17. }
  18. // @Title 搜索接口
  19. // @Description 搜索接口
  20. // @Param PageSize query int false "每页数据条数"
  21. // @Param CurrentIndex query int false "当前页页码,从1开始"
  22. // @Param KeyWord query string true "搜索关键词"
  23. // @Param OrderColumn query string false "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  24. // @Param ListType query int false "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  25. // @Success 200 {object} models.SearchResp
  26. // @router /list [get]
  27. func (this *MobileSearchController) ListHomeArtAndChart() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. pageSize, _ := this.GetInt("PageSize")
  34. currentIndex, _ := this.GetInt("CurrentIndex")
  35. listType, _ := this.GetInt("ListType")
  36. var startSize int
  37. if pageSize <= 0 {
  38. pageSize = utils.PageSize20
  39. }
  40. if currentIndex <= 0 {
  41. currentIndex = 1
  42. }
  43. if listType == 0 {
  44. listType = 1
  45. }
  46. startSize = paging.StartIndex(currentIndex, pageSize)
  47. keyWord := this.GetString("KeyWord")
  48. orderColumn := this.GetString("OrderColumn")
  49. if keyWord == "" {
  50. br.Msg = "请输入搜索词"
  51. br.ErrMsg = "请输入搜索词"
  52. return
  53. }
  54. user := this.User
  55. if user == nil {
  56. br.Msg = "请重新登录"
  57. br.Ret = 408
  58. return
  59. }
  60. //研选的五张图片
  61. detailResearch, errConfig := models.GetConfigByCode("category_research_img_url")
  62. if errConfig != nil {
  63. br.Msg = "获取数据失败"
  64. br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error()
  65. return
  66. }
  67. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  68. //对应分类的所图片
  69. detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url")
  70. if errConfig != nil {
  71. br.Msg = "获取数据失败"
  72. br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error()
  73. return
  74. }
  75. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  76. mapCategoryUrl := make(map[string]string)
  77. var categoryId string
  78. var imgUrlChart string
  79. for _, v := range categoryUrlList {
  80. vslice := strings.Split(v, "_")
  81. categoryId = vslice[0]
  82. imgUrlChart = vslice[len(vslice)-1]
  83. mapCategoryUrl[categoryId] = imgUrlChart
  84. }
  85. if orderColumn == "" {
  86. orderColumn = "Matching"
  87. }
  88. //indexName := utils.IndexName
  89. //pageSize = 20
  90. var chartTotal int
  91. resp := new(models.SearchResp)
  92. //page := paging.GetPaging(currentIndex, pageSize, total)
  93. var chartList []*models.HomeChartListResp
  94. var err error
  95. var condition string
  96. var pars []interface{}
  97. if listType == 1 || listType == 3 {
  98. //if listType == 1 {
  99. // pageSize = 100
  100. //}
  101. if currentIndex <= 2 {
  102. condition = ` AND title LIKE '%` + keyWord + `%'`
  103. chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
  104. if err != nil {
  105. br.Msg = "获取信息失败"
  106. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  107. return
  108. }
  109. chartTotal, err = models.GetChartCount(condition, pars)
  110. if err != nil {
  111. br.Msg = "获取信息失败"
  112. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  113. return
  114. }
  115. }
  116. }
  117. for k, v := range chartList {
  118. chartList[k].IsNeedJump = true
  119. chartList[k].Source = 2
  120. if v.PtagName != "" {
  121. chartList[k].CtagNamePc = v.PtagName
  122. }
  123. if v.CtagName != "" {
  124. chartList[k].CtagNamePc += "," + v.CtagName
  125. }
  126. if v.PtagNameTwo != "" {
  127. chartList[k].CtagNamePc += "," + v.PtagNameTwo
  128. }
  129. if v.CtagNameTwo != "" {
  130. chartList[k].CtagNamePc += "," + v.CtagNameTwo
  131. }
  132. if v.PtagName != "" {
  133. labelItem := new(models.LabelList)
  134. labelItem.PtagName = v.PtagName
  135. labelItem.CtagName = v.CtagName
  136. chartList[k].LabelList = append(chartList[k].LabelList, labelItem)
  137. }
  138. if v.PtagNameTwo != "" {
  139. labelItemTwo := new(models.LabelList)
  140. labelItemTwo.PtagName = v.PtagNameTwo
  141. labelItemTwo.CtagName = v.CtagNameTwo
  142. chartList[k].LabelList = append(chartList[k].LabelList, labelItemTwo)
  143. }
  144. if len(chartList[k].LabelList) == 0 {
  145. chartList[k].LabelList = make([]*models.LabelList, 0)
  146. }
  147. }
  148. if len(chartList) == 0 {
  149. chartList = make([]*models.HomeChartListResp, 0)
  150. }
  151. resp.ChartList = chartList
  152. var result []*models.SearchItem
  153. var total int64
  154. if listType == 1 || listType == 2 {
  155. //if orderColumn == "PublishDate" {
  156. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
  157. // result = tmpResult
  158. // total = tmpTotal
  159. // err = tmpErr
  160. //} else {
  161. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
  162. // result = tmpResult
  163. // total = tmpTotal
  164. // err = tmpErr
  165. //}
  166. tmpResult, tmpTotal, tmpErr := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn)
  167. result = tmpResult
  168. total = tmpTotal
  169. err = tmpErr
  170. if err != nil {
  171. br.Msg = "检索失败"
  172. br.ErrMsg = "检索失败,Err:" + err.Error()
  173. return
  174. }
  175. if len(result) == 0 {
  176. result = make([]*models.SearchItem, 0)
  177. }
  178. for k, v := range result {
  179. //如果是研选系列的任意取五张图片的中的一张
  180. if v.CategoryId == "0" {
  181. knum := v.ArticleId % 5
  182. result[k].ImgUrlPc = researchList[knum]
  183. } else {
  184. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  185. }
  186. result[k].Source = 1
  187. }
  188. }
  189. //ListType query int true "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  190. //记录用户搜索关键词
  191. var source int
  192. if listType == 1 {
  193. source = 3
  194. } else if listType == 2 {
  195. source = 1
  196. } else {
  197. source = 2
  198. }
  199. //如果是有效用户就记录搜索历史
  200. if user.UserId > 0 {
  201. go services.AddSearchKeyWord(user, keyWord, source)
  202. }
  203. if chartTotal > int(total) {
  204. total = int64(chartTotal)
  205. }
  206. if listType == 1 {
  207. total = total + int64(chartTotal)
  208. }
  209. if len(result) == 0 {
  210. result = make([]*models.SearchItem, 0)
  211. }
  212. page := paging.GetPaging(currentIndex, pageSize, int(total))
  213. resp.Paging = page
  214. resp.List = result
  215. br.Ret = 200
  216. br.Success = true
  217. br.Msg = "获取成功"
  218. br.Data = resp
  219. }
  220. // @Title 获取搜索推荐词
  221. // @Description 获取搜索推荐词
  222. // @Success 200 {object} models.ConfigResp
  223. // @router /keyWord [get]
  224. func (this *MobileSearchController) BrowseHistoryList() {
  225. br := new(models.BaseResponse).Init()
  226. defer func() {
  227. this.Data["json"] = br
  228. this.ServeJSON()
  229. }()
  230. user := this.User
  231. if user == nil {
  232. br.Msg = "请重新登录"
  233. br.Ret = 408
  234. return
  235. }
  236. resp := new(models.ConfigResp)
  237. hotSearch, err := models.GetHotSearch()
  238. if err != nil {
  239. br.Msg = "获取数据失败"
  240. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  241. return
  242. }
  243. slicehotSearch := strings.Split(hotSearch, ",")
  244. for _, v := range slicehotSearch {
  245. item := new(models.KeyWord)
  246. item.KeyWord = v
  247. resp.ListRecommend = append(resp.ListRecommend, item)
  248. }
  249. hotList, err := models.GetSearchKeyWordTop()
  250. if err != nil {
  251. br.Msg = "获取信息失败"
  252. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  253. return
  254. }
  255. for _, v := range hotList {
  256. item := new(models.KeyWord)
  257. item.KeyWord = v.KeyWord
  258. resp.ListHot = append(resp.ListHot, item)
  259. }
  260. //如果用户存在就展示历史搜索记录
  261. if user.UserId > 0 {
  262. historySearch, err := models.GetSearchKeyWordByUser(user.UserId)
  263. if err != nil {
  264. br.Msg = "获取数据失败"
  265. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  266. return
  267. }
  268. resp.ListHistory = historySearch
  269. }
  270. if len(resp.ListHistory) == 0 {
  271. resp.ListHistory = make([]*models.KeyWord, 0)
  272. }
  273. br.Msg = "获取成功!"
  274. br.Ret = 200
  275. br.Success = true
  276. br.Data = resp
  277. }