Search.vue 13 KB

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