MainSearch.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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="searchType == 'product' ? '请输入您要查找的型号/品牌/类目' : '请输入您要查找的店铺'" @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)">
  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 (item) {
  81. if (item) {
  82. this.keyword = item.keyword
  83. if (item.type === 'SEARCH_STORE') {
  84. this.$router.push('/mobile/shop?keyword=' + encodeURIComponent(this.keyword))
  85. } else if (item.type === 'SEARCH_PRODUCT') {
  86. this.$router.push('/mobile/search?w=' + encodeURIComponent(this.keyword))
  87. }
  88. } else {
  89. if (this.keyword) {
  90. if (this.searchType === 'product') {
  91. this.$router.push('/mobile/search?w=' + encodeURIComponent(this.keyword))
  92. } else if (this.searchType === 'store') {
  93. this.$router.push('/mobile/shop?keyword=' + encodeURIComponent(this.keyword))
  94. }
  95. }
  96. }
  97. },
  98. onChange () {
  99. if (!this.keyword) {
  100. this.associate.show = false
  101. this.$store.dispatch('resetSearchKeywords')
  102. } else {
  103. this.searchKeywords()
  104. }
  105. if (this.click_flag) {
  106. this.associate.show = false
  107. }
  108. },
  109. searchKeywords () {
  110. if (this.searchType === 'product') {
  111. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  112. this.associate.show = true
  113. }
  114. },
  115. onAssociateClick (word) {
  116. this.keyword = word
  117. this.onSearch()
  118. },
  119. cancelSearch: function () {
  120. this.$emit('cancelSearchAction')
  121. },
  122. cancelFocus: function () {
  123. document.getElementById('search-box').blur()
  124. },
  125. deleteHistory () {
  126. this.$http.delete('/search/searchHistory').then(response => {
  127. this.$store.dispatch('searchData/getSearchHistory')
  128. })
  129. },
  130. setSearchType (type, $event) {
  131. $event.stopPropagation()
  132. this.searchType = type
  133. this.setShowSearchType(false)
  134. this.associate.show = false
  135. },
  136. setShowSearchType (flag, e) {
  137. if (e) {
  138. e.stopPropagation()
  139. }
  140. this.showSearchType = flag
  141. }
  142. },
  143. created () {
  144. this.$store.dispatch('resetSearchKeywords')
  145. },
  146. mounted () {
  147. document.getElementById('search-box').focus()
  148. let height = window.innerHeight
  149. window.onresize = function () {
  150. if (window.innerHeight < height) {
  151. document.getElementById('main-search').style.bottom = (window.innerHeight - height) / (document.documentElement.clientWidth / 750) + 'rem'
  152. } else {
  153. document.getElementById('main-search').style.bottom = 0
  154. }
  155. }
  156. },
  157. watch: {
  158. 'keyword': function (val, oldVal) {
  159. let keywords = this.similarKeywords.data
  160. if (!keywords || !keywords.length) {
  161. this.onChange()
  162. }
  163. }
  164. },
  165. computed: {
  166. similarKeywords () {
  167. return this.$store.state.search.keywords.data
  168. },
  169. searchHistory () {
  170. return this.$store.state.searchData.searchHistory.searchHistory.data
  171. }
  172. }
  173. }
  174. </script>
  175. <style lang="scss" scoped>
  176. .main-search {
  177. background: #fff;
  178. width: 100%;
  179. position: fixed;
  180. z-index: 1000;
  181. top: -2rem;
  182. bottom: 0;
  183. .main-search-header {
  184. position: relative;
  185. height: .88rem;
  186. background: #3e82f5;
  187. padding-left: .5rem;
  188. line-height: .88rem;
  189. margin-top: 2rem;
  190. input {
  191. width: 4.78rem;
  192. height: .62rem;
  193. line-height: .62rem;
  194. font-size: .26rem;
  195. color: #999;
  196. padding-left: .82rem;
  197. border: .04rem solid #fff;
  198. background: #fff;
  199. outline: none;
  200. border-radius: 0;
  201. float: left;
  202. margin-top: .12rem;
  203. -webkit-appearance: none;
  204. border-top-left-radius: .14rem;
  205. border-bottom-left-radius: .14rem;
  206. }
  207. span {
  208. display: inline-block;
  209. width: 1.02rem;
  210. text-align: center;
  211. height: .62rem;
  212. line-height: .62rem;
  213. color: #366df3;
  214. font-size: .28rem;
  215. margin-left: .02rem;
  216. border-top-right-radius: .14rem;
  217. border-bottom-right-radius: .14rem;
  218. background: #fff;
  219. float: left;
  220. margin-top: .12rem;
  221. }
  222. a {
  223. font-size: .28rem;
  224. color: #fff;
  225. margin-left: .2rem;
  226. }
  227. .options {
  228. position: absolute;
  229. left: .5rem;
  230. font-size: .26rem;
  231. width: .8rem;
  232. padding-left: .05rem;
  233. background: url('/images/mobile/@2x/search/select-arrow.png') no-repeat;
  234. background-size: .14rem .12rem;
  235. background-position: .63rem .36rem;
  236. i {
  237. height: .46rem;
  238. width: .01rem;
  239. background: #eceef0;
  240. display: block;
  241. float: right;
  242. margin-top: .18rem;
  243. }
  244. ul {
  245. position: absolute;
  246. li {
  247. width: .82rem;
  248. height: .59rem;
  249. border-radius: .02rem;
  250. background: rgba(0, 0, 0, .6);
  251. font-size: .26rem;
  252. color: rgba(255, 255, 255, .89);
  253. text-align: center;
  254. line-height: .59rem;
  255. }
  256. }
  257. }
  258. }
  259. .associate-list {
  260. background: #fff;
  261. li {
  262. height: 0.7rem;
  263. line-height: .9rem;
  264. margin: 0 .45rem;
  265. border-bottom: .04rem solid #f1f0f0;
  266. i {
  267. font-size: .36rem;
  268. margin-right: .24rem;
  269. color: #ddd;
  270. }
  271. span {
  272. color: #999;
  273. font-size: .28rem;
  274. line-height: .58rem;
  275. height: .58rem;
  276. display: inline-block;
  277. }
  278. &:active, &:hover {
  279. background: #eee;
  280. }
  281. &:last-child {
  282. text-align: center;
  283. font-size: .3rem;
  284. color: #3976f4;
  285. border-bottom: none;
  286. &:active, &:hover {
  287. background: #fff;
  288. }
  289. }
  290. }
  291. }
  292. .hot-history {
  293. .search-history {
  294. padding-left: .51rem;
  295. padding-top: .38rem;
  296. >p {
  297. font-size: .3rem;
  298. color: #333;
  299. i {
  300. font-size: .3rem;
  301. float: right;
  302. margin-right: 0.4rem;
  303. }
  304. }
  305. ul {
  306. text-align: left;
  307. margin-top: .26rem;
  308. li {
  309. display: inline-block;
  310. max-width: 2.83rem;
  311. overflow: hidden;
  312. text-overflow: ellipsis;
  313. white-space: nowrap;
  314. margin: 0 .1rem .1rem 0;
  315. background: #f2f6ff;
  316. height: .56rem;
  317. line-height: .56rem;
  318. padding: 0 .12rem;
  319. a {
  320. font-size: .3rem;
  321. color: #666;
  322. }
  323. }
  324. }
  325. }
  326. .search-hot {
  327. text-align: center;
  328. margin-top: .3rem;
  329. >img {
  330. width: 2.56rem;
  331. height: .67rem;
  332. }
  333. ul {
  334. text-align: left;
  335. padding-left: .51rem;
  336. margin-top: .31rem;
  337. li {
  338. display: inline-block;
  339. max-width: 2.83rem;
  340. overflow: hidden;
  341. text-overflow: ellipsis;
  342. white-space: nowrap;
  343. margin: 0 .1rem .1rem 0;
  344. background: #fef1eb;
  345. height: .56rem;
  346. line-height: .56rem;
  347. padding: 0 .12rem;
  348. a {
  349. font-size: .3rem;
  350. color: #666;
  351. }
  352. &:nth-child(1) {
  353. a {
  354. color: #fc5708;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. </style>