Search.vue 8.4 KB

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