Search.vue 7.7 KB

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