Search.vue 13 KB

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