Search.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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(k.nameCn)">{{ k.nameCn }}
  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. if (this.keyword) {
  154. this.associate.show = false
  155. this.$store.dispatch('resetSearchKeywords')
  156. if (this.searchType === 'product') {
  157. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  158. } else if (this.searchType === 'store') {
  159. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  160. }
  161. }
  162. },
  163. onAssociateClick (word) {
  164. this.click_flag = true
  165. this.keyword = word
  166. this.onSearch()
  167. }
  168. },
  169. created () {
  170. this.$store.dispatch('resetSearchKeywords')
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. @import '~assets/scss/variables';
  176. .form-control{
  177. border-radius: 0;
  178. }
  179. .search-box {
  180. width: 520px;
  181. height: 40px;
  182. position: relative;
  183. .search-input{
  184. width: 372px;
  185. float: left;
  186. }
  187. .search-input, .search-btn {
  188. height: 40px;
  189. border-width: 2px;
  190. }
  191. .select-type{
  192. width: 70px;
  193. float: left;
  194. border: #5078cb 2px solid;
  195. height: 40px;
  196. border-right: none;
  197. margin-right: -1px;
  198. }
  199. .search-btn {
  200. font-size: 16px;
  201. width: 79px;
  202. border-radius: 0;
  203. }
  204. .search-hot ul li a{
  205. color: #838383;
  206. }
  207. .search-hot {
  208. .item {
  209. display: inline-block;
  210. font-size: $font-size-small;
  211. margin-right: $pad;
  212. &.item-first {
  213. color: $red;
  214. }
  215. }
  216. }
  217. .association {
  218. position: absolute;
  219. left: 69px;
  220. top: 100%;
  221. right: 81px;
  222. background: $white;
  223. border: $border;
  224. border-top-width: 0;
  225. z-index: 21;
  226. .item {
  227. padding: 0 15px;
  228. line-height: 30px;
  229. cursor: pointer;
  230. &.active {
  231. background-color: $dark-bg;
  232. }
  233. &:hover {
  234. background-color: $grey-bg;
  235. }
  236. }
  237. .similar-title {
  238. padding: 0 15px;
  239. line-height: 30px;
  240. font-size: 16px;
  241. font-weight: bold;
  242. border-top: 1px solid #ccc;
  243. cursor: default;
  244. }
  245. }
  246. }
  247. </style>