MainSearch.vue 13 KB

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