MainSearch.vue 11 KB

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