Search.vue 8.8 KB

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