MainSearch.vue 13 KB

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