MainSearch.vue 8.1 KB

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