Search.vue 7.4 KB

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