Search.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <div class="search-box" :class="{'search-box2': !SelectItem}">
  3. <div class="input-group">
  4. <select @change="onSelectTypeChange" class="form-control select-type select-adder" v-if="SelectItem">
  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()" 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. this.associate.show = true
  176. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  177. },
  178. onSearch () {
  179. document.getElementsByClassName('search-input')[0].blur()
  180. if (this.keyword) {
  181. this.associate.show = false
  182. this.$store.dispatch('resetSearchKeywords')
  183. if (this.searchType === 'product') {
  184. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  185. } else if (this.searchType === 'store') {
  186. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  187. }
  188. }
  189. },
  190. onAssociateClick (word) {
  191. this.click_flag = true
  192. this.keyword = word
  193. this.onSearch()
  194. },
  195. isCnStart () {
  196. if (this.keyword && this.keyword.length > 0) {
  197. return this.keyword.charCodeAt(0) > 255
  198. }
  199. }
  200. },
  201. created () {
  202. this.$store.dispatch('resetSearchKeywords')
  203. }
  204. }
  205. </script>
  206. <style lang="scss" scoped type="text/scss">
  207. @import '~assets/scss/variables';
  208. .form-control{
  209. border-radius: 0;
  210. }
  211. .search-box {
  212. width: 520px;
  213. height: 40px;
  214. position: relative;
  215. .search-input{
  216. width: 372px;
  217. float: left;
  218. }
  219. .search-input, .search-btn {
  220. height: 40px;
  221. border-width: 2px;
  222. }
  223. .select-type{
  224. width: 70px;
  225. float: left;
  226. border: #5078cb 2px solid;
  227. height: 40px;
  228. border-right: none;
  229. margin-right: -1px;
  230. }
  231. .search-btn {
  232. font-size: 16px;
  233. width: 79px;
  234. border-radius: 0;
  235. }
  236. .Isblue{
  237. vertical-align: middle;
  238. display: inline-block;
  239. text-align: center;
  240. width: 72px;
  241. border-left: 0;
  242. background: #d3e1fc;
  243. color: #5078cb;
  244. text-align: center;
  245. font-size: 14px;
  246. position: absolute;
  247. right: 0;
  248. top: 0;
  249. &:hover {
  250. background: #d2272d;
  251. color: #fff;
  252. transition: all .3s;
  253. }
  254. }
  255. .search-hot ul{
  256. line-height: 12px;
  257. }
  258. .search-hot ul li a{
  259. color: #838383;
  260. }
  261. .search-hot {
  262. margin-top:5px;
  263. .item {
  264. display: inline-block;
  265. max-width:22%;
  266. text-align: center;
  267. vertical-align: middle;
  268. font-size: $font-size-small;
  269. padding-right: $pad;
  270. a{
  271. display:block;
  272. overflow: hidden;
  273. text-overflow: ellipsis;
  274. white-space: nowrap;
  275. }
  276. &.item-first {
  277. width:12%;
  278. color: $red;
  279. }
  280. }
  281. }
  282. .association {
  283. position: absolute;
  284. left: 69px;
  285. top: 100%;
  286. right: 81px;
  287. background: $white;
  288. border: $border;
  289. border-top-width: 0;
  290. z-index: 21;
  291. .item {
  292. padding: 0 15px;
  293. line-height: 30px;
  294. cursor: pointer;
  295. &.active {
  296. background-color: $dark-bg;
  297. }
  298. &:hover {
  299. background-color: $grey-bg;
  300. }
  301. }
  302. .similar-title {
  303. padding: 0 15px;
  304. line-height: 30px;
  305. font-size: 16px;
  306. font-weight: bold;
  307. border-top: 1px solid #ccc;
  308. cursor: default;
  309. }
  310. }
  311. .association2 {
  312. left: 2px;
  313. right: 71px
  314. }
  315. }
  316. .search-box2 {
  317. width: 442px;
  318. }
  319. </style>