SearchHeader.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <div class="search-content com-mobile-header">
  3. <a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>
  4. <input type="text" v-model="keyword" @input="getSimilarList()" :placeholder="placeholder" @keyup.13="onSearch()">
  5. <span @click="onSearch()"><i class="iconfont icon-sousuo"></i></span>
  6. <ul v-if="emptyStatus && type == 'supplier' && keyword && keyword !== '' && showSimilarWord">
  7. <template v-if="similarList.pCmpCode && similarList.pCmpCode.length">
  8. <li class="title text-ellipse">型号</li>
  9. <li class="text-ellipse" v-for="code in similarList.pCmpCode.slice(0, 4)" @click="onSearch(code.pCmpCode, 'pCmpCode', $event)">{{code.pCmpCode}}</li>
  10. </template>
  11. <template v-if="similarList.pBrandEn && similarList.pBrandEn.length">
  12. <li class="title text-ellipse">品牌</li>
  13. <li class="text-ellipse" v-for="brand in similarList.pBrandEn.slice(0, 4)" @click="onSearch(brand.nameCn, 'pBrandEn', $event)">{{brand.nameCn}}</li>
  14. </template>
  15. <template v-if="similarList.kind && similarList.kind.length">
  16. <li class="title text-ellipse">类目</li>
  17. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.kind, 'kind', $event)">{{kind.kind}}</li>
  18. </template>
  19. </ul>
  20. <ul v-if="emptyStatus && type == 'default' && keyword && keyword !== '' && showSimilarWord">
  21. <template v-if="similarList.component && similarList.component.length">
  22. <li class="title text-ellipse">型号</li>
  23. <li class="text-ellipse" v-for="code in similarList.component.slice(0, 4)" @click="onSearch(code.code, 'code', $event)">{{code.code}}</li>
  24. </template>
  25. <template v-if="similarList.brand && similarList.brand.length">
  26. <li class="title text-ellipse">品牌</li>
  27. <li class="text-ellipse" v-for="brand in similarList.brand.slice(0, 4)" @click="onSearch(brand.nameCn, 'brand', $event)">{{brand.nameCn}}</li>
  28. </template>
  29. <template v-if="similarList.kind && similarList.kind.length">
  30. <li class="title text-ellipse">类目</li>
  31. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.nameCn, 'kind', $event)">{{kind.nameCn}}</li>
  32. </template>
  33. </ul>
  34. </div>
  35. </template>
  36. <script>
  37. import {scrollTo} from '~utils/scroll'
  38. export default {
  39. props: {
  40. placeholder: {
  41. type: String,
  42. default: '请输入要查找的内容'
  43. },
  44. similarUrl: { // 联想词url
  45. type: String,
  46. default: '/search/similarKeywords'
  47. },
  48. type: { // 搜索类型
  49. type: String,
  50. default: 'default'
  51. },
  52. showSimilar: { // 是否显示联想词
  53. type: Boolean,
  54. default: true
  55. }
  56. },
  57. data () {
  58. return {
  59. keyword: '',
  60. similarList: {},
  61. showSimilarWord: false,
  62. searchKeyword: ''
  63. }
  64. },
  65. mounted () {
  66. this.$nextTick(() => {
  67. document.onclick = () => {
  68. this.showSimilarWord = false
  69. }
  70. })
  71. },
  72. computed: {
  73. emptyStatus () {
  74. let similarList = this.similarList
  75. if (this.type === 'supplier') {
  76. return (similarList.pCmpCode && similarList.pCmpCode.length) ||
  77. (similarList.pBrandEn && similarList.pBrandEn.length) ||
  78. (similarList.kind && similarList.kind.length)
  79. } else if (this.type === 'default') {
  80. return (similarList.component && similarList.component.length) ||
  81. (similarList.brand && similarList.brand.length) ||
  82. (similarList.kind && similarList.kind.length)
  83. }
  84. }
  85. },
  86. methods: {
  87. onSearch: function (key, type, e) {
  88. if (e) {
  89. e.stopPropagation()
  90. }
  91. if (key === this.searchKeyword || this.keyword === this.searchKeyword) {
  92. return
  93. }
  94. if (key) {
  95. this.keyword = key
  96. this.$emit('searchAction', {
  97. keyword: this.keyword,
  98. type: type
  99. })
  100. } else {
  101. this.$emit('searchAction', {
  102. keyword: this.keyword
  103. })
  104. }
  105. this.searchKeyword = this.keyword
  106. scrollTo('body', 10)
  107. this.showSimilarWord = false
  108. },
  109. getSimilarList: function () {
  110. if (this.showSimilar && this.keyword && this.keyword !== '') {
  111. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  112. res => {
  113. this.similarList = res.data
  114. this.showSimilarWord = true
  115. }
  116. )
  117. }
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. .search-content {
  124. color: #333;
  125. input {
  126. margin: 0 0 0 .5rem;
  127. line-height: normal;
  128. }
  129. ul {
  130. width: 6.48rem;
  131. background: #fff;
  132. position: absolute;
  133. left: .6rem;
  134. top: .72rem;
  135. border: 1px solid #ccc;
  136. border-radius: .05rem;
  137. li {
  138. height: .6rem;
  139. line-height: .6rem;
  140. padding: 0 .1rem;
  141. font-size: .26rem;
  142. &.title {
  143. color: #666;
  144. border-bottom: 1px solid #ddd;
  145. font-weight: bold;
  146. background: #f6f5f5;
  147. }
  148. }
  149. }
  150. }
  151. </style>