MainSearch.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. <div class="main-search-header">
  16. <input type="text" v-model="keyword" id="search-box" @keyup.13="onSearch()" :placeholder="placeholder">
  17. <span @click="onSearch()">搜索</span>
  18. <a @click="cancelSearch()">取消</a>
  19. <div class="main-search-header-controll clearfix">
  20. <div style="display: flex;align-items: center;justify-content: space-between;width:100%;">
  21. <div :class="ChooseTop === 'component' ? 'active' : ''" @click="setChangelistHander('component')"><a>型号</a></div>
  22. <div :class="ChooseTop === 'kind' ? 'active' : ''" @click="setChangelistHander('kind')"><a>物料名称</a></div>
  23. <div :class="ChooseTop === 'store' ? 'active' : ''" @click="setChangelistHander('store')"><a>卖家</a></div>
  24. <div :class="ChooseTop === 'brand' ? 'active' : ''" @click="setChangelistHander('brand')"><a>品牌</a></div>
  25. </div>
  26. </div>
  27. </div>
  28. <ul class="associate-list" v-show="associate.show">
  29. <li @click="onAssociateClick(similar)" v-for="similar in similarKeywords.result">
  30. <i class="icon-sousuo iconfont"></i>
  31. <span>{{Getsimilar(similar)}}</span>
  32. <!--<span v-if="ChooseTop === 'component'">{{similar.code}}</span>-->
  33. <!--<span v-else-if="ChooseTop === 'product'">{{similar}}</span>-->
  34. <!--<span v-else-if="ChooseTop === 'store'">{{similar}}</span>-->
  35. <!--<span v-else-if="ChooseTop === 'brand'">{{similar.nameEn}}</span>-->
  36. </li>
  37. <li @click="onAssociateClick(keyword)">查找“{{baseUtils.filterStringEllipsis(keyword, 30)}}”</li>
  38. </ul>
  39. <div class="hot-history" v-show="!associate.show" style="margin-top: 2.08rem">
  40. <div class="search-history" v-if="searchHistory && searchHistory.length > 0">
  41. <p>历史搜索<i class="iconfont icon-lajitong" @click="deleteHistory"></i></p>
  42. <ul>
  43. <li v-for="item in searchHistory" @click="onSearch(item.keyword)" v-if="item.keyword && item.keyword.trim().length">
  44. <a>{{item.keyword}}</a>
  45. </li>
  46. </ul>
  47. </div>
  48. <div class="search-hot">
  49. <img src="/images/mobile/@2x/home/hot-search.png" alt="">
  50. <ul>
  51. <li v-for="hotword in hotwords">
  52. <a :href="hotword.url" v-text="hotword.name"></a>
  53. </li>
  54. </ul>
  55. </div>
  56. </div>
  57. </div>
  58. </template>
  59. <script>
  60. export default {
  61. name: 'home',
  62. data () {
  63. return {
  64. keyword: '',
  65. associate: {
  66. show: false
  67. },
  68. searchType: 'product',
  69. showSearchType: false,
  70. ChooseTop: 'component'
  71. }
  72. },
  73. props: {
  74. hotwords: {
  75. type: Array,
  76. default () {
  77. return [
  78. {name: '深圳华商龙', url: 'https://www.usoftmall.com/mobile/shop/worldshine'},
  79. {name: 'DSP1-DC5V-F', url: 'https://www.usoftmall.com/mobile/brand/componentDetail/0900300200000669'},
  80. {name: 'Vishay', url: 'https://www.usoftmall.com/mobile/brand/30327265e42a871be050007f01003d96'},
  81. {name: 'Panasonic', url: 'https://www.usoftmall.com/mobile/brand/30327265e47d871be050007f01003d96'},
  82. {name: 'Taiyo Yuden', url: 'https://www.usoftmall.com/mobile/brand/30327265e4be871be050007f01003d96'},
  83. {name: 'AE324FB5PN', url: 'https://www.usoftmall.com/mobile/brand/componentDetail/0900502200684613'}
  84. ]
  85. }
  86. }
  87. },
  88. // filters: {
  89. // similarFilter: function ([key, keyword]) {
  90. // console.log(keyword)
  91. // let index = key.indexOf(keyword)
  92. // if (index !== -1) {
  93. // key = key.substring(0, index) + '<strong>' + key.substr(index, keyword.length) + '</strong>' + key.substring(index + keyword.length, key.length)
  94. // }
  95. // return key
  96. // }
  97. // },
  98. methods: {
  99. setChangelistHander(str) {
  100. this.ChooseTop = str
  101. if (!this.keyword) return
  102. this.$store.dispatch('searchKeywords', {keyword: this.keyword, type: this.ChooseTop})
  103. this.associate.show = true
  104. },
  105. onSearch (item) {
  106. if (this.keyword) {
  107. this.$router.push(`/mobile/search/newkeycode?choosetype=${this.ChooseTop}&keyword=${encodeURIComponent(this.keyword)}`)
  108. }
  109. // if (item) {
  110. // this.keyword = item.keyword
  111. // if (item.type === 'SEARCH_STORE') {
  112. // this.$router.push('/mobile/shop?keyword=' + encodeURIComponent(this.keyword))
  113. // } else if (item.type === 'SEARCH_PRODUCT') {
  114. // this.$router.push('/mobile/search?w=' + encodeURIComponent(this.keyword))
  115. // }
  116. // } else {
  117. // if (this.keyword) {
  118. // if (this.searchType === 'product') {
  119. // this.$router.push('/mobile/search?w=' + encodeURIComponent(this.keyword))
  120. // } else if (this.searchType === 'store') {
  121. // this.$router.push('/mobile/shop?keyword=' + encodeURIComponent(this.keyword))
  122. // }
  123. // }
  124. // }
  125. },
  126. onChange () {
  127. if (!this.keyword) {
  128. this.associate.show = false
  129. this.$store.dispatch('resetSearchKeywords')
  130. } else {
  131. this.searchKeywords()
  132. }
  133. if (this.click_flag) {
  134. this.associate.show = false
  135. }
  136. },
  137. searchKeywords () {
  138. this.$store.dispatch('searchKeywords', {keyword: this.keyword, type: this.ChooseTop})
  139. this.associate.show = true
  140. },
  141. onAssociateClick (word) {
  142. if (this.ChooseTop === 'component') {
  143. this.keyword = word.code
  144. } else if (this.ChooseTop === 'kind') {
  145. this.keyword = word.nameCn
  146. } else if (this.ChooseTop === 'store') {
  147. this.keyword = word.name
  148. } else if (this.ChooseTop === 'brand') {
  149. this.keyword = word.nameEn
  150. }
  151. this.onSearch()
  152. },
  153. Getsimilar(word) {
  154. if (this.ChooseTop === 'component') {
  155. return word.code
  156. } else if (this.ChooseTop === 'kind') {
  157. return word.nameCn
  158. } else if (this.ChooseTop === 'store') {
  159. return word.name
  160. } else if (this.ChooseTop === 'brand') {
  161. return word.nameEn
  162. }
  163. },
  164. cancelSearch: function () {
  165. this.$emit('cancelSearchAction')
  166. },
  167. cancelFocus: function () {
  168. // document.getElementById('search-box').blur()
  169. },
  170. deleteHistory () {
  171. this.$http.delete('/search/searchHistory').then(response => {
  172. this.$store.dispatch('searchData/getSearchHistory')
  173. })
  174. },
  175. setSearchType (type, $event) {
  176. $event.stopPropagation()
  177. this.searchType = type
  178. this.setShowSearchType(false)
  179. this.associate.show = false
  180. },
  181. setShowSearchType (flag, e) {
  182. if (e) {
  183. e.stopPropagation()
  184. }
  185. this.showSearchType = flag
  186. }
  187. },
  188. created () {
  189. this.$store.dispatch('resetSearchKeywords')
  190. },
  191. mounted () {
  192. document.getElementById('search-box').focus()
  193. let height = window.innerHeight
  194. window.onresize = function () {
  195. if (window.innerHeight < height) {
  196. document.getElementById('main-search').style.bottom = (window.innerHeight - height) / (document.documentElement.clientWidth / 750) + 'rem'
  197. } else {
  198. document.getElementById('main-search').style.bottom = 0
  199. }
  200. }
  201. },
  202. watch: {
  203. 'keyword': function (val, oldVal) {
  204. let keywords = this.similarKeywords.data
  205. if (!keywords || !keywords.length) {
  206. this.onChange()
  207. }
  208. }
  209. },
  210. computed: {
  211. similarKeywords () {
  212. return this.$store.state.search.keywords.data
  213. },
  214. searchHistory () {
  215. return this.$store.state.searchData.searchHistory.searchHistory.data
  216. },
  217. placeholder() {
  218. if (this.ChooseTop === 'cmpcode') {
  219. return '请输入您要搜索的型号'
  220. } else if (this.ChooseTop === 'materiel') {
  221. return '请输入您要搜索的物料名称'
  222. } else if (this.ChooseTop === 'seller') {
  223. return '请输入您要搜索的卖家名称'
  224. } else if (this.ChooseTop === 'cmpbrand') {
  225. return '请输入您要搜索的品牌'
  226. }
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss" scoped>
  232. @mixin overFlowHidden {
  233. overflow: hidden;
  234. text-overflow: ellipsis;
  235. white-space: nowrap;
  236. }
  237. @mixin lineHeight($value) {
  238. height: $value;
  239. line-height: $value;
  240. }
  241. .main-search {
  242. background: #fff;
  243. width: 100%;
  244. position: fixed;
  245. z-index: 1000;
  246. top: 0;
  247. bottom: 0;
  248. .main-search-header {
  249. position: absolute;
  250. background: #3e82f5;
  251. line-height: .88rem;
  252. margin-top: 0;
  253. z-index: 1;
  254. width: 100%;
  255. input {
  256. width: 4.88rem;
  257. height: .62rem;
  258. line-height: .62rem;
  259. font-size: .26rem;
  260. color: #333;
  261. margin-left: 0.3rem;
  262. /*padding-left: 1.26rem;*/
  263. border: .04rem solid #fff;
  264. background: #fff;
  265. outline: none;
  266. border-radius: 0;
  267. float: left;
  268. margin-top: .12rem;
  269. -webkit-appearance: none;
  270. border-top-left-radius: .14rem;
  271. border-bottom-left-radius: .14rem;
  272. }
  273. span {
  274. display: inline-block;
  275. width: 1.02rem;
  276. text-align: center;
  277. height: .62rem;
  278. line-height: .62rem;
  279. color: #366df3;
  280. font-size: .28rem;
  281. margin-left: .02rem;
  282. border-top-right-radius: .14rem;
  283. border-bottom-right-radius: .14rem;
  284. background: #fff;
  285. float: left;
  286. margin-top: .12rem;
  287. }
  288. a {
  289. font-size: .28rem;
  290. color: #fff;
  291. margin-left: .2rem;
  292. }
  293. .main-search-header-controll {
  294. background: #3e82f5;
  295. width: 5.9rem;
  296. margin-left: 0.3rem;
  297. div {
  298. @include lineHeight(0.66rem);
  299. font-size: 0.28rem;
  300. color: #fff;
  301. margin: 0;
  302. background: rgba(0,0,0,0);
  303. border-radius: 0;
  304. &.active a{
  305. color: #fff;
  306. border-bottom: 0.02rem solid #fff;
  307. }
  308. }
  309. }
  310. }
  311. .associate-list {
  312. position: absolute;
  313. width: 100%;
  314. background: #fff;
  315. top: 0.8rem;
  316. border: 1px solid #dcdcdc;
  317. z-index: 100;
  318. height: 100%;
  319. left: 0;
  320. bottom: 0;
  321. right: 0;
  322. li {
  323. height: 0.7rem;
  324. line-height: .9rem;
  325. margin: 0 .45rem;
  326. border-bottom: .04rem solid #f1f0f0;
  327. @include overFlowHidden();
  328. i {
  329. font-size: .36rem;
  330. margin-right: .24rem;
  331. color: #ddd;
  332. }
  333. span {
  334. color: #999;
  335. font-size: .28rem;
  336. line-height: .58rem;
  337. height: .58rem;
  338. display: inline-block;
  339. }
  340. &:active, &:hover {
  341. background: #eee;
  342. }
  343. &:last-child {
  344. text-align: center;
  345. font-size: .3rem;
  346. color: #3976f4;
  347. border-bottom: none;
  348. &:active, &:hover {
  349. background: #fff;
  350. }
  351. }
  352. }
  353. }
  354. .hot-history {
  355. .search-history {
  356. padding-left: .51rem;
  357. >p {
  358. font-size: .3rem;
  359. color: #333;
  360. i {
  361. font-size: .3rem;
  362. float: right;
  363. margin-right: 0.4rem;
  364. }
  365. }
  366. ul {
  367. text-align: left;
  368. margin-top: .26rem;
  369. li {
  370. display: inline-block;
  371. max-width: 2.83rem;
  372. overflow: hidden;
  373. text-overflow: ellipsis;
  374. white-space: nowrap;
  375. margin: 0 .1rem .1rem 0;
  376. background: #f2f6ff;
  377. height: .56rem;
  378. line-height: .56rem;
  379. padding: 0 .12rem;
  380. a {
  381. font-size: .3rem;
  382. color: #666;
  383. }
  384. }
  385. }
  386. }
  387. .search-hot {
  388. text-align: center;
  389. margin-top: .3rem;
  390. >img {
  391. width: 2.56rem;
  392. height: .67rem;
  393. }
  394. ul {
  395. text-align: left;
  396. padding-left: .51rem;
  397. margin-top: .31rem;
  398. li {
  399. display: inline-block;
  400. /*max-width: 2.83rem;*/
  401. overflow: hidden;
  402. text-overflow: ellipsis;
  403. white-space: nowrap;
  404. margin: 0 .1rem .1rem 0;
  405. background: #fef1eb;
  406. height: .56rem;
  407. line-height: .56rem;
  408. padding: 0 .12rem;
  409. a {
  410. font-size: .3rem;
  411. color: #666;
  412. }
  413. &:nth-child(1) {
  414. a {
  415. color: #fc5708;
  416. }
  417. }
  418. }
  419. }
  420. }
  421. }
  422. }
  423. </style>