search.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 = e.currentTarget.dataset.keyword
  52. this.search(e)
  53. },
  54. search (e) {
  55. console.log(e)
  56. const { activeIndex, searchTypes } = this.data
  57. const value = e.detail
  58. if (!value) {
  59. this.setData({
  60. showResult: false
  61. })
  62. return
  63. }
  64. saveSearchHistory(value)
  65. const params = {
  66. type: searchTypes[activeIndex].value,
  67. keyword: value
  68. }
  69. GoodsApi.searchGoodsOrCompany(params).then(res => {
  70. const name_key = params.type === 'company' ? 'companyName' : 'name'
  71. const id_key = params.type === 'company' ? 'companyId' : 'goodsId'
  72. const img_key = params.type === 'company' ? 'introduceImage' : 'listPicUrl'
  73. this.setData({
  74. showResult: true,
  75. resultList: res.data.list.map(item => {
  76. console.log(item[img_key], 'item[img_key]')
  77. let img = JSON.parse(item[img_key])
  78. console.log(img)
  79. item.name = item[name_key]
  80. item.id = item[id_key]
  81. item.img = img[0].img
  82. return item
  83. }),
  84. history: loadSearchHistory(),
  85. keyword: params.keyword
  86. })
  87. })
  88. },
  89. toResultDetail (e) {
  90. const { index } = e.currentTarget.dataset
  91. const item = this.data.resultList[index]
  92. if (item.vrLink) {
  93. Router.push({
  94. url: 'scene',
  95. query: {
  96. vr_link: item.vrLink
  97. }
  98. })
  99. } else {
  100. Router.push({
  101. url: 'goodsDetail',
  102. query: {
  103. goods_id: item.id
  104. }
  105. })
  106. }
  107. }
  108. }
  109. })