Search.vue 14 KB

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