MainSearch.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. {name: 'Taiyo Yuden', url: '/mobile/brand/30327265e4be871be050007f01003d96'},
  55. {name: 'AE3ZZP332', url: '/mobile/brand/componentDetail/0100100100000003'}
  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. },
  116. watch: {
  117. 'keyword': function (val, oldVal) {
  118. let keywords = this.similarKeywords.data
  119. if (!keywords || !keywords.length) {
  120. this.onChange()
  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: 100;
  141. top: 0;
  142. .main-search-header {
  143. height: .71rem;
  144. background: #4391f7;
  145. padding-left: .5rem;
  146. line-height: .71rem;
  147. input {
  148. width: 4.78rem;
  149. height: .54rem;
  150. line-height: .54rem;
  151. font-size: .28rem;
  152. color: #999;
  153. padding-left: .2rem;
  154. border: .04rem solid #fff;
  155. background: #fff;
  156. outline: none;
  157. border-radius: 0;
  158. float: left;
  159. margin-top: .08rem;
  160. -webkit-appearance: none;
  161. }
  162. span {
  163. display: inline-block;
  164. width: 1.02rem;
  165. text-align: center;
  166. height: .54rem;
  167. line-height: .54rem;
  168. color: #366df3;
  169. font-size: .28rem;
  170. margin-left: .02rem;
  171. border-top-right-radius: .05rem;
  172. border-bottom-right-radius: .05rem;
  173. background: #fff;
  174. float: left;
  175. margin-top: .08rem;
  176. }
  177. a {
  178. font-size: .28rem;
  179. color: #fff;
  180. margin-left: .2rem;
  181. }
  182. }
  183. .associate-list {
  184. background: #fff;
  185. li {
  186. height: 0.7rem;
  187. line-height: .9rem;
  188. margin: 0 .45rem;
  189. border-bottom: .04rem solid #f1f0f0;
  190. i {
  191. font-size: .36rem;
  192. margin-right: .24rem;
  193. color: #ddd;
  194. }
  195. span {
  196. color: #999;
  197. font-size: .28rem;
  198. line-height: .58rem;
  199. height: .58rem;
  200. display: inline-block;
  201. }
  202. &:active, &:hover {
  203. background: #eee;
  204. }
  205. &:last-child {
  206. text-align: center;
  207. font-size: .3rem;
  208. color: #3976f4;
  209. border-bottom: none;
  210. &:active, &:hover {
  211. background: #fff;
  212. }
  213. }
  214. }
  215. }
  216. .hot-history {
  217. .search-history {
  218. padding-left: .51rem;
  219. padding-top: .38rem;
  220. >p {
  221. font-size: .3rem;
  222. color: #333;
  223. i {
  224. font-size: .3rem;
  225. float: right;
  226. margin-right: 0.4rem;
  227. }
  228. }
  229. ul {
  230. text-align: left;
  231. margin-top: .26rem;
  232. li {
  233. display: inline-block;
  234. max-width: 2.83rem;
  235. overflow: hidden;
  236. text-overflow: ellipsis;
  237. white-space: nowrap;
  238. margin: 0 .1rem .1rem 0;
  239. background: #f2f6ff;
  240. height: .56rem;
  241. line-height: .56rem;
  242. padding: 0 .12rem;
  243. a {
  244. font-size: .3rem;
  245. color: #666;
  246. }
  247. }
  248. }
  249. }
  250. .search-hot {
  251. text-align: center;
  252. margin-top: .3rem;
  253. >img {
  254. width: 2.56rem;
  255. height: .67rem;
  256. }
  257. ul {
  258. text-align: left;
  259. padding-left: .51rem;
  260. margin-top: .31rem;
  261. li {
  262. display: inline-block;
  263. max-width: 2.83rem;
  264. overflow: hidden;
  265. text-overflow: ellipsis;
  266. white-space: nowrap;
  267. margin: 0 .1rem .1rem 0;
  268. background: #fef1eb;
  269. height: .56rem;
  270. line-height: .56rem;
  271. padding: 0 .12rem;
  272. a {
  273. font-size: .3rem;
  274. color: #666;
  275. }
  276. &:nth-child(1) {
  277. a {
  278. color: #fc5708;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. </style>