PcSearchHeader.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. storeTypes: {
  64. type: String,
  65. default: ''
  66. }
  67. // storeTypes ''
  68. },
  69. data() {
  70. return {
  71. keyword: '',
  72. similarList: {},
  73. showSimilarWord: false,
  74. searchKeyword: '',
  75. clickCount: 0
  76. }
  77. },
  78. watch: {
  79. outerKeyword: {
  80. handler: function (val) {
  81. this.keyword = val
  82. },
  83. immediate: true
  84. }
  85. },
  86. mounted() {
  87. this.$nextTick(() => {
  88. document.onclick = () => {
  89. this.showSimilarWord = false
  90. }
  91. })
  92. },
  93. computed: {
  94. emptyStatus() {
  95. let similarList = this.similarList
  96. return (similarList.component && similarList.component.length) ||
  97. (similarList.brand && similarList.brand.length) ||
  98. (similarList.kind && similarList.kind.length) ||
  99. (similarList.store && similarList.store.length)
  100. }
  101. },
  102. methods: {
  103. onSearch: function (key, type, e) {
  104. if (e) {
  105. e.stopPropagation()
  106. }
  107. // if (key === this.searchKeyword || this.keyword === this.searchKeyword) {
  108. // return
  109. // }
  110. if (key || !this.useMatchRule) {
  111. this.keyword = key || this.keyword
  112. this.$emit('searchAction', {
  113. keyword: this.keyword,
  114. type: type
  115. })
  116. } else {
  117. let sType = null
  118. if (this.keyword && this.keyword !== '' && this.similarList.component) {
  119. if (this.similarList.store[0] && this.keyword === this.similarList.store[0].name) {
  120. sType = 'store'
  121. } else if (this.similarList.brand[0] && this.keyword === this.similarList.brand[0].nameEn) {
  122. sType = 'brand'
  123. } else if (this.similarList.kind[0] && this.keyword === this.similarList.kind[0].nameCn) {
  124. sType = 'kind'
  125. } else if (this.similarList.component[0] && this.keyword === this.similarList.component[0].code) {
  126. sType = 'code'
  127. } else {
  128. let arr = [...this.similarList.store, ...this.similarList.brand, ...this.similarList.kind, ...this.similarList.component]
  129. if (arr[0]) {
  130. if (arr[0].name) {
  131. this.keyword = arr[0].name
  132. sType = 'store'
  133. } else if (arr[0].nameEn) {
  134. this.keyword = arr[0].nameEn
  135. sType = 'brand'
  136. } else if (arr[0].nameCn) {
  137. this.keyword = arr[0].nameCn
  138. sType = 'kind'
  139. } else if (arr[0].code) {
  140. this.keyword = arr[0].code
  141. sType = 'code'
  142. }
  143. }
  144. }
  145. }
  146. this.$emit('searchAction', {
  147. keyword: this.keyword,
  148. type: sType
  149. })
  150. }
  151. this.searchKeyword = this.keyword
  152. this.showSimilarWord = false
  153. },
  154. onKeywordInput: function () {
  155. this.clickCount++
  156. let count = this.clickCount
  157. let timer = setTimeout(() => {
  158. this.getSimilarList(count, timer)
  159. }, 300)
  160. },
  161. getSimilarList: function (clickCount, timer) {
  162. clearTimeout(timer)
  163. if (this.showSimilar && this.keyword && this.keyword !== '' && clickCount === this.clickCount) {
  164. if (this.storeTypes === '') { // 供应商和求购
  165. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  166. res => {
  167. this.similarList = res.data
  168. this.showSimilarWord = true
  169. }
  170. )
  171. } else { // 走店铺
  172. this.$http.get(this.similarUrl, {params: {keyword: this.keyword, storeTypes:this.storeTypes, type:'all'}}).then(
  173. res => {
  174. this.similarList = res.data
  175. this.showSimilarWord = true
  176. }
  177. )
  178. }
  179. }
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .search-content-pc {
  186. position: relative;
  187. color: #333;
  188. z-index: 991;
  189. background: transparent;
  190. height: auto;
  191. line-height: normal;
  192. padding: 0;
  193. input {
  194. line-height: normal;
  195. }
  196. ul {
  197. width: 283px;
  198. background: #fff;
  199. position: absolute;
  200. left: 533px;
  201. top: 28px;
  202. border: 1px solid #ccc;
  203. border-radius: 5px;
  204. max-height: 400px;
  205. overflow-y: auto;
  206. li {
  207. height: 36px;
  208. line-height: 36px;
  209. padding: 0 10px;
  210. font-size: 14px;
  211. color: #333;
  212. &:hover {
  213. background: #f6f5f5;
  214. cursor: pointer;
  215. }
  216. &.title {
  217. color: #666;
  218. border-bottom: 1px solid #ddd;
  219. font-weight: bold;
  220. background: #f6f5f5;
  221. }
  222. }
  223. }
  224. }
  225. </style>