PcSearchHeader.vue 8.5 KB

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