Search.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <div class="search-box" :class="{'search-box2': !SelectItem}">
  3. <div class="input-group">
  4. <select v-model="searchType" class="form-control select-type select-adder" v-if="SelectItem">
  5. <option value="product">产品</option>
  6. <option value="store">店铺</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()" style="z-index: 10">
  16. <button class="btn btn-primary search-btn" type="button" :class="{'Isblue':!SelectItem}">搜&nbsp;索</button>
  17. </span>
  18. </div>
  19. <ul class="association" :class="{'association2': !SelectItem}" 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" 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" 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" 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" v-if="SelectItem">
  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. props: {
  57. SelectItem: {
  58. type: Boolean,
  59. default: true
  60. }
  61. },
  62. data () {
  63. return {
  64. keyword: '',
  65. associate: {
  66. focus: false,
  67. show: false,
  68. activeIndex: null
  69. },
  70. click_flag: false,
  71. searchType: 'product'
  72. }
  73. },
  74. computed: {
  75. hotDevice () {
  76. return this.$store.state.hotSearchDevice.hot.data
  77. },
  78. hotBrand () {
  79. return this.$store.state.hotSearchBrand.hot.data
  80. },
  81. similarKeywords () {
  82. return this.$store.state.search.keywords
  83. },
  84. showAssociate () {
  85. return this.keyword &&
  86. this.associate.show &&
  87. this.similarKeywords.data &&
  88. (this.similarKeywords.data.brand || this.similarKeywords.data.component || this.similarKeywords.data.kind)
  89. },
  90. hotSearchData () {
  91. let list = this.$store.state.floor.list_v3.data
  92. let obj = {}
  93. if (list && list.length) {
  94. for (let i = 0; i < list.length; i++) {
  95. if (list[i].floorNumber === 2) {
  96. obj = list[i]
  97. }
  98. }
  99. // obj = list.find(item => item.floorNumber === 2) || {}
  100. }
  101. return obj
  102. },
  103. ifFloorsHotSearchInValid () {
  104. let obj = this.hotSearchData
  105. let tmp = false
  106. if (obj.items && obj.items.length) {
  107. for (let i = 0; i < obj.items.length; i++) {
  108. if (!obj.items[i].body || obj.items[i].body === '') {
  109. tmp = obj.items[i]
  110. break
  111. }
  112. }
  113. // let result = obj.items.find(item => !item.body || item.body === '') || true
  114. return tmp || true
  115. }
  116. return true
  117. }
  118. },
  119. watch: {
  120. 'keyword': {
  121. handler (val, oldVal) {
  122. let keywords = this.similarKeywords.data
  123. if (!keywords || !keywords.length || this.associate.activeIndex === null || val !== keywords[this.associate.activeIndex]) {
  124. this.onChange()
  125. }
  126. }
  127. }
  128. },
  129. methods: {
  130. // onSelectTypeChange: function (e) {
  131. // let type = e.target[e.target.selectedIndex].innerHTML
  132. // if (type === '产品') {
  133. // this.searchType = 'product'
  134. // } else if (type === '店铺') {
  135. // this.searchType = 'store'
  136. // }
  137. // },
  138. onFocus () {
  139. this.associate.show = true
  140. },
  141. onBlur () {
  142. this.associate.show = this.associate.focus
  143. },
  144. onSelectChange (count) {
  145. let keywords = this.similarKeywords.data
  146. if (keywords && keywords.length) {
  147. let index = this.associate.activeIndex
  148. if (index === null) {
  149. index = -1
  150. }
  151. index += count
  152. if (index >= keywords.length) {
  153. index = 0
  154. } else if (index < 0) {
  155. index = keywords.length - 1
  156. }
  157. this.associate.activeIndex = index
  158. this.keyword = keywords[index]
  159. }
  160. },
  161. onChange () {
  162. this.associate.activeIndex = null
  163. if (!this.keyword) {
  164. this.associate.show = false
  165. this.$store.dispatch('resetSearchKeywords')
  166. } else {
  167. this.searchKeywords()
  168. }
  169. if (this.click_flag) {
  170. this.associate.show = false
  171. this.click_flag = false
  172. }
  173. },
  174. searchKeywords () {
  175. if (this.searchType === 'product') {
  176. this.associate.show = true
  177. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  178. }
  179. },
  180. onSearch () {
  181. document.getElementsByClassName('search-input')[0].blur()
  182. if (this.keyword) {
  183. this.associate.show = false
  184. this.$store.dispatch('resetSearchKeywords')
  185. if (this.searchType === 'product') {
  186. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  187. } else if (this.searchType === 'store') {
  188. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  189. }
  190. }
  191. },
  192. onAssociateClick (word) {
  193. this.click_flag = true
  194. this.keyword = word
  195. this.onSearch()
  196. },
  197. isCnStart () {
  198. if (this.keyword && this.keyword.length > 0) {
  199. return this.keyword.charCodeAt(0) > 255
  200. }
  201. }
  202. },
  203. created () {
  204. this.$store.dispatch('resetSearchKeywords')
  205. }
  206. }
  207. </script>
  208. <style lang="scss" scoped type="text/scss">
  209. @import '~assets/scss/variables';
  210. .form-control{
  211. border-radius: 0;
  212. }
  213. .search-box {
  214. width: 520px;
  215. height: 40px;
  216. position: relative;
  217. .search-input{
  218. width: 372px;
  219. float: left;
  220. }
  221. .search-input, .search-btn {
  222. height: 40px;
  223. border-width: 2px;
  224. }
  225. .select-type{
  226. width: 70px;
  227. float: left;
  228. border: #5078cb 2px solid;
  229. height: 40px;
  230. border-right: none;
  231. margin-right: -1px;
  232. }
  233. .search-btn {
  234. font-size: 16px;
  235. width: 79px;
  236. border-radius: 0;
  237. }
  238. .Isblue{
  239. vertical-align: middle;
  240. display: inline-block;
  241. text-align: center;
  242. width: 72px;
  243. border-left: 0;
  244. background: #d3e1fc;
  245. color: #5078cb;
  246. text-align: center;
  247. font-size: 14px;
  248. position: absolute;
  249. right: 0;
  250. top: 0;
  251. &:hover {
  252. background: #d2272d;
  253. color: #fff;
  254. transition: all .3s;
  255. }
  256. }
  257. .search-hot ul{
  258. line-height: 12px;
  259. }
  260. .search-hot ul li a{
  261. color: #838383;
  262. }
  263. .search-hot {
  264. margin-top:5px;
  265. .item {
  266. display: inline-block;
  267. max-width:22%;
  268. text-align: center;
  269. vertical-align: middle;
  270. font-size: $font-size-small;
  271. padding-right: $pad;
  272. a{
  273. display:block;
  274. overflow: hidden;
  275. text-overflow: ellipsis;
  276. white-space: nowrap;
  277. }
  278. &.item-first {
  279. width:12%;
  280. color: $red;
  281. }
  282. }
  283. }
  284. .association {
  285. position: absolute;
  286. left: 69px;
  287. top: 100%;
  288. right: 81px;
  289. background: $white;
  290. border: $border;
  291. border-top-width: 0;
  292. z-index: 21;
  293. .item {
  294. padding: 0 15px;
  295. line-height: 30px;
  296. cursor: pointer;
  297. &.active {
  298. background-color: $dark-bg;
  299. }
  300. &:hover {
  301. background-color: $grey-bg;
  302. }
  303. }
  304. .similar-title {
  305. padding: 0 15px;
  306. line-height: 30px;
  307. font-size: 16px;
  308. font-weight: bold;
  309. border-top: 1px solid #ccc;
  310. cursor: default;
  311. }
  312. }
  313. .association2 {
  314. left: 2px;
  315. right: 71px
  316. }
  317. }
  318. .search-box2 {
  319. width: 442px;
  320. }
  321. </style>