search.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = []
  78. // try {
  79. // img = JSON.parse(item[img_key])
  80. // } catch (err) {
  81. // console.log(err, '解析图片列表出错')
  82. // }
  83. // console.log(img)
  84. item.name = item[name_key]
  85. item.id = item[id_key]
  86. item.img = item.companyLogo
  87. return item
  88. }),
  89. history: loadSearchHistory(),
  90. keyword: params.keyword
  91. })
  92. })
  93. },
  94. toResultDetail (e) {
  95. const { index } = e.currentTarget.dataset
  96. const item = this.data.resultList[index]
  97. if (item.vrLink) {
  98. Router.push({
  99. url: 'scene',
  100. query: {
  101. vr_link: item.vrLink
  102. }
  103. })
  104. } else {
  105. Router.push({
  106. url: 'goodsDetail',
  107. query: {
  108. goods_id: item.id
  109. }
  110. })
  111. }
  112. }
  113. }
  114. })