Search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <div class="search-box">
  3. <div class="input-group">
  4. <template v-if="!isPcb">
  5. <div class="type">
  6. <span class="type-item" :class="{active: newSearchType == 'component'}" @click="setNewSearchType('component')">型号</span>
  7. <span class="type-item" :class="{active: newSearchType == 'kind'}" @click="setNewSearchType('kind')">物料名称</span>
  8. <span class="type-item" :class="{active: newSearchType == 'store'}" @click="setNewSearchType('store')">卖家</span>
  9. <span class="type-item" :class="{active: newSearchType == 'brand'}" @click="setNewSearchType('brand')">品牌</span>
  10. </div>
  11. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  12. :placeholder="placeholderByType"
  13. @focus.stop.prevent="onFocus()"
  14. @blur.stop.prevent="onBlur()"
  15. @keyup.13="onSearch()"
  16. @keyup.40="onSelectChange(1)"
  17. @keyup.38="onSelectChange(-1)"
  18. />
  19. <span class="input-group-btn" @click="onSearch()" style="z-index: 10">
  20. <button class="btn btn-primary search-btn" type="button" :class="{'Isblue':!SelectItem}">搜&nbsp;索</button>
  21. </span>
  22. </template>
  23. <template v-if="isPcb">
  24. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  25. :placeholder="'请输入pcb型号'"
  26. @focus.stop.prevent="onFocus()"
  27. @blur.stop.prevent="onBlur()"
  28. style="width: 441px;"
  29. @keyup.13="onSearch()"/>
  30. <span class="input-group-btn" @click="onSearch()" style="z-index: 10">
  31. <button class="btn btn-primary search-btn" type="button" :class="{'Isblue':!SelectItem}">搜&nbsp;索</button>
  32. </span>
  33. </template>
  34. </div>
  35. <ul class="association"
  36. :class="{'association2': !SelectItem, 'pcb-asso': isPcb}"
  37. v-show="showAssociate"
  38. @mouseenter="associate.focus=true"
  39. @mouseleave="associate.focus=false"
  40. >
  41. <li v-for="(v, index) in similarKeywords.result" class="item"
  42. :class="{'active': index==associate.activeIndex}"
  43. @click.stop.prevent="onAssociateClick(v.value)">{{v.value}}
  44. </li>
  45. </ul>
  46. <div class="search-hot" v-if="SelectItem">
  47. <ul class="list-untyled">
  48. <li class="item item-first">热门搜索</li>
  49. <li class="item" v-for="w in hotDeviceBrand">
  50. <a :href="w.detailsLink">{{ w.title }}</a>
  51. </li>
  52. </ul>
  53. </div>
  54. </div>
  55. </template>
  56. <script>
  57. export default {
  58. name: 'search-box',
  59. props: {
  60. SelectItem: {
  61. type: Boolean,
  62. default: true
  63. },
  64. isPcb: {
  65. type: Boolean,
  66. default: false
  67. }
  68. },
  69. data () {
  70. return {
  71. keyword: '',
  72. associate: {
  73. focus: false,
  74. show: false,
  75. activeIndex: null
  76. },
  77. click_flag: false,
  78. searchType: 'product',
  79. // 新版搜索控制变量
  80. newSearchType: 'component',
  81. isInit: false
  82. }
  83. },
  84. computed: {
  85. hotDeviceBrand () {
  86. return this.$store.state.hotSearch.hot.data.data ? this.$store.state.hotSearch.hot.data.data : []
  87. },
  88. similarKeywords () {
  89. let list = []
  90. let data = this.$store.state.search.keywords.data.result
  91. if (data && data.length > 0) {
  92. data.forEach(item => {
  93. if (this.newSearchType === 'component' && item.code) {
  94. list.push({
  95. value: item.code
  96. })
  97. } else if (this.newSearchType === 'kind' && item.nameCn) {
  98. list.push({
  99. value: item.nameCn
  100. })
  101. } else if (this.newSearchType === 'store' && item.name) {
  102. list.push({
  103. value: item.name
  104. })
  105. } else if (this.newSearchType === 'brand' && item.nameEn) {
  106. list.push({
  107. value: item.nameEn
  108. })
  109. }
  110. })
  111. }
  112. return {
  113. result: list
  114. }
  115. },
  116. showAssociate () {
  117. return this.keyword &&
  118. this.associate.show &&
  119. this.similarKeywords.result &&
  120. this.similarKeywords.result.length
  121. },
  122. placeholderByType () {
  123. let type
  124. switch (this.newSearchType) {
  125. case 'component':
  126. type = '型号'
  127. break
  128. case 'brand':
  129. type = '品牌'
  130. break
  131. case 'kind':
  132. type = '物料名称'
  133. break
  134. case 'store':
  135. type = '卖家名称'
  136. break
  137. default:
  138. type = '型号'
  139. }
  140. return `请输入${type}`
  141. }
  142. },
  143. watch: {
  144. 'keyword': {
  145. handler (val, oldVal) {
  146. let keywords = this.similarKeywords
  147. if (!keywords.result || !keywords.result.length || this.associate.activeIndex === null || val !== keywords.result[this.associate.activeIndex].value) {
  148. this.onChange()
  149. }
  150. }
  151. },
  152. '$route': {
  153. handler: function (route) {
  154. if (route.path === '/search') {
  155. this.newSearchType = route.query.type || 'component'
  156. this.keyword = route.query.w || ''
  157. this.isInit = true
  158. }
  159. },
  160. immediate: true
  161. }
  162. },
  163. methods: {
  164. onFocus () {
  165. this.associate.show = true
  166. },
  167. onBlur () {
  168. this.associate.show = this.associate.focus
  169. },
  170. onSelectChange (count) {
  171. let keywords = this.similarKeywords
  172. if (keywords && keywords.result.length) {
  173. let index = this.associate.activeIndex
  174. if (index === null) {
  175. index = -1
  176. }
  177. index += count
  178. if (index >= keywords.result.length) {
  179. index = 0
  180. } else if (index < 0) {
  181. index = keywords.result.length - 1
  182. }
  183. this.associate.activeIndex = index
  184. this.keyword = keywords.result[index].value || keywords.result[index].code || keywords.result[index].nameCn || keywords.result[index].name
  185. }
  186. },
  187. onChange () {
  188. this.associate.activeIndex = null
  189. if (!this.keyword) {
  190. this.associate.show = false
  191. this.$store.dispatch('resetSearchKeywords')
  192. } else {
  193. if (!this.isInit) {
  194. this.searchKeywords()
  195. } else {
  196. this.isInit = false
  197. }
  198. }
  199. if (this.click_flag) {
  200. this.associate.show = false
  201. this.click_flag = false
  202. }
  203. },
  204. searchKeywords () {
  205. this.associate.show = true
  206. this.$store.dispatch('searchKeywords', { keyword: this.keyword, type: this.newSearchType })
  207. },
  208. onSearch (str) {
  209. document.getElementsByClassName('search-input')[0].blur()
  210. if (this.keyword) {
  211. this.associate.show = false
  212. this.$store.dispatch('resetSearchKeywords')
  213. if (this.isPcb) {
  214. this.$router.push({path: '/pcb/search?w=' + encodeURIComponent(this.keyword)})
  215. } else {
  216. if (this.searchType === 'product') {
  217. this.$router.push({path: `/search?w=${encodeURIComponent(this.keyword)}&type=${this.newSearchType}`})
  218. } else if (this.searchType === 'store') {
  219. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  220. }
  221. }
  222. } else {
  223. if (!str) { this.$message.info('请输入关键字') }
  224. }
  225. },
  226. onAssociateClick (word) {
  227. this.click_flag = true
  228. this.keyword = word
  229. this.onSearch()
  230. },
  231. isCnStart () {
  232. if (this.keyword && this.keyword.length > 0) {
  233. return this.keyword.charCodeAt(0) > 255
  234. }
  235. },
  236. setNewSearchType (type) {
  237. this.newSearchType = type
  238. this.onSearch('fromLabel')
  239. }
  240. },
  241. created () {
  242. this.$store.dispatch('resetSearchKeywords')
  243. this.$store.dispatch('loadHotSearch')
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped type="text/scss">
  248. @import '~assets/scss/variables';
  249. .form-control{
  250. border-radius: 0;
  251. }
  252. .search-box {
  253. width: 520px;
  254. height: 40px;
  255. position: relative;
  256. .search-input{
  257. width: 441px;
  258. float: left;
  259. }
  260. .search-input, .search-btn {
  261. height: 40px;
  262. border-width: 2px;
  263. }
  264. .select-type{
  265. width: 70px;
  266. float: left;
  267. border: #5078cb 2px solid;
  268. height: 40px;
  269. border-right: none;
  270. margin-right: -1px;
  271. }
  272. .search-btn {
  273. font-size: 16px;
  274. width: 79px;
  275. border-radius: 0;
  276. }
  277. .Isblue{
  278. vertical-align: middle;
  279. display: inline-block;
  280. text-align: center;
  281. width: 72px;
  282. border-left: 0;
  283. background: #d3e1fc;
  284. color: #5078cb;
  285. text-align: center;
  286. font-size: 14px;
  287. position: absolute;
  288. right: 0;
  289. top: 0;
  290. &:hover {
  291. background: #d2272d;
  292. color: #fff;
  293. transition: all .3s;
  294. }
  295. }
  296. .search-hot ul{
  297. line-height: 12px;
  298. }
  299. .search-hot ul li a{
  300. color: #838383;
  301. }
  302. .search-hot {
  303. margin-top:5px;
  304. overflow: hidden;
  305. height:25px;
  306. .item {
  307. display: inline-block;
  308. max-width:22%;
  309. text-align: center;
  310. vertical-align: middle;
  311. font-size: $font-size-small;
  312. padding-right: $pad;
  313. a{
  314. display:block;
  315. overflow: hidden;
  316. text-overflow: ellipsis;
  317. white-space: nowrap;
  318. &:hover{
  319. color: $red;
  320. }
  321. }
  322. &.item-first {
  323. width:12%;
  324. color: $red;
  325. }
  326. }
  327. }
  328. .association {
  329. position: absolute;
  330. left: 0;
  331. top: 100%;
  332. right: 81px;
  333. background: $white;
  334. border: $border;
  335. border-top-width: 0;
  336. z-index: 21;
  337. &.pcb-asso {
  338. left: 0;
  339. }
  340. .item {
  341. padding: 0 15px;
  342. line-height: 30px;
  343. cursor: pointer;
  344. overflow: hidden;
  345. text-overflow: ellipsis;
  346. white-space: nowrap;
  347. &.active {
  348. background-color: $dark-bg;
  349. }
  350. &:hover {
  351. background-color: $grey-bg;
  352. }
  353. }
  354. .similar-title {
  355. padding: 0 15px;
  356. line-height: 30px;
  357. font-size: 16px;
  358. font-weight: bold;
  359. border-top: 1px solid #ccc;
  360. cursor: default;
  361. }
  362. }
  363. .association2 {
  364. left: 2px;
  365. right: 71px
  366. }
  367. .input-group {
  368. .type {
  369. position: absolute;
  370. top: -21px;
  371. .type-item {
  372. padding: 4px 12px;
  373. color: #5078cb;
  374. font-size: 14px;
  375. cursor: pointer;
  376. &.active, &:hover {
  377. background: #4071c6;
  378. color: #fff;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. .search-box2 {
  385. width: 442px;
  386. }
  387. </style>