Search.vue 7.9 KB

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