PcSearchHeader.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <div class="search-content com-mobile-header">
  3. <!--<a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>-->
  4. <div class="input-group" style="display: table;">
  5. <input :placeholder="placeholder" class="form-control" type="text" v-model="keyword" @input="onKeywordInput()" @keyup.13="onSearch()" />
  6. <span class="input-group-btn">
  7. <button type="button" class="btn btn-default" @click="onSearch()"><i class="iconfont">&#xe6fc;</i></button>
  8. </span>
  9. </div>
  10. <!--<input type="text" v-model="keyword" @input="onKeywordInput()" :placeholder="placeholder" @keyup.13="onSearch()">-->
  11. <!--<span @click="onSearch()"><i class="iconfont icon-sousuo"></i></span>-->
  12. <ul v-if="emptyStatus && type == 'supplier' && keyword && keyword !== '' && showSimilarWord">
  13. <template v-if="similarList.pBrandEn && similarList.pBrandEn.length">
  14. <li class="title text-ellipse">品牌</li>
  15. <li class="text-ellipse" v-for="brand in similarList.pBrandEn.slice(0, 4)" @click="onSearch(brand.nameEn, 'pBrandEn', $event)">{{brand.nameEn}}</li>
  16. </template>
  17. <template v-if="similarList.kind && similarList.kind.length">
  18. <li class="title text-ellipse">类目</li>
  19. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.kind, 'kind', $event)">{{kind.kind}}</li>
  20. </template>
  21. <template v-if="similarList.pCmpCode && similarList.pCmpCode.length">
  22. <li class="title text-ellipse">型号</li>
  23. <li class="text-ellipse" v-for="code in similarList.pCmpCode.slice(0, 4)" @click="onSearch(code.pCmpCode, 'pCmpCode', $event)">{{code.pCmpCode}}</li>
  24. </template>
  25. </ul>
  26. <ul v-if="emptyStatus && type == 'default' && keyword && keyword !== '' && showSimilarWord">
  27. <template v-if="similarList.brand && similarList.brand.length">
  28. <li class="title text-ellipse">品牌</li>
  29. <li class="text-ellipse" v-for="brand in similarList.brand.slice(0, 4)" @click="onSearch(brand.nameEn, 'brand', $event)">{{brand.nameEn}}</li>
  30. </template>
  31. <template v-if="similarList.kind && similarList.kind.length">
  32. <li class="title text-ellipse">类目</li>
  33. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.nameCn, 'kind', $event)">{{kind.nameCn}}</li>
  34. </template>
  35. <template v-if="similarList.component && similarList.component.length">
  36. <li class="title text-ellipse">型号</li>
  37. <li class="text-ellipse" v-for="code in similarList.component.slice(0, 4)" @click="onSearch(code.code, 'code', $event)">{{code.code}}</li>
  38. </template>
  39. </ul>
  40. </div>
  41. </template>
  42. <script>
  43. import {scrollTo} from '~utils/scroll'
  44. export default {
  45. props: {
  46. placeholder: {
  47. type: String,
  48. default: '请输入要查找的内容'
  49. },
  50. similarUrl: { // 联想词url
  51. type: String,
  52. default: '/search/similarKeywords'
  53. },
  54. type: { // 搜索类型
  55. type: String,
  56. default: 'default'
  57. },
  58. showSimilar: { // 是否显示联想词
  59. type: Boolean,
  60. default: true
  61. },
  62. outerKeyword: {
  63. type: String,
  64. default: ''
  65. }
  66. },
  67. data () {
  68. return {
  69. keyword: '',
  70. similarList: {},
  71. showSimilarWord: false,
  72. searchKeyword: '',
  73. clickCount: 0
  74. }
  75. },
  76. watch: {
  77. outerKeyword: {
  78. handler: function (val) {
  79. this.keyword = val
  80. },
  81. immediate: true
  82. }
  83. },
  84. mounted () {
  85. this.$nextTick(() => {
  86. document.onclick = () => {
  87. this.showSimilarWord = false
  88. }
  89. })
  90. },
  91. computed: {
  92. emptyStatus () {
  93. let similarList = this.similarList
  94. if (this.type === 'supplier') {
  95. return (similarList.pCmpCode && similarList.pCmpCode.length) ||
  96. (similarList.pBrandEn && similarList.pBrandEn.length) ||
  97. (similarList.kind && similarList.kind.length)
  98. } else if (this.type === 'default') {
  99. return (similarList.component && similarList.component.length) ||
  100. (similarList.brand && similarList.brand.length) ||
  101. (similarList.kind && similarList.kind.length)
  102. }
  103. }
  104. },
  105. methods: {
  106. onSearch: function (key, type, e) {
  107. if (e) {
  108. e.stopPropagation()
  109. }
  110. // if (key === this.searchKeyword || this.keyword === this.searchKeyword) {
  111. // return
  112. // }
  113. if (key) {
  114. this.keyword = key
  115. this.$emit('searchAction', {
  116. keyword: this.keyword,
  117. type: type
  118. })
  119. } else {
  120. let sType = null
  121. if (this.type === 'default' && this.keyword && this.keyword !== '' && this.similarList.component) {
  122. if (this.similarList.component[0] && this.keyword === this.similarList.component[0].code) {
  123. sType = 'code'
  124. } else if (this.similarList.brand[0] && this.keyword === this.similarList.brand[0].nameEn) {
  125. sType = 'brand'
  126. } else if (this.similarList.kind[0] && this.keyword === this.similarList.kind[0].nameCn) {
  127. sType = 'kind'
  128. } else {
  129. let arr = [...this.similarList.component, ...this.similarList.brand, ...this.similarList.kind]
  130. if (arr[0]) {
  131. if (arr[0].code) {
  132. this.keyword = arr[0].code
  133. sType = 'code'
  134. } else if (arr[0].nameEn) {
  135. this.keyword = arr[0].nameEn
  136. sType = 'brand'
  137. } else if (arr[0].nameCn) {
  138. this.keyword = arr[0].nameCn
  139. sType = 'kind'
  140. }
  141. }
  142. }
  143. } else if (this.type === 'supplier' && this.keyword && this.keyword !== '' && this.similarList.pCmpCode) {
  144. if (this.similarList.pCmpCode[0] && this.keyword === this.similarList.pCmpCode[0].pCmpCode) {
  145. sType = 'pCmpCode'
  146. } else if (this.similarList.pBrandEn[0] && this.keyword === this.similarList.pBrandEn[0].nameEn) {
  147. sType = 'pBrandEn'
  148. } else if (this.similarList.kind[0] && this.keyword === this.similarList.kind[0].kind) {
  149. sType = 'kind'
  150. } else {
  151. let arr = [...this.similarList.pCmpCode, ...this.similarList.pBrandEn, ...this.similarList.kind]
  152. if (arr[0]) {
  153. if (arr[0].pCmpCode) {
  154. this.keyword = arr[0].pCmpCode
  155. sType = 'pCmpCode'
  156. } else if (arr[0].nameEn) {
  157. this.keyword = arr[0].nameEn
  158. sType = 'pBrandEn'
  159. } else if (arr[0].kind) {
  160. this.keyword = arr[0].kind
  161. sType = 'kind'
  162. }
  163. }
  164. }
  165. }
  166. this.$emit('searchAction', {
  167. keyword: this.keyword,
  168. type: sType
  169. })
  170. }
  171. this.searchKeyword = this.keyword
  172. scrollTo('body', 10)
  173. this.showSimilarWord = false
  174. },
  175. onKeywordInput: function () {
  176. this.clickCount ++
  177. let count = this.clickCount
  178. let timer = setTimeout(() => {
  179. this.getSimilarList(count, timer)
  180. }, 300)
  181. },
  182. getSimilarList: function (clickCount, timer) {
  183. clearTimeout(timer)
  184. if (this.showSimilar && this.keyword && this.keyword !== '' && clickCount === this.clickCount) {
  185. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  186. res => {
  187. this.similarList = res.data
  188. this.showSimilarWord = true
  189. }
  190. )
  191. }
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .search-content {
  198. position: relative;
  199. color: #333;
  200. z-index: 991;
  201. background: #fff;
  202. input {
  203. line-height: normal;
  204. }
  205. ul {
  206. width: 100%;
  207. background: #fff;
  208. position: absolute;
  209. left: 0;
  210. top: 36px;
  211. border: 1px solid #ccc;
  212. border-radius: 5px;
  213. max-height: 400px;
  214. overflow-y: auto;
  215. li {
  216. height: 36px;
  217. line-height: 36px;
  218. padding: 0 10px;
  219. font-size: 14px;
  220. &:hover {
  221. background: #f6f5f5;
  222. cursor: pointer;
  223. }
  224. &.title {
  225. color: #666;
  226. border-bottom: 1px solid #ddd;
  227. font-weight: bold;
  228. background: #f6f5f5;
  229. }
  230. }
  231. }
  232. }
  233. </style>