MainSearch.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <div class="main-search" @touchstart="cancelFocus" @click="setShowSearchType(false)" id="main-search">
  3. <div class="main-search-header">
  4. <div class="options" @click="setShowSearchType(!showSearchType, $event)">
  5. {{searchType == 'product' ? '产品' : '店铺'}}
  6. <i></i>
  7. <ul v-if="showSearchType">
  8. <li @click="setSearchType(searchType == 'product' ? 'store' : 'product', $event)">{{searchType == 'product' ? '店铺' : '产品'}}</li>
  9. </ul>
  10. </div>
  11. <input type="text" id="search-box" v-model="keyword" placeholder="请输入您要查找的型号或品牌" @keyup.13="onSearch()">
  12. <span @click="onSearch()">搜索</span>
  13. <a @click="cancelSearch">取消</a>
  14. </div>
  15. <ul class="associate-list" v-show="associate.show">
  16. <li @click="onAssociateClick(similar)" v-for="similar in similarKeywords.all">
  17. <i class="icon-sousuo iconfont"></i>
  18. <span>{{similar}}</span>
  19. </li>
  20. <li @click="onAssociateClick(keyword)">查找“{{keyword}}”</li>
  21. </ul>
  22. <div class="hot-history" v-show="!associate.show">
  23. <div class="search-history" v-if="searchHistory && searchHistory.length > 0">
  24. <p>历史搜索<i class="iconfont icon-lajitong" @click="deleteHistory"></i></p>
  25. <ul>
  26. <li v-for="item in searchHistory" @click="onSearch(item.keyword)">
  27. <a>{{item.keyword}}</a>
  28. </li>
  29. </ul>
  30. </div>
  31. <div class="search-hot">
  32. <img src="/images/mobile/@2x/home/hot-search.png" alt="">
  33. <ul>
  34. <li v-for="hotword in hotwords">
  35. <nuxt-link :to="hotword.url" v-text="hotword.name"></nuxt-link>
  36. </li>
  37. </ul>
  38. </div>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. export default {
  44. name: 'home',
  45. data () {
  46. return {
  47. keyword: '',
  48. associate: {
  49. show: false
  50. },
  51. searchType: 'product',
  52. showSearchType: false
  53. }
  54. },
  55. props: {
  56. hotwords: {
  57. type: Array,
  58. default () {
  59. return [
  60. {name: 'DSP1-DC5V-F', url: '/mobile/brand/componentDetail/0900300200000669'},
  61. {name: 'Vishay', url: '/mobile/brand/30327265e42a871be050007f01003d96'},
  62. {name: 'Panasonic', url: '/mobile/brand/30327265e47d871be050007f01003d96'},
  63. {name: 'Taiyo Yuden', url: '/mobile/brand/30327265e4be871be050007f01003d96'},
  64. {name: 'AE324FB5PN', url: '/mobile/brand/componentDetail/0900502200684613'}
  65. ]
  66. }
  67. }
  68. },
  69. // filters: {
  70. // similarFilter: function ([key, keyword]) {
  71. // console.log(keyword)
  72. // let index = key.indexOf(keyword)
  73. // if (index !== -1) {
  74. // key = key.substring(0, index) + '<strong>' + key.substr(index, keyword.length) + '</strong>' + key.substring(index + keyword.length, key.length)
  75. // }
  76. // return key
  77. // }
  78. // },
  79. methods: {
  80. onSearch (key) {
  81. if (key && key !== '') {
  82. this.keyword = key
  83. }
  84. if (this.keyword) {
  85. if (this.searchType === 'product') {
  86. this.$router.push('/mobile/search?w=' + encodeURIComponent(this.keyword))
  87. } else if (this.searchType === 'store') {
  88. this.$router.push('/mobile/shop?keyword=' + encodeURIComponent(this.keyword))
  89. }
  90. }
  91. },
  92. onChange () {
  93. if (!this.keyword) {
  94. this.associate.show = false
  95. this.$store.dispatch('resetSearchKeywords')
  96. } else {
  97. this.searchKeywords()
  98. }
  99. if (this.click_flag) {
  100. this.associate.show = false
  101. }
  102. },
  103. searchKeywords () {
  104. if (this.searchType === 'product') {
  105. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  106. this.associate.show = true
  107. }
  108. },
  109. onAssociateClick (word) {
  110. this.keyword = word
  111. this.onSearch()
  112. },
  113. cancelSearch: function () {
  114. this.$emit('cancelSearchAction')
  115. },
  116. cancelFocus: function () {
  117. document.getElementById('search-box').blur()
  118. },
  119. deleteHistory () {
  120. this.$http.delete('/search/searchHistory').then(response => {
  121. this.$store.dispatch('searchData/getSearchHistory')
  122. })
  123. },
  124. setSearchType (type, $event) {
  125. $event.stopPropagation()
  126. this.searchType = type
  127. this.setShowSearchType(false)
  128. this.associate.show = false
  129. },
  130. setShowSearchType (flag, e) {
  131. if (e) {
  132. e.stopPropagation()
  133. }
  134. this.showSearchType = flag
  135. }
  136. },
  137. created () {
  138. this.$store.dispatch('resetSearchKeywords')
  139. },
  140. mounted () {
  141. document.getElementById('search-box').focus()
  142. let height = window.innerHeight
  143. window.onresize = function () {
  144. if (window.innerHeight < height) {
  145. document.getElementById('main-search').style.bottom = (window.innerHeight - height) / (document.documentElement.clientWidth / 750) + 'rem'
  146. } else {
  147. document.getElementById('main-search').style.bottom = 0
  148. }
  149. }
  150. },
  151. watch: {
  152. 'keyword': function (val, oldVal) {
  153. let keywords = this.similarKeywords.data
  154. if (!keywords || !keywords.length) {
  155. this.onChange()
  156. }
  157. }
  158. },
  159. computed: {
  160. similarKeywords () {
  161. return this.$store.state.search.keywords.data
  162. },
  163. searchHistory () {
  164. return this.$store.state.searchData.searchHistory.searchHistory.data
  165. }
  166. }
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. .main-search {
  171. background: #fff;
  172. width: 100%;
  173. position: fixed;
  174. z-index: 1000;
  175. top: -2rem;
  176. bottom: 0;
  177. .main-search-header {
  178. position: relative;
  179. height: .88rem;
  180. background: #3e82f5;
  181. padding-left: .5rem;
  182. line-height: .88rem;
  183. margin-top: 2rem;
  184. input {
  185. width: 4.78rem;
  186. height: .62rem;
  187. line-height: .62rem;
  188. font-size: .28rem;
  189. color: #999;
  190. padding-left: .82rem;
  191. border: .04rem solid #fff;
  192. background: #fff;
  193. outline: none;
  194. border-radius: 0;
  195. float: left;
  196. margin-top: .12rem;
  197. -webkit-appearance: none;
  198. border-top-left-radius: .14rem;
  199. border-bottom-left-radius: .14rem;
  200. }
  201. span {
  202. display: inline-block;
  203. width: 1.02rem;
  204. text-align: center;
  205. height: .62rem;
  206. line-height: .62rem;
  207. color: #366df3;
  208. font-size: .28rem;
  209. margin-left: .02rem;
  210. border-top-right-radius: .05rem;
  211. border-bottom-right-radius: .05rem;
  212. background: #fff;
  213. float: left;
  214. margin-top: .12rem;
  215. }
  216. a {
  217. font-size: .28rem;
  218. color: #fff;
  219. margin-left: .2rem;
  220. }
  221. .options {
  222. position: absolute;
  223. left: .5rem;
  224. font-size: .28rem;
  225. width: .8rem;
  226. text-align: center;
  227. background: url('/images/mobile/@2x/search/select-arrow.png') no-repeat;
  228. background-size: .14rem .12rem;
  229. background-position: .63rem .36rem;
  230. i {
  231. height: .46rem;
  232. width: .01rem;
  233. background: #eceef0;
  234. display: block;
  235. float: right;
  236. margin-top: .18rem;
  237. }
  238. ul {
  239. position: absolute;
  240. li {
  241. width: .82rem;
  242. height: .59rem;
  243. border-radius: .02rem;
  244. background: rgba(0, 0, 0, .6);
  245. font-size: .23rem;
  246. color: rgba(255, 255, 255, .89);
  247. text-align: center;
  248. line-height: .59rem;
  249. }
  250. }
  251. }
  252. }
  253. .associate-list {
  254. background: #fff;
  255. li {
  256. height: 0.7rem;
  257. line-height: .9rem;
  258. margin: 0 .45rem;
  259. border-bottom: .04rem solid #f1f0f0;
  260. i {
  261. font-size: .36rem;
  262. margin-right: .24rem;
  263. color: #ddd;
  264. }
  265. span {
  266. color: #999;
  267. font-size: .28rem;
  268. line-height: .58rem;
  269. height: .58rem;
  270. display: inline-block;
  271. }
  272. &:active, &:hover {
  273. background: #eee;
  274. }
  275. &:last-child {
  276. text-align: center;
  277. font-size: .3rem;
  278. color: #3976f4;
  279. border-bottom: none;
  280. &:active, &:hover {
  281. background: #fff;
  282. }
  283. }
  284. }
  285. }
  286. .hot-history {
  287. .search-history {
  288. padding-left: .51rem;
  289. padding-top: .38rem;
  290. >p {
  291. font-size: .3rem;
  292. color: #333;
  293. i {
  294. font-size: .3rem;
  295. float: right;
  296. margin-right: 0.4rem;
  297. }
  298. }
  299. ul {
  300. text-align: left;
  301. margin-top: .26rem;
  302. li {
  303. display: inline-block;
  304. max-width: 2.83rem;
  305. overflow: hidden;
  306. text-overflow: ellipsis;
  307. white-space: nowrap;
  308. margin: 0 .1rem .1rem 0;
  309. background: #f2f6ff;
  310. height: .56rem;
  311. line-height: .56rem;
  312. padding: 0 .12rem;
  313. a {
  314. font-size: .3rem;
  315. color: #666;
  316. }
  317. }
  318. }
  319. }
  320. .search-hot {
  321. text-align: center;
  322. margin-top: .3rem;
  323. >img {
  324. width: 2.56rem;
  325. height: .67rem;
  326. }
  327. ul {
  328. text-align: left;
  329. padding-left: .51rem;
  330. margin-top: .31rem;
  331. li {
  332. display: inline-block;
  333. max-width: 2.83rem;
  334. overflow: hidden;
  335. text-overflow: ellipsis;
  336. white-space: nowrap;
  337. margin: 0 .1rem .1rem 0;
  338. background: #fef1eb;
  339. height: .56rem;
  340. line-height: .56rem;
  341. padding: 0 .12rem;
  342. a {
  343. font-size: .3rem;
  344. color: #666;
  345. }
  346. &:nth-child(1) {
  347. a {
  348. color: #fc5708;
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. </style>