search.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: 1,
  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. item.name = item[name_key]
  76. item.id = item[id_key]
  77. item.img = item[img_key]
  78. return item
  79. }),
  80. history: loadSearchHistory(),
  81. keyword: params.keyword
  82. })
  83. })
  84. },
  85. toResultDetail (e) {
  86. const { index } = e.currentTarget.dataset
  87. const item = this.data.resultList[index]
  88. if (item.vrLink) {
  89. Router.push({
  90. url: 'scene',
  91. query: {
  92. vr_link: item.vrLink
  93. }
  94. })
  95. } else {
  96. Router.push({
  97. url: 'goodsDetail',
  98. query: {
  99. goods_id: item.id
  100. }
  101. })
  102. }
  103. }
  104. }
  105. })