MainSearch.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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" :placeholder="searchType == 'product' ? '请输入您要查找的型号/品牌/类目' : '请输入您要查找的店铺'" @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. input {
  192. width: 4.88rem;
  193. height: .62rem;
  194. line-height: .62rem;
  195. font-size: .26rem;
  196. color: #999;
  197. padding-left: .92rem;
  198. border: .04rem solid #fff;
  199. background: #fff;
  200. outline: none;
  201. border-radius: 0;
  202. float: left;
  203. margin-top: .12rem;
  204. -webkit-appearance: none;
  205. border-top-left-radius: .14rem;
  206. border-bottom-left-radius: .14rem;
  207. }
  208. span {
  209. display: inline-block;
  210. width: 1.02rem;
  211. text-align: center;
  212. height: .62rem;
  213. line-height: .62rem;
  214. color: #366df3;
  215. font-size: .28rem;
  216. margin-left: .02rem;
  217. border-top-right-radius: .14rem;
  218. border-bottom-right-radius: .14rem;
  219. background: #fff;
  220. float: left;
  221. margin-top: .12rem;
  222. }
  223. a {
  224. font-size: .28rem;
  225. color: #fff;
  226. margin-left: .2rem;
  227. }
  228. .options {
  229. position: absolute;
  230. left: .3rem;
  231. font-size: .26rem;
  232. width: .9rem;
  233. padding-left: .15rem;
  234. background: url('/images/mobile/@2x/search/select-arrow.png') no-repeat;
  235. background-size: .14rem .12rem;
  236. background-position: .68rem .36rem;
  237. i {
  238. height: .46rem;
  239. width: .01rem;
  240. background: #eceef0;
  241. display: block;
  242. float: right;
  243. margin-top: .18rem;
  244. }
  245. ul {
  246. position: absolute;
  247. left: 0;
  248. li {
  249. width: 1rem;
  250. height: .7rem;
  251. border-radius: .02rem;
  252. background: rgba(0, 0, 0, .6);
  253. font-size: .26rem;
  254. color: rgba(255, 255, 255, .89);
  255. text-align: center;
  256. line-height: .7rem;
  257. }
  258. }
  259. }
  260. }
  261. .associate-list {
  262. background: #fff;
  263. li {
  264. height: 0.7rem;
  265. line-height: .9rem;
  266. margin: 0 .45rem;
  267. border-bottom: .04rem solid #f1f0f0;
  268. i {
  269. font-size: .36rem;
  270. margin-right: .24rem;
  271. color: #ddd;
  272. }
  273. span {
  274. color: #999;
  275. font-size: .28rem;
  276. line-height: .58rem;
  277. height: .58rem;
  278. display: inline-block;
  279. }
  280. &:active, &:hover {
  281. background: #eee;
  282. }
  283. &:last-child {
  284. text-align: center;
  285. font-size: .3rem;
  286. color: #3976f4;
  287. border-bottom: none;
  288. &:active, &:hover {
  289. background: #fff;
  290. }
  291. }
  292. }
  293. }
  294. .hot-history {
  295. .search-history {
  296. padding-left: .51rem;
  297. padding-top: .38rem;
  298. >p {
  299. font-size: .3rem;
  300. color: #333;
  301. i {
  302. font-size: .3rem;
  303. float: right;
  304. margin-right: 0.4rem;
  305. }
  306. }
  307. ul {
  308. text-align: left;
  309. margin-top: .26rem;
  310. li {
  311. display: inline-block;
  312. max-width: 2.83rem;
  313. overflow: hidden;
  314. text-overflow: ellipsis;
  315. white-space: nowrap;
  316. margin: 0 .1rem .1rem 0;
  317. background: #f2f6ff;
  318. height: .56rem;
  319. line-height: .56rem;
  320. padding: 0 .12rem;
  321. a {
  322. font-size: .3rem;
  323. color: #666;
  324. }
  325. }
  326. }
  327. }
  328. .search-hot {
  329. text-align: center;
  330. margin-top: .3rem;
  331. >img {
  332. width: 2.56rem;
  333. height: .67rem;
  334. }
  335. ul {
  336. text-align: left;
  337. padding-left: .51rem;
  338. margin-top: .31rem;
  339. li {
  340. display: inline-block;
  341. /*max-width: 2.83rem;*/
  342. overflow: hidden;
  343. text-overflow: ellipsis;
  344. white-space: nowrap;
  345. margin: 0 .1rem .1rem 0;
  346. background: #fef1eb;
  347. height: .56rem;
  348. line-height: .56rem;
  349. padding: 0 .12rem;
  350. a {
  351. font-size: .3rem;
  352. color: #666;
  353. }
  354. &:nth-child(1) {
  355. a {
  356. color: #fc5708;
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364. </style>