Search.vue 12 KB

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