search.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. searchTypes: [
  11. {
  12. text: '企业',
  13. value: 'company'
  14. },
  15. // {
  16. // text: '产品',
  17. // value: 'goods'
  18. // },
  19. ],
  20. activeIndex: 0,
  21. keyword: ''
  22. },
  23. methods: {
  24. onLoad () {
  25. this.setData({
  26. history: loadSearchHistory()
  27. })
  28. },
  29. clearHistory () {
  30. removeSearchHistory()
  31. this.setData({
  32. history: []
  33. })
  34. },
  35. hideSelect () {
  36. this.setData({
  37. selectShowStatus: false
  38. })
  39. },
  40. showSelect () {
  41. this.setData({
  42. selectShowStatus: true
  43. })
  44. },
  45. changeSearchType (e) {
  46. this.setData({
  47. activeIndex: e.currentTarget.dataset.index
  48. })
  49. },
  50. searchHistory (e) {
  51. e.detail.value = e.currentTarget.dataset.keyword
  52. this.search(e)
  53. },
  54. search (e) {
  55. const { activeIndex, searchTypes } = this.data
  56. const { value } = e.detail
  57. if (!value) {
  58. this.setData({
  59. showResult: false
  60. })
  61. return
  62. }
  63. saveSearchHistory(value)
  64. const params = {
  65. type: searchTypes[activeIndex].value,
  66. keyword: e.detail.value
  67. }
  68. GoodsApi.searchGoodsOrCompany(params).then(res => {
  69. const name_key = params.type === 'company' ? 'companyName' : 'name'
  70. const id_key = params.type === 'company' ? 'companyId' : 'goodsId'
  71. const img_key = params.type === 'company' ? 'introduceImage' : 'listPicUrl'
  72. this.setData({
  73. showResult: true,
  74. resultList: res.data.list.map(item => {
  75. console.log(item[img_key], 'item[img_key]')
  76. let img = JSON.parse(item[img_key])
  77. console.log(img)
  78. item.name = item[name_key]
  79. item.id = item[id_key]
  80. item.img = img[0].img
  81. return item
  82. }),
  83. history: loadSearchHistory(),
  84. keyword: params.keyword
  85. })
  86. })
  87. },
  88. toResultDetail (e) {
  89. const { index } = e.currentTarget.dataset
  90. const item = this.data.resultList[index]
  91. if (item.vrLink) {
  92. Router.push({
  93. url: 'scene',
  94. query: {
  95. vr_link: item.vrLink
  96. }
  97. })
  98. } else {
  99. Router.push({
  100. url: 'goodsDetail',
  101. query: {
  102. goods_id: item.id
  103. }
  104. })
  105. }
  106. }
  107. }
  108. })