PcSearchHeader.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. <!--<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. export default {
  44. props: {
  45. placeholder: {
  46. type: String,
  47. default: '请输入要查找的内容'
  48. },
  49. similarUrl: { // 联想词url
  50. type: String,
  51. default: '/search/similarKeywords'
  52. },
  53. type: { // 搜索类型
  54. type: String,
  55. default: 'default'
  56. },
  57. showSimilar: { // 是否显示联想词
  58. type: Boolean,
  59. default: true
  60. },
  61. outerKeyword: {
  62. type: String,
  63. default: ''
  64. }
  65. },
  66. data () {
  67. return {
  68. keyword: '',
  69. similarList: {},
  70. showSimilarWord: false,
  71. searchKeyword: '',
  72. clickCount: 0
  73. }
  74. },
  75. watch: {
  76. outerKeyword: {
  77. handler: function (val) {
  78. this.keyword = val
  79. },
  80. immediate: true
  81. }
  82. },
  83. mounted () {
  84. this.$nextTick(() => {
  85. document.onclick = () => {
  86. this.showSimilarWord = false
  87. }
  88. })
  89. },
  90. computed: {
  91. emptyStatus () {
  92. let similarList = this.similarList
  93. if (this.type === 'supplier') {
  94. return (similarList.pCmpCode && similarList.pCmpCode.length) ||
  95. (similarList.pBrandEn && similarList.pBrandEn.length) ||
  96. (similarList.kind && similarList.kind.length)
  97. } else if (this.type === 'default') {
  98. return (similarList.component && similarList.component.length) ||
  99. (similarList.brand && similarList.brand.length) ||
  100. (similarList.kind && similarList.kind.length)
  101. }
  102. }
  103. },
  104. methods: {
  105. onSearch: function (key, type, e) {
  106. if (e) {
  107. e.stopPropagation()
  108. }
  109. // if (key === this.searchKeyword || this.keyword === this.searchKeyword) {
  110. // return
  111. // }
  112. if (key) {
  113. this.keyword = key
  114. this.$emit('searchAction', {
  115. keyword: this.keyword,
  116. type: type
  117. })
  118. } else {
  119. let sType = null
  120. if (this.type === 'default' && this.keyword && this.keyword !== '' && this.similarList.component) {
  121. 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.component, ...this.similarList.brand, ...this.similarList.kind]
  129. if (arr[0]) {
  130. if (arr[0].nameEn) {
  131. this.keyword = arr[0].nameEn
  132. sType = 'brand'
  133. } else if (arr[0].nameCn) {
  134. this.keyword = arr[0].nameCn
  135. sType = 'kind'
  136. } else if (arr[0].code) {
  137. this.keyword = arr[0].code
  138. sType = 'code'
  139. }
  140. }
  141. }
  142. } else if (this.type === 'supplier' && this.keyword && this.keyword !== '' && this.similarList.pCmpCode) {
  143. if (this.similarList.pBrandEn[0] && this.keyword === this.similarList.pBrandEn[0].nameEn) {
  144. sType = 'pBrandEn'
  145. } else if (this.similarList.kind[0] && this.keyword === this.similarList.kind[0].kind) {
  146. sType = 'kind'
  147. } else if (this.similarList.pCmpCode[0] && this.keyword === this.similarList.pCmpCode[0].pCmpCode) {
  148. sType = 'pCmpCode'
  149. } else {
  150. let arr = [...this.similarList.pCmpCode, ...this.similarList.pBrandEn, ...this.similarList.kind]
  151. if (arr[0]) {
  152. if (arr[0].nameEn) {
  153. this.keyword = arr[0].nameEn
  154. sType = 'pBrandEn'
  155. } else if (arr[0].kind) {
  156. this.keyword = arr[0].kind
  157. sType = 'kind'
  158. } else if (arr[0].pCmpCode) {
  159. this.keyword = arr[0].pCmpCode
  160. sType = 'pCmpCode'
  161. }
  162. }
  163. }
  164. }
  165. this.$emit('searchAction', {
  166. keyword: this.keyword,
  167. type: sType
  168. })
  169. }
  170. this.searchKeyword = this.keyword
  171. this.showSimilarWord = false
  172. },
  173. onKeywordInput: function () {
  174. this.clickCount ++
  175. let count = this.clickCount
  176. let timer = setTimeout(() => {
  177. this.getSimilarList(count, timer)
  178. }, 300)
  179. },
  180. getSimilarList: function (clickCount, timer) {
  181. clearTimeout(timer)
  182. if (this.showSimilar && this.keyword && this.keyword !== '' && clickCount === this.clickCount) {
  183. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  184. res => {
  185. this.similarList = res.data
  186. this.showSimilarWord = true
  187. }
  188. )
  189. }
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="scss" scoped>
  195. .search-content-pc {
  196. position: relative;
  197. color: #333;
  198. z-index: 991;
  199. background: transparent;
  200. height: auto;
  201. line-height: normal;
  202. padding: 0;
  203. input {
  204. line-height: normal;
  205. }
  206. ul {
  207. width: 258px;
  208. background: #fff;
  209. position: absolute;
  210. left: 558px;
  211. top: 28px;
  212. border: 1px solid #ccc;
  213. border-radius: 5px;
  214. max-height: 400px;
  215. overflow-y: auto;
  216. li {
  217. height: 36px;
  218. line-height: 36px;
  219. padding: 0 10px;
  220. font-size: 14px;
  221. &:hover {
  222. background: #f6f5f5;
  223. cursor: pointer;
  224. }
  225. &.title {
  226. color: #666;
  227. border-bottom: 1px solid #ddd;
  228. font-weight: bold;
  229. background: #f6f5f5;
  230. }
  231. }
  232. }
  233. }
  234. </style>