Search.vue 12 KB

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