Search.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <div class="search-box">
  3. <div class="input-group">
  4. <select @change="onSelectTypeChange" class="form-control select-type select-adder">
  5. <option value="">产品</option>
  6. <option value="">店铺</option>
  7. </select>
  8. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  9. placeholder="型号/类目/品牌"
  10. @focus.stop.prevent="onFocus()"
  11. @blur.stop.prevent="onBlur()"
  12. @keyup.40="onSelectChange(1)"
  13. @keyup.38="onSelectChange(-1)"
  14. @keyup.13="onSearch()"/>
  15. <span class="input-group-btn" @click="onSearch()">
  16. <button class="btn btn-primary search-btn" type="button">搜&nbsp;索</button>
  17. </span>
  18. </div>
  19. <ul class="association" v-show="showAssociate && searchType == 'product'"
  20. @mouseenter="associate.focus=true" @mouseleave="associate.focus=false">
  21. <li v-if="similarKeywords.data.component && similarKeywords.data.component.length > 0" class="similar-title">型号:</li>
  22. <li v-for="(k, index) in similarKeywords.data.component" :key="k.code" class="item"
  23. :class="{'active': index==associate.activeIndex}"
  24. @click.stop.prevent="onAssociateClick(k.code)">{{ k.code }}
  25. </li>
  26. <li v-if="similarKeywords.data.brand && similarKeywords.data.brand.length > 0" class="similar-title">品牌:</li>
  27. <li v-for="(k, index) in similarKeywords.data.brand" :key="k.nameCn" class="item"
  28. :class="{'active': index==associate.activeIndex}"
  29. @click.stop.prevent="onAssociateClick(isCnStart() ? k.nameCn : k.nameEn)">{{ isCnStart() ? k.nameCn : k.nameEn }}
  30. </li>
  31. <li v-if="similarKeywords.data.kind && similarKeywords.data.kind.length > 0" class="similar-title">类目:</li>
  32. <li v-for="(k, index) in similarKeywords.data.kind" :key="k.nameCn" class="item"
  33. :class="{'active': index==associate.activeIndex}"
  34. @click.stop.prevent="onAssociateClick(k.nameCn)">{{ k.nameCn }}
  35. </li>
  36. </ul>
  37. <div class="search-hot">
  38. <ul class="list-untyled">
  39. <li class="item item-first">热门搜索</li>
  40. <li class="item" v-for="w in hotwords">
  41. <nuxt-link :to="w.url" target="_blank">{{ w.name }}</nuxt-link>
  42. </li>
  43. </ul>
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. export default {
  49. name: 'search-box',
  50. data () {
  51. return {
  52. keyword: '',
  53. associate: {
  54. focus: false,
  55. show: false,
  56. activeIndex: null
  57. },
  58. click_flag: false,
  59. searchType: 'product'
  60. }
  61. },
  62. computed: {
  63. similarKeywords () {
  64. return this.$store.state.search.keywords
  65. },
  66. showAssociate () {
  67. return this.keyword &&
  68. this.associate.show &&
  69. this.similarKeywords.data &&
  70. (this.similarKeywords.data.brand || this.similarKeywords.data.component || this.similarKeywords.data.kind)
  71. }
  72. },
  73. props: {
  74. hotwords: {
  75. type: Array,
  76. default () {
  77. return [{
  78. name: 'SCT2080KEC',
  79. url: '/product/component/1100400300009990'
  80. }, {
  81. name: '电池组',
  82. url: '/product/kind/346'
  83. }, {
  84. name: 'Vishay',
  85. url: '/product/brand/30327265e42a871be050007f01003d96'
  86. }, {
  87. name: 'Panasonic',
  88. url: '/product/brand/30327265e47d871be050007f01003d96'
  89. }]
  90. }
  91. }
  92. },
  93. watch: {
  94. 'keyword': {
  95. handler (val, oldVal) {
  96. let keywords = this.similarKeywords.data
  97. if (!keywords || !keywords.length || this.associate.activeIndex === null || val !== keywords[this.associate.activeIndex]) {
  98. this.onChange()
  99. }
  100. }
  101. }
  102. },
  103. methods: {
  104. onSelectTypeChange: function (e) {
  105. let type = e.target[e.target.selectedIndex].innerHTML
  106. if (type === '产品') {
  107. this.searchType = 'product'
  108. } else if (type === '店铺') {
  109. this.searchType = 'store'
  110. }
  111. },
  112. onFocus () {
  113. this.associate.show = true
  114. },
  115. onBlur () {
  116. this.associate.show = this.associate.focus
  117. },
  118. onSelectChange (count) {
  119. let keywords = this.similarKeywords.data
  120. if (keywords && keywords.length) {
  121. let index = this.associate.activeIndex
  122. if (index === null) {
  123. index = -1
  124. }
  125. index += count
  126. if (index >= keywords.length) {
  127. index = 0
  128. } else if (index < 0) {
  129. index = keywords.length - 1
  130. }
  131. this.associate.activeIndex = index
  132. this.keyword = keywords[index]
  133. }
  134. },
  135. onChange () {
  136. this.associate.activeIndex = null
  137. if (!this.keyword) {
  138. this.associate.show = false
  139. this.$store.dispatch('resetSearchKeywords')
  140. } else {
  141. this.searchKeywords()
  142. }
  143. if (this.click_flag) {
  144. this.associate.show = false
  145. this.click_flag = false
  146. }
  147. },
  148. searchKeywords () {
  149. this.associate.show = true
  150. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  151. },
  152. onSearch () {
  153. document.getElementsByClassName('search-input')[0].blur()
  154. if (this.keyword) {
  155. this.associate.show = false
  156. this.$store.dispatch('resetSearchKeywords')
  157. if (this.searchType === 'product') {
  158. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  159. } else if (this.searchType === 'store') {
  160. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  161. }
  162. }
  163. },
  164. onAssociateClick (word) {
  165. this.click_flag = true
  166. this.keyword = word
  167. this.onSearch()
  168. },
  169. isCnStart () {
  170. if (this.keyword && this.keyword.length > 0) {
  171. return this.keyword.charCodeAt(0) > 255
  172. }
  173. }
  174. },
  175. created () {
  176. this.$store.dispatch('resetSearchKeywords')
  177. }
  178. }
  179. </script>
  180. <style lang="scss" scoped>
  181. @import '~assets/scss/variables';
  182. .form-control{
  183. border-radius: 0;
  184. }
  185. .search-box {
  186. width: 520px;
  187. height: 40px;
  188. position: relative;
  189. .search-input{
  190. width: 372px;
  191. float: left;
  192. }
  193. .search-input, .search-btn {
  194. height: 40px;
  195. border-width: 2px;
  196. }
  197. .select-type{
  198. width: 70px;
  199. float: left;
  200. border: #5078cb 2px solid;
  201. height: 40px;
  202. border-right: none;
  203. margin-right: -1px;
  204. }
  205. .search-btn {
  206. font-size: 16px;
  207. width: 79px;
  208. border-radius: 0;
  209. }
  210. .search-hot ul li a{
  211. color: #838383;
  212. }
  213. .search-hot {
  214. .item {
  215. display: inline-block;
  216. font-size: $font-size-small;
  217. margin-right: $pad;
  218. &.item-first {
  219. color: $red;
  220. }
  221. }
  222. }
  223. .association {
  224. position: absolute;
  225. left: 69px;
  226. top: 100%;
  227. right: 81px;
  228. background: $white;
  229. border: $border;
  230. border-top-width: 0;
  231. z-index: 21;
  232. .item {
  233. padding: 0 15px;
  234. line-height: 30px;
  235. cursor: pointer;
  236. &.active {
  237. background-color: $dark-bg;
  238. }
  239. &:hover {
  240. background-color: $grey-bg;
  241. }
  242. }
  243. .similar-title {
  244. padding: 0 15px;
  245. line-height: 30px;
  246. font-size: 16px;
  247. font-weight: bold;
  248. border-top: 1px solid #ccc;
  249. cursor: default;
  250. }
  251. }
  252. }
  253. </style>