search.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { VueLikePage } from '../../utils/page'
  2. import { saveSearchHistory, loadSearchHistory, removeSearchHistory } from '../../utils/storage'
  3. import GoodsApi from '../../apis/goods'
  4. import Router from '../../utils/routes'
  5. VueLikePage([], {
  6. data: {
  7. showResult: false,
  8. selectShowStatus: false,
  9. resultList: [],
  10. searchTips: [],
  11. searchTypes: [
  12. {
  13. text: '企业',
  14. value: 'company'
  15. },
  16. // {
  17. // text: '产品',
  18. // value: 'goods'
  19. // },
  20. ],
  21. activeIndex: 0,
  22. keyword: ''
  23. },
  24. methods: {
  25. onLoad () {
  26. this.setData({
  27. history: loadSearchHistory()
  28. })
  29. },
  30. clearHistory () {
  31. removeSearchHistory()
  32. this.setData({
  33. history: []
  34. })
  35. },
  36. hideSelect () {
  37. this.setData({
  38. selectShowStatus: false
  39. })
  40. },
  41. showSelect () {
  42. this.setData({
  43. selectShowStatus: true
  44. })
  45. },
  46. changeSearchType (e) {
  47. this.setData({
  48. activeIndex: e.currentTarget.dataset.index
  49. })
  50. },
  51. searchHistory (e) {
  52. e.detail = e.currentTarget.dataset.keyword
  53. this.search(e)
  54. },
  55. search (e) {
  56. console.log(e)
  57. const { activeIndex, searchTypes } = this.data
  58. const value = e.detail
  59. if (!value) {
  60. this.setData({
  61. showResult: false
  62. })
  63. return
  64. }
  65. saveSearchHistory(value)
  66. const params = {
  67. type: searchTypes[activeIndex].value,
  68. keyword: value
  69. }
  70. GoodsApi.searchGoodsOrCompany(params).then(res => {
  71. const name_key = params.type === 'company' ? 'companyName' : 'name'
  72. const id_key = params.type === 'company' ? 'companyId' : 'goodsId'
  73. const img_key = params.type === 'company' ? 'introduceImage' : 'listPicUrl'
  74. this.setData({
  75. showResult: true,
  76. resultList: res.data.list.map(item => {
  77. console.log(item[img_key], 'item[img_key]')
  78. let img = []
  79. // try {
  80. // img = JSON.parse(item[img_key])
  81. // } catch (err) {
  82. // console.log(err, '解析图片列表出错')
  83. // }
  84. // console.log(img)
  85. item.name = item[name_key]
  86. item.id = item[id_key]
  87. item.img = item.companyLogo
  88. return item
  89. }),
  90. history: loadSearchHistory(),
  91. keyword: params.keyword,
  92. searchTips: []
  93. })
  94. })
  95. },
  96. toResultDetail (e) {
  97. const { index } = e.currentTarget.dataset
  98. const item = this.data.resultList[index]
  99. if (item.vrLink) {
  100. Router.push({
  101. url: 'scene',
  102. query: {
  103. vr_link: item.vrLink
  104. }
  105. })
  106. } else {
  107. Router.push({
  108. url: 'goodsDetail',
  109. query: {
  110. goods_id: item.id
  111. }
  112. })
  113. }
  114. },
  115. handleTipClick (e) {
  116. e.detail = e.currentTarget.dataset.item
  117. this.search(e)
  118. },
  119. handleInput (e) {
  120. console.log(e)
  121. const params = {
  122. type: 'company',
  123. keyword: e.detail
  124. }
  125. if (!e.detail) {
  126. this.setData({
  127. searchTips: []
  128. })
  129. }
  130. GoodsApi.searchGoodsOrCompany(params).then(res => {
  131. const name_key = 'companyName'
  132. this.setData({
  133. searchTips: res.data.list.map(item => item[name_key]),
  134. })
  135. })
  136. }
  137. }
  138. })