MainSearch.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div class="main-search">
  3. <div class="main-search-header">
  4. <input type="text" v-model="keyword" placeholder="请输入您要查找的型号或品牌" @keyup.13="onSearch()" autofocus>
  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.code)" v-for="similar in similarKeywords.all">
  10. <i class="icon-sousuo iconfont"></i>
  11. <span>{{similar.code}}</span>
  12. </li>
  13. <li>查找“{{keyword}}”</li>
  14. </ul>
  15. <div class="hot-history" v-show="!associate.show">
  16. <div class="search-history">
  17. <p>历史搜索</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) {
  60. // let keyword = this.keyword
  61. // let index = key.indexOf(keyword)
  62. // if (index !== -1) {
  63. // key = key.substring(0, index) + '<strong>' + key.sub(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.associate.show = false
  75. this.$router.push({path: '/mobile/search?w=' + encodeURIComponent(this.keyword)})
  76. }
  77. },
  78. onChange () {
  79. if (!this.keyword) {
  80. this.associate.show = false
  81. this.$store.dispatch('resetSearchKeywords')
  82. } else {
  83. this.searchKeywords()
  84. }
  85. if (this.click_flag) {
  86. this.associate.show = false
  87. }
  88. },
  89. searchKeywords () {
  90. this.associate.show = true
  91. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  92. },
  93. onAssociateClick (word) {
  94. this.keyword = word
  95. this.onSearch()
  96. },
  97. cancelSearch: function () {
  98. this.$emit('cancelSearchAction')
  99. }
  100. },
  101. created () {
  102. this.$store.dispatch('resetSearchKeywords')
  103. },
  104. watch: {
  105. 'keyword': {
  106. handler (val, oldVal) {
  107. let keywords = this.similarKeywords.data
  108. if (!keywords || !keywords.length) {
  109. this.onChange()
  110. }
  111. }
  112. }
  113. },
  114. computed: {
  115. similarKeywords () {
  116. return this.$store.state.search.keywords.data
  117. },
  118. searchHistory () {
  119. return this.$store.state.searchData.searchHistory.searchHistory.data
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="scss" scoped>
  125. .main-search {
  126. background: #fff;
  127. height: -webkit-fill-available;
  128. width: -webkit-fill-available;
  129. position: fixed;
  130. z-index: 9;
  131. top: 0;
  132. .main-search-header {
  133. height: .71rem;
  134. background: #4391f7;
  135. padding-left: .84rem;
  136. display: flex;
  137. align-items: center;
  138. input {
  139. width: 4.48rem;
  140. height: .54rem;
  141. line-height: .54rem;
  142. font-size: .23rem;
  143. color: #999;
  144. padding-left: .2rem;
  145. border-bottom-left-radius: .05rem;
  146. border-top-left-radius: .05rem;
  147. border: .01rem solid #fff;
  148. background: #fff;
  149. }
  150. span {
  151. display: inline-block;
  152. width: 1.02rem;
  153. text-align: center;
  154. height: .54rem;
  155. line-height: .54rem;
  156. color: #366df3;
  157. font-size: .23rem;
  158. margin-left: .02rem;
  159. border-top-right-radius: .05rem;
  160. border-bottom-right-radius: .05rem;
  161. background: #fff;
  162. }
  163. a {
  164. font-size: .27rem;
  165. color: #fff;
  166. margin-left: .2rem;
  167. }
  168. }
  169. .associate-list {
  170. padding-top: .25rem;
  171. background: #fff;
  172. li {
  173. height: 58px;
  174. line-height: 58px;
  175. padding: 0 45px;
  176. border-bottom: .01rem solid #f1f0f0;
  177. i {
  178. font-size: .4rem;
  179. margin-right: .24rem;
  180. color: #ddd;
  181. }
  182. span {
  183. color: #999;
  184. font-size: .28rem;
  185. line-height: 58px;
  186. height: .58rem;
  187. display: inline-block;
  188. }
  189. &:active, &:hover {
  190. background: #eee;
  191. }
  192. &:last-child {
  193. text-align: center;
  194. font-size: .3rem;
  195. color: #3976f4;
  196. border-bottom: none;
  197. &:active, &:hover {
  198. background: #fff;
  199. }
  200. }
  201. }
  202. }
  203. .hot-history {
  204. .search-history {
  205. padding-left: .51rem;
  206. padding-top: .38rem;
  207. >p {
  208. font-size: .3rem;
  209. color: #333;
  210. }
  211. ul {
  212. text-align: left;
  213. margin-top: .26rem;
  214. li {
  215. display: inline-block;
  216. max-width: 2.83rem;
  217. overflow: hidden;
  218. text-overflow: ellipsis;
  219. white-space: nowrap;
  220. margin-right: 10px;
  221. background: #f2f6ff;
  222. height: .56rem;
  223. line-height: .56rem;
  224. padding: 0 .12rem;
  225. a {
  226. font-size: .3rem;
  227. color: #666;
  228. }
  229. }
  230. }
  231. }
  232. .search-hot {
  233. text-align: center;
  234. margin-top: .3rem;
  235. >img {
  236. width: 2.56rem;
  237. height: .67rem;
  238. }
  239. ul {
  240. text-align: left;
  241. padding-left: .51rem;
  242. margin-top: .31rem;
  243. li {
  244. display: inline-block;
  245. max-width: 2.83rem;
  246. overflow: hidden;
  247. text-overflow: ellipsis;
  248. white-space: nowrap;
  249. margin-right: 10px;
  250. background: #fef1eb;
  251. height: .56rem;
  252. line-height: .56rem;
  253. padding: 0 .12rem;
  254. a {
  255. font-size: .3rem;
  256. color: #666;
  257. }
  258. &:nth-child(1) {
  259. a {
  260. color: #fc5708;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. </style>