PcSearchHeader.vue 7.0 KB

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