MainSearch.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <div class="main-search" @touchstart="cancelFocus">
  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. ]
  55. }
  56. }
  57. },
  58. // filters: {
  59. // similarFilter: function ([key, keyword]) {
  60. // console.log(keyword)
  61. // let index = key.indexOf(keyword)
  62. // if (index !== -1) {
  63. // key = key.substring(0, index) + '<strong>' + key.substr(index, keyword.length) + '</strong>' + key.substring(index + keyword.length, key.length)
  64. // }
  65. // return key
  66. // }
  67. // },
  68. methods: {
  69. onSearch (key) {
  70. if (key && key !== '') {
  71. this.keyword = key
  72. }
  73. if (this.keyword) {
  74. this.$router.push({path: '/mobile/search?w=' + encodeURIComponent(this.keyword)})
  75. }
  76. },
  77. onChange () {
  78. if (!this.keyword) {
  79. this.associate.show = false
  80. this.$store.dispatch('resetSearchKeywords')
  81. } else {
  82. this.searchKeywords()
  83. }
  84. if (this.click_flag) {
  85. this.associate.show = false
  86. }
  87. },
  88. searchKeywords () {
  89. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  90. this.associate.show = true
  91. },
  92. onAssociateClick (word) {
  93. this.keyword = word
  94. this.onSearch()
  95. },
  96. cancelSearch: function () {
  97. this.$emit('cancelSearchAction')
  98. },
  99. cancelFocus: function () {
  100. document.getElementById('search-box').blur()
  101. },
  102. deleteHistory () {
  103. this.$http.delete('/search/searchHistory').then(response => {
  104. this.$store.dispatch('searchData/getSearchHistory')
  105. })
  106. }
  107. },
  108. created () {
  109. this.$store.dispatch('resetSearchKeywords')
  110. },
  111. mounted () {
  112. document.getElementById('search-box').focus()
  113. },
  114. watch: {
  115. 'keyword': {
  116. handler (val, oldVal) {
  117. let keywords = this.similarKeywords.data
  118. if (!keywords || !keywords.length) {
  119. this.onChange()
  120. }
  121. }
  122. }
  123. },
  124. computed: {
  125. similarKeywords () {
  126. return this.$store.state.search.keywords.data
  127. },
  128. searchHistory () {
  129. return this.$store.state.searchData.searchHistory.searchHistory.data
  130. }
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. .main-search {
  136. background: #fff;
  137. height: -webkit-fill-available;
  138. width: -webkit-fill-available;
  139. position: fixed;
  140. z-index: 9;
  141. top: 0;
  142. .main-search-header {
  143. height: .71rem;
  144. background: #4391f7;
  145. padding-left: .84rem;
  146. display: flex;
  147. align-items: center;
  148. input {
  149. width: 4.48rem;
  150. height: .54rem;
  151. line-height: .54rem;
  152. font-size: .23rem;
  153. color: #999;
  154. padding-left: .2rem;
  155. border-bottom-left-radius: .05rem;
  156. border-top-left-radius: .05rem;
  157. border: .01rem solid #fff;
  158. background: #fff;
  159. outline: none;
  160. }
  161. span {
  162. display: inline-block;
  163. width: 1.02rem;
  164. text-align: center;
  165. height: .54rem;
  166. line-height: .54rem;
  167. color: #366df3;
  168. font-size: .23rem;
  169. margin-left: .02rem;
  170. border-top-right-radius: .05rem;
  171. border-bottom-right-radius: .05rem;
  172. background: #fff;
  173. }
  174. a {
  175. font-size: .27rem;
  176. color: #fff;
  177. margin-left: .2rem;
  178. }
  179. }
  180. .associate-list {
  181. padding-top: .25rem;
  182. background: #fff;
  183. li {
  184. height: .58rem;
  185. line-height: .58rem;
  186. padding: 0 .45rem;
  187. border-bottom: .01rem solid #f1f0f0;
  188. i {
  189. font-size: .4rem;
  190. margin-right: .24rem;
  191. color: #ddd;
  192. }
  193. span {
  194. color: #999;
  195. font-size: .28rem;
  196. line-height: .58rem;
  197. height: .58rem;
  198. display: inline-block;
  199. }
  200. &:active, &:hover {
  201. background: #eee;
  202. }
  203. &:last-child {
  204. text-align: center;
  205. font-size: .3rem;
  206. color: #3976f4;
  207. border-bottom: none;
  208. &:active, &:hover {
  209. background: #fff;
  210. }
  211. }
  212. }
  213. }
  214. .hot-history {
  215. .search-history {
  216. padding-left: .51rem;
  217. padding-top: .38rem;
  218. >p {
  219. font-size: .3rem;
  220. color: #333;
  221. i {
  222. font-size: .3rem;
  223. float: right;
  224. margin-right: 0.4rem;
  225. }
  226. }
  227. ul {
  228. text-align: left;
  229. margin-top: .26rem;
  230. li {
  231. display: inline-block;
  232. max-width: 2.83rem;
  233. overflow: hidden;
  234. text-overflow: ellipsis;
  235. white-space: nowrap;
  236. margin-right: .1rem;
  237. background: #f2f6ff;
  238. height: .56rem;
  239. line-height: .56rem;
  240. padding: 0 .12rem;
  241. a {
  242. font-size: .3rem;
  243. color: #666;
  244. }
  245. }
  246. }
  247. }
  248. .search-hot {
  249. text-align: center;
  250. margin-top: .3rem;
  251. >img {
  252. width: 2.56rem;
  253. height: .67rem;
  254. }
  255. ul {
  256. text-align: left;
  257. padding-left: .51rem;
  258. margin-top: .31rem;
  259. li {
  260. display: inline-block;
  261. max-width: 2.83rem;
  262. overflow: hidden;
  263. text-overflow: ellipsis;
  264. white-space: nowrap;
  265. margin-right: .1rem;
  266. background: #fef1eb;
  267. height: .56rem;
  268. line-height: .56rem;
  269. padding: 0 .12rem;
  270. a {
  271. font-size: .3rem;
  272. color: #666;
  273. }
  274. &:nth-child(1) {
  275. a {
  276. color: #fc5708;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. </style>